How to Backup Linux Home Directories Using rsync
Posted on In LinuxI need to backup my Linux home directory to one of my portable hard disk. I tried to use git, but failed since git doesn’t support large file (I failed after many tries, I have file larger than 5G). I find rsync, the fast, versatile, remote (and local) file-copying tool and I am happy with it now. rsync is very fast especially the change is incremental. rsync can generate a delta of the change.
The method is simple, just execute this command:
$ rsync -avxP \ /path/to/directory/to/backup \ /path/to/directory/for/storing/backup
-a means archive mode -v means print out the verbose information during backuping -x means not cross filesystem boundaries -P is shortcut for --progress means printing information showing the progress of the transfer --partial means keeping partially transferred files if the transfer is interrupted
Note that the files on the destination directory storing the backup data will be kept if the files do not exist in the source directory to be backup’ed. If you would like `rsync` to delete the files/directories in the destination directory if the files/directories does not exist in the source directory, you can add `–delete` to `rsync`:
$ rsync -avxP --delete \ /path/to/directory/to/backup \ /path/to/directory/for/storing/backup
You can check all options of rsync from [[man:1|rsync|rsync’s man page]].
That’s it. You can use cron to run it every day or every week.
The method can also be used to backup other kinds of directories besides of linux home directories given the user running `rsync` has sufficient permission to the directories and files. rsync also supports copying files to remote host. Please check “man rsync” for more.
You haven’t explained what –delete does. Does it mean that it deletes the extra unnecessary data?
Good point. `–delete` will delete the extra files in the destination directory. It deserves special introduction. Updated the original post. Thanks!
Eric
You started with “I need to backup my Linux home directory to one of my portable hard disk.”
You ended with “The method can also be used to backup any kinds of directory except linux home ”
Huh?
Are you using user crontab -e or system /etc/crontab?
The sentence you quoted indeed was not accurate. I have revised it a little bit. This post is on the method. For how to use the method for certain cases, the users may need to do some further work, e.g. ensuring the correct user having sufficient permission to run the job.
About your question, if you would like to back up all users’ home directories, a crontab job by ‘root’ may be needed.