Using rsync to Backup a Home Folder
I have been trying to get more serious about my personal backups lately and I have finally started to figure out a scheme. I have a primary machine that is setup as a RAID 1 with two drives such that it keeps two copies of every file. And then every once in a while I copy the home directory with all of my files from that computer to an external drive.
It takes forever to transfer my home directory full of files over to the drive, so I decide to try and use rsync to synchronize the drives by only copying, moving and deleting the files that have changed.
For those of you who have never heard of rsync, it is an Open Source utility that can be used to copy files from one place to the other while minimizing the amount of transferring by detecting differences between the source and destination and only transferring the differences. So if you are trying to archive or backup a lot of files this can save a lot of time.
rsync is a pretty advanced tool and it took me a while to get the command correct, so I thought I would share it here in hopes that it might save someone else some time.
# rsync -rptDuv -e ssh user@127.0.0.1:/home/user/* /media/backup
Let me explain this in detail. First you have the command itself.
# rsync ...
Then you have the options.
# ... -rptDuv ...
The ‘r’ is for recursive, which means that it will go into folders and copy those files as well. The ‘p’ keeps the file permissions in tact. The ‘t’ is for time, which means that the copied files will keep the same created and modified times as the original. The ‘D’ is to account for special files, I am not sure that this was needed for my purposes. The ‘u’ is for update, which means it is not to copy files that already exist on the destination. The ‘v’ is for verbose, which just means that it will tell you what it is doing as it does it.
The next part is the source folder (i.e. the files you want to backup). Since my source folder was actually on another computer I had to specify some extra stuff to connect to it.
# ... -e ssh user@127.0.0.1:/home/user/* ...
If you were just backing up a folder on the same computer you could just do something like this instead.
# ... /home/user/* ...
One thing to note about the source is that I had to specify the ‘*’ at the end to prevent it from trying to backup hidden files as well. Since most of these files were just random configuration files for the user, I didn’t feel I needed to back them up.
Last is the destination (i.e. where you are keeping your backups). I am transferring mine to an external hard drive so I can take it to another location.
# ... /media/backup
The first time you run the command it will transfer the whole set of folders and files to the backup drive. But after that it will only update the backup drive with the changes you have made.
Thanks to Chad from the Linux Basement for helping me work through this command over IRC.