Synchronizing files from one server to another is quite awesome. You can use it for backups, for keeping web servers in sync, and much more. It's fast and it doesn't take up as much bandwidth as normal copying would. And the best thing is, it can be done with only 1 command. Welcome to the wonderful world of rsync.
$ aptitude -y install rsync
Simple - One Command
Let's copy our local
resides on the server:
$ rsync -az --progress --size-only /home/kevin/source/* server.example.com:/home/kevin/destination/
-aarchive, preserves all attributes like recursive ownership, timestamps, etc
-zcompress, saves bandwidth but is harder on your CPU so use it for slow/expensive connections only
--progressshows you the progress of all the files that are being synced
--size-onlycompare files based on their size instead of hashes (less CPU, so faster)
Note that this sync excludes hidden files since it uses the bash
*. If you want to include
hidden files, write the source like this:
/home/kevin/source/ and remove the trailing slash
from the destination like so:
Well, that's it! But read on if you want to learn how to automate this.
Advanced - Automatic Syncing With SSH Keys
Alright so syncing files on Linux is pretty easy. But what if we want to automate this? How can we avoid that rsync asks for a password every time?
There are different ways to go about this, but the one I mostly use is installing SSH keys. By installing your SSH key on the destination server, it will recognize you in the future and permit instant access. So this way we can automate the synchronization with rsync.
I've written another article explaining on setting up SSH keys. It also includes a script that can do all the work for you.
Did It Work?
Open a terminal and type:
$ ssh server.example.com
It should not ask you for any password. Great! this means we can also run rsync directly without logging in! If you need more in depth information on this, I wrote an article on logging in automatically with SSH keys.
Let's create a sync script
So now just create a script
$ $EDITOR /root/bin/syncdata.bash
that contains your rsync command:
#!/usr/bin/env bash rsync -az --delete /home/kevin/source/ server.example.com:/home/kevin/destination
Save the file and exit and make it executable like this:
$ chmod +x /root/bin/syncdata.bash
Schedule It to Run Every Hour
And to have your data synchronized every hour, open up your crontab editor:
$ crontab -e
0 * * * * /root/bin/syncdata.bash
(if you need more in depth information on crontab I've written another article on scheduling tasks on linux using crontab)
That's it! New files are automatically updated @
server.example.com:/home/kevin/destination/ every hour. Files that are
/home/kevin/source/* are also deleted at the destination, thanks
Some Extra rsync Command Line Options
Some extra arguments that might come in handy customizing your synchronization job:
--deletedelete files remotely that no longer exist locally
--dry-runshow what would have been transferred, but do not transfer anything
--max-delete=10don't delete more than 10 files in one run, safety precaution
--delay-updatesput all updated files into place at transfer's end, very useful for live systems
--compress-level=9explicitly set compression level 9. 0 disabled compression
--exclude-from=/root/sync_excludespecifies a /root/sync_exclude that contains exclude patterns (one per line). filenames matching these patterns will not be tranfered
--bwlimit=1024This option specifies a maximum transfer rate of 1024 kilobytes per second.
- Of course you should really be carefull where and when to install SSH keys, because if one machine is comprimised, it's very easy for a cracker to hop to the next system without logging in. So choose wisely when to use this technology. You might consider 'pulling' the files in from the backup machine if that one is less exposed. This way if your main machine gets hacked, they can't hop to your backup machine.
- Keys are user user specific. So if you're going to run programs as root that need to automatically login to systems, you must also install the key as root.