-avz flags. This approach ensures data redundancy and consistent synchronization between your local machine and the remote storage device without manual intervention or complex network configurations.
Prerequisites and Environment Setup
To successfully mirror files using rsync, you must have SSH access enabled on your Synology DiskStation Manager (DSM). Navigate to the Control Panel in DSM, select "Terminal & SNMP," and check the box for "Enable SSH service." Note the port number, which defaults to 22 but may be customized for security reasons. On your local Linux machine, ensure the rsync utility is installed via your package manager, such as apt install rsync on Debian-based systems.
Before running any synchronization commands, verify that your local user has read permissions for the source directory and that the destination path on the NAS is writable. If you are mirroring large directories, consider the bandwidth limitations of your network connection. For faster initial transfers, you might want to compress data during transit, which rsync handles natively.
Configuring SSH Key Authentication
Automating backups requires passwordless login. Generate an SSH key pair on your local machine if you do not already have one by running ssh-keygen -t ed25519. Press Enter to accept the default file location. Next, copy the public key to your NAS using the following command, replacing username and nas_ip_address with your actual credentials:
ssh-copy-id username@nas_ip_address
If you encounter connection issues, refer to our guide on Troubleshooting kex_exchange_identification Error in Linux SSH Connection. This article helps resolve common cryptographic handshake failures that can prevent rsync from establishing a secure tunnel. Once the key is copied, test the connection by running ssh username@nas_ip_address. You should log in without being prompted for a password.
The Core Rsync Command
The standard command to mirror a directory structure uses several critical flags. The -a flag enables archive mode, which preserves symbolic links, permissions, timestamps, and group ownership. The -v flag provides verbose output, allowing you to monitor progress. The -z flag compresses file data during the transfer, reducing bandwidth usage.
To perform a full mirror, adding the --delete flag is essential. This ensures that files deleted from the source are also removed from the destination, keeping the two directories identical. Without this flag, rsync only updates new or changed files, leaving orphaned files on the NAS. Construct your command as follows:
rsync -avz --delete /local/source/path/ username@nas_ip_address:/remote/destination/path/
Note the trailing slash on the source path. Including it tells rsync to copy the *contents* of the directory into the destination folder. Omitting it creates a new subdirectory named after the source folder inside the destination. Always double-check your paths before hitting enter, as --delete is irreversible within the session.
Handling Sparse Files
If your source directory contains sparse files (files that appear larger than their actual disk usage), the transfer time may be significantly longer than expected. In such cases, use the -S operator to handle sparse files efficiently. For more details on managing these files, see How to Shrink Sparse Files in Linux After Copying Them Without the -S Operator. This optimization prevents unnecessary data transmission for empty blocks.
Scheduling Automated Mirrors
For continuous redundancy, schedule the command using crontab. Open the crontab editor with crontab -e and add a line to run the script at your desired interval. For example, to run a backup every night at 2 AM, use:
0 2 * * * /usr/bin/rsync -avz --delete /local/source/ username@nas_ip_address:/remote/dest/ > /var/log/rsync_backup.log 2>&1
This setup logs output to a file, helping you diagnose issues if the sync fails. Ensure the cron job runs as the same user who owns the SSH keys to avoid permission errors.
Frequently Asked Questions
What happens if I omit the trailing slash on the source path?
Omitting the trailing slash causes rsync to create a new directory inside the destination that matches the name of the source folder. Instead of merging contents, it nests them, which breaks the mirror structure. Always include the slash to synchronize the contents directly.
Can I use rsync to mirror a directory to another local directory?
Yes, rsync works locally without SSH. Simply remove the user@host: portion from the destination argument. However, you lose the benefit of compression (-z) since local transfers do not typically benefit from network-level compression overhead.
How do I resume a failed transfer?
rsync automatically resumes from where it left off if interrupted. It checks file sizes and modification times before transferring data. If a file is partially transferred, rsync will restart it from the beginning unless you use specific partial transfer flags, which are rarely needed for standard backups.
Why is my rsync transfer so slow compared to SCP?
Slow speeds often result from high CPU load due to compression (-z) on weak hardware, or network latency. Try removing the -z flag if your local network is fast. Additionally, ensure you are not syncing millions of tiny files, which incurs significant overhead per file operation.
Is it safe to use --delete on a production server?
Using --delete is dangerous if the source directory is accidentally emptied or misconfigured, as it will wipe the destination. Always test with --dry-run first to preview changes. Never use this flag unless you explicitly want the destination to become an exact clone of the source.