Backing up files locally

The first example shows a simple backup of a user's personal files. Here I'm copying the /home/chris directory (including all its files and subdirectories) to another directory on the local computer. That directory (/mnt/backup/homes) could be on a separate partition (see Chapter 2 for creating separate partitions), hard disk (see Chapter 10 to add a hard disk), or a remote NFS file system (see Chapter 18 to mount an NFS file system):

# rsync -av /home/chris /mnt/backup/homes/

NOTE: Notice that there's no trailing slash after /home/chris (so it's not /home/chris/). Without that trailing slash, rsync will copy files from that directory to a target directory named chris (/mnt/backup/homes/chris). With a trailing slash, all files from /home/chris / are copied directly to the homes directory (/mnt/backup/homes/).

In this example, the entire contents of the /home/chris directory structure are added to the /mnt/backup/homes/chris directory. All files, subdirectories, links, devices, and other file types are copied. By using the archive option (-a), all ownership, permissions, and creation times are maintained on the copied files. Using the -a option saves you from having to enter the following options individually: -r (recursive), -l (copy symbolic links), -p (preserve permissions), -t (preserve times), -g (preserve group), -o (preserve owner) and -D (preserve devices and special files). The verbose option (-v) results in more messages being displayed as rsync progresses.

If /mnt/backup/homes is on a separate disk, you now have your entire /home/chris directory copied in two places on the same machine. If the /mnt/backup/homes directory is an NFS shared directory (with write permission on), the files are now backed up to another machine.

Because the example is a backup of my personal files that don't change too often, after a few days of changes to the files, I might want to run the exact same command again:

# rsync -av /home/chris /mnt/backup/homes/

This time any new files are copied to the target directory and the changes to any files I modified are applied to the original backup files. Any files I deleted from my home directory will still be in the target directory (rsync doesn't remove deleted files unless you specifically tell it to). The result is, again, a complete copy of the /home/chris directory at the moment the rsync command is run, plus any files that have been deleted from any /home/chris directories.

NOTE: If you want files that were deleted from the sending directory to be likewise deleted from the target directory, you can add the - -delete option to rsync.

Computer Hard Drive Data Recovery

Computer Hard Drive Data Recovery

Learn How To Recover Your Hard Drive Data After A Computer Failure.

Get My Free Ebook

Post a comment