Using rsync to Mirror Directories
rsync utility can be used to mirror a directory locally or over a network. As the name implies,
rsync synchronizes two directories. Key benefits of the utility include:
- After the first copy, only differences in files are copied, not the entire file. This saves time and bandwidth.
sshso it can copy files securely over a network.
Command Basics and the slash
To copy a directory from one directory to another, they command line would be:
rsync -options --otherOptions sourceDir targetDir
rsync -vaz ~/bk/ ~/test
This example copies the contents of the
~/bk directory to the test
rsync -vaz ~/bk ~/test
This example creates a
bk directory under the
~/test directory and recursively copies the contents of the
~/bk into this new directory. This is slighly different from the first example.
Command Line Options
A few command line options are listed in the previous example. Each of these options is described below.
|-v||Turn on verbose mode|
|-a||This turns on archive mode. Bascially this causes
|-z||Turns on compression during the transfer. This option compresses the data as it is copied over the network.|
ssh to copy the files over the network, just add the
--rsh option to the command line. Simply specify the ssh command line as shown in the example. Also, to specify another machine as a target, precede the directory target with a host name and a colon.
rsync -vaz --rsh="ssh -l username" ~/bk targetHost:~/test
After typing the command line, you will be prompted for your password. After entering the password, the command executes and your files are copied. You can also set the
ssh command using the
RSYNC_RSH environment variable. You can also avoid entering the
ssh password if you use
This option allows you to exclude certain files and directories from the copy process. You can exclude by specific names or by using wildcards.
rsync -vaz --exclude=log/ --exclude=*.xml ~/bk targetHost:~/test
In this example, the
log directory is excluded from the copy as well as any file with a
This option deletes any files that exist in your target directories but that do not exist in the source directory struction. Using this option truly keeps your files synchronized. However, use this option with caution. It can delete a lot of stuff on the target machine if you aren't careful.