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.
Installing rsync
On most modern Linux distributions you will find rsync comes preinstalled. If that's not the case, just install it with your package manager. On Ubuntu this would look like:
$ aptitude -y install rsync
done!
Simple - One Command
Let's copy our local /home/kevin/source
to /home/kevin/destination
which resides on the server: server.example.com
:
$ rsync -az --progress --size-only /home/kevin/source/* server.example.com:/home/kevin/destination/
explained:
-a
archive, preserves all attributes like recursive ownership, timestamps, etc-z
compress, saves bandwidth but is harder on your CPU so use it for slow/expensive connections only--progress
shows you the progress of all the files that are being synced--size-only
compare 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: /home/kevin/destination
.
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.
Easy Script
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 /root/bin/syncdata.bash
$ $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
And type
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 deleted from /home/kevin/source/*
are also deleted at the destination, thanks to the --delete
parameter.
Some Extra rsync Command Line Options
Some extra arguments that might come in handy customizing your synchronization job:
--delete
delete files remotely that no longer exist locally--dry-run
show what would have been transferred, but do not transfer anything--max-delete=10
don't delete more than 10 files in one run, safety precaution--delay-updates
put all updated files into place at transfer's end, very useful for live systems--compress-level=9
explicitly set compression level 9. 0 disabled compression--exclude-from=/root/sync_exclude
specifies a /root/sync_exclude that contains exclude patterns (one per line). filenames matching these patterns will not be tranfered--bwlimit=1024
This option specifies a maximum transfer rate of 1024 kilobytes per second.
Pitfalls
- 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.