Sync Files Between Servers using rsync
This is a follow up post to Copy/Transfer Files Between Two Servers Using Linux SCP. Transferring via scp copies all the files which is what you want sometimes, but I find rsync to be a much better solution in most instances.
I'll provide a handful of ways I tend to use rsync but check out the full Linux man page for the a complete description of rsync and all the available options.
Synchronize from Local Server to Remote
rsync -avuz /var/www/example.com/ firstname.lastname@example.org:/var/www/example.com/ // only one file rsync -avuz /var/www/example.com/index.html email@example.com:/var/www/example.com/
- a = archive mode
- v = increase verbose
- u = skip files that are newer on the receiver
- z = compress file data during the transfer
- Archive mode is the same as options -rlptgoD
- r = recurse into directories
- l = copy symlinks as symlinks
- p = preserve permissions
- t = preserve modification times
- g = preserve group
- o = preserve owner (super-user only)
- D = preserve device & special files
Synchronize from Local Directory to Another Local Directory
rsync -avuz /var/www/example.com/ /user/websites/
View the progress of the Sync
A percentage of each file's status will be displayed which is useful if you're transferring large files. Not so useful for tons of small files.
rsync -avuz --progress /var/www/example.com/ firstname.lastname@example.org:/var/www/example.com/
Run Sync in the Background
If you're running a large sync with either lots of files or large files, you probably want to run it in the background. Nothings more frustrating than starting a large sync and then find out later your connection with the server dropped. Below will keep the job running even if you're connection gets dropped - actually you can exit the SSH session if you want. The job will continue to run.
- Start rsync command
- Press Ctrl+Z
- Enter bg command to place job to the background
- You can then view the job (or any other jobs) running in the background, by entering jobs command.
These are just a few ways to use rsync, but there are tons of other ways. If you're interested in leaning about some more checkout How to Backup Linux? 15 rsync Command Examples.