The Flaw of Standard Copy Commands
If you need to move a 50GB database from a production server to an offsite backup server, using the standard scp (Secure Copy) or cp command is dangerous. If the network connection drops at 49GB, the standard copy command fails completely. You have to restart the transfer from 0GB.
Furthermore, if you are running a daily backup, you don’t want to copy all 50GB every single day. If only 10MB of data actually changed since yesterday, you only want to copy that 10MB to save time and bandwidth.
The solution is rsync (Remote Sync). It is the undisputed king of Linux file transfers. rsync uses a brilliant delta-transfer algorithm: it compares the source file and the destination file, and it only transmits the exact bytes that have changed. It is fully restartable, incredibly fast, and runs entirely over secure SSH.
The Basic Syntax
The syntax for rsync looks very similar to standard copy commands:
rsync [flags] [source] [destination]
Because it operates over SSH, the destination usually requires a username and IP address, followed by a colon and the directory path.
The Ultimate Backup Command
If you are backing up a web server directory to a remote machine, this is the command you should use:
rsync -avz --progress --delete /var/www/html/ [email protected]:/backup/website/
Breaking Down the Flags:
-a(Archive): This is the most important flag. It tellsrsyncto operate recursively (copying all subdirectories) while perfectly preserving file permissions, timestamps, symbolic links, and ownership. If you don’t use-a, your backup might have the wrong permissions, breaking your server if you ever need to restore it.-v(Verbose): Prints the names of the files being transferred to the terminal screen so you can see what is happening.-z(Compress): Compresses the data stream before sending it over the network. This uses slightly more CPU power but drastically reduces bandwidth usage, which is crucial for slow internet connections.--progress: Displays a live progress bar, the transfer speed (e.g., MB/s), and the estimated time remaining for each file.--delete(The Mirror Flag): Use with caution. By default,rsynconly adds or updates files. If you delete a file on your source server, it will still exist forever on the backup server. The--deleteflag tellsrsyncto delete files on the destination if they no longer exist on the source, creating a perfect 1:1 mirror.
The Trailing Slash Rule
The most common mistake new users make with rsync involves the trailing slash (/) on the source directory.
/var/www/html/(With slash): This tellsrsyncto copy the contents of thehtmlfolder into the destination./var/www/html(No slash): This tellsrsyncto copy thehtmlfolder itself into the destination (creating a new folder namedhtmlinside the backup directory).
Dry Runs (Testing Before Executing)
Because the --delete flag can be dangerous (if you accidentally swap the source and destination, you will delete your entire production server), you should always perform a “Dry Run” first.
rsync -avz --delete --dry-run /source/ [email protected]:/destination/
The --dry-run flag tells rsync to calculate everything it would do and print it to the screen, but without actually transferring or deleting a single byte. Once you confirm the output looks correct, remove the flag and run the real command.
Conclusion
rsync is the foundational technology behind almost every automated backup script in the Linux ecosystem. By utilizing its delta-transfer algorithm and perfect metadata preservation, you can build enterprise-grade, bandwidth-efficient backup routines directly from the command line.