IntroductionRsync, which stands for "remote sync", is a remote and local file synchronization tool. It uses an algorithm that minimizes the amount of data copied by only moving the portions of files that have changed. In this guide, we will cover the basic usage of this powerful utility. We will be using an Ubuntu 12.04 VPS in the examples, but you can use any modern Linux distribution to follow along. What Is Rsync?Rsync is a very flexible network-enabled syncing tool. It can also refer to the network protocol developed to utilize this tool. When we reference rsync in this guide, we are mainly referring to the utility, and not the protocol. Due to its ubiquity on Linux and Unix-like systems and its popularity as a tool for system scripts, it is included on most Linux distributions by default. Basic SyntaxThe basic syntax of rsync is very straight forward, and operates in a way that is similar to ssh, scp, and cp. We will create two test directories and some test files with the following commands:
We now have a directory called
We also have an empty directory called To sync the contents of
The -r option means recursive, which is necessary for directory syncing. We could also use the -a flag instead:
The -a option is a combination flag. It stands for "archive" and syncs recursively and preserves symbolic links, special and device files, modification times, group, owner, and permissions. It is more commonly used than -r and is usually what you want to use. An Important NoteYou may have noticed that there is a trailing slash (/) at the end of the first argument in the above commands: rsync -a dir1/ dir2
This is necessary to mean "the contents of The alternative, without the trailing slash, would place
Always double-check your arguments before executing an rsync command. Rsync provides a method for doing this by passing the -n or --dry-run options. The -v flag (for verbose) is also necessary to get the appropriate output:
Compare this output to the output we get when we remove the trailing slash:
You can see here that the directory itself is transfered. How To Use Rsync to Sync with a Remote SystemSyncing to a remote system is trivial if you have SSH access to the remote machine and rsync installed on both sides. If you need to set up SSH keys, click here. Once you have SSH access verified on between the two machines, you can sync the rsync -a ~/dir1 username@remote_host:destination_directory This is called a "push" operation because it pushes a directory from the local system to a remote system. The opposite operation is "pull". It is used to sync a remote directory to the local system. If the rsync -a username@remote_host:/home/username/dir1 place_to_sync_on_local_machine Like "cp" and similar tools, the source is always the first argument, and the destination is always the second. Useful Options for RsyncRsync provides many options for altering the default behavior of the utility. We have already discussed some of the more necessary flags. If you are transferring files that have not already been compressed, like text files, you can reduce the network transfer by adding compression with the -z option: rsync -az source destination The -P flag is very helpful. It combines the flags --progress and --partial. The first of these gives you a progress bar for the transfers and the second allows you to resume interrupted transfers: rsync -azP source destination
If we run the command again, we will get a shorter output, because no changes have been made. This illustrates rsync's ability to use modification times to determine if changes have been made. rsync -azP source destination
We can update the modification time on some of the files and see that rsync intelligently re-copies only the changed files: touch dir1/file{1..10} rsync -azP source destination
In order to keep two directories truly in sync, it is necessary to delete files from the destination directory if they are removed from the source. By default, rsync does not delete anything from the destination directory. We can change this behavior with the --delete option. Before using this option, use the --dry-run option and do testing to prevent data loss: rsync -a --delete source destination If you wish to exclude certain files or directories located inside a directory you are syncing, you can do so by specifying them in a comma-separated list following the --exclude= option: rsync -a --exclude=pattern_to_exclude source destination If we have specified a pattern to exclude, we can override that exclusion for files that match a different pattern by using the --include= option. rsync -a --exclude=pattern_to_exclude --include=pattern_to_include source destination ConclusionRsync can simplify file transfers over networked connections and add robustness to local directory syncing. The flexibility of rsync makes it a good option for many different file-level operations. A mastery of rsync allows you to design complex backup operations and obtain fine-grained control over what is transferred and how. By Justin Ellingwood - external |
Blog >