{"id":1715,"date":"2016-03-16T16:00:03","date_gmt":"2016-03-16T21:00:03","guid":{"rendered":"http:\/\/swildow.darktech.org\/wp\/?p=1715"},"modified":"2016-03-16T16:00:03","modified_gmt":"2016-03-16T21:00:03","slug":"how-to-use-rsync-to-sync-local-and-remote-directories-on-a-vps","status":"publish","type":"post","link":"https:\/\/www.wildow.com\/blog\/?p=1715","title":{"rendered":"How To Use Rsync to Sync Local and Remote Directories on a VPS"},"content":{"rendered":"<div class=\"container\">\n<h1 class=\"content-title\">How To Use Rsync to Sync Local and Remote Directories on a VPS<\/h1>\n<p><span class=\"meta-section tags\"><span class=\"meta-value\"><span class=\"tutorial-date\">Sep 10, 2013<\/span><span class=\"Apple-converted-space\">\u00a0<\/span><span class=\"Apple-converted-space\">\u00a0<\/span><a class=\"\" href=\"https:\/\/www.digitalocean.com\/community\/tags\/linux-basics?type=tutorials\">Linux Basics<\/a>,<span class=\"Apple-converted-space\">\u00a0<\/span><a class=\"\" href=\"https:\/\/www.digitalocean.com\/community\/tags\/backups?type=tutorials\">Backups<\/a><\/span><\/span><\/div>\n<div class=\"content-body tutorial-content tutorial-content-legacy\" data-growable-markdown=\"\">\n<h3 id=\"introduction\">Introduction<\/h3>\n<p><em>Rsync<\/em>, which stands for &#8220;remote sync&#8221;, 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.<\/p>\n<p>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.<\/p>\n<h2 id=\"what-is-rsync\">What Is Rsync?<\/h2>\n<p>Rsync is a very flexible network-enabled syncing tool. It can<span class=\"Apple-converted-space\">\u00a0<\/span><em>also<\/em><span class=\"Apple-converted-space\">\u00a0<\/span>refer to the network protocol developed to utilize this tool.<\/p>\n<p>When we reference rsync in this guide, we are mainly referring to the utility, and not the protocol.<\/p>\n<p>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.<\/p>\n<h2 id=\"basic-syntax\">Basic Syntax<\/h2>\n<p>The basic syntax of rsync is very straight forward, and operates in a way that is similar to ssh, scp, and cp.<\/p>\n<p>We will create two test directories and some test files with the following commands:<\/p>\n<pre class=\"code-pre \"><code>cd ~\r\nmkdir dir1\r\nmkdir dir2\r\ntouch dir1\/file{1..100}\r\n<\/code><\/pre>\n<p>We now have a directory called<span class=\"Apple-converted-space\">\u00a0<\/span><code>dir1<\/code><span class=\"Apple-converted-space\">\u00a0<\/span>with 100 empty files in it.<\/p>\n<pre class=\"code-pre \"><code>ls dir1\r\n<\/code><\/pre>\n<pre class=\"code-pre \"><code>file1    file18  file27  file36  file45  file54  file63  file72  file81  file90\r\nfile10   file19  file28  file37  file46  file55  file64  file73  file82  file91\r\nfile100  file2   file29  file38  file47  file56  file65  file74  file83  file92\r\nfile11   file20  file3   file39  file48  file57  file66  file75  file84  file93\r\nfile12   file21  file30  file4   file49  file58  file67  file76  file85  file94\r\nfile13   file22  file31  file40  file5   file59  file68  file77  file86  file95\r\nfile14   file23  file32  file41  file50  file6   file69  file78  file87  file96\r\nfile15   file24  file33  file42  file51  file60  file7   file79  file88  file97\r\nfile16   file25  file34  file43  file52  file61  file70  file8   file89  file98\r\nfile17   file26  file35  file44  file53  file62  file71  file80  file9   file99\r\n<\/code><\/pre>\n<p>We also have an empty directory called<span class=\"Apple-converted-space\">\u00a0<\/span><code>dir2<\/code>.<\/p>\n<p>To sync the contents of<span class=\"Apple-converted-space\">\u00a0<\/span><code>dir1<\/code><span class=\"Apple-converted-space\">\u00a0<\/span>to<span class=\"Apple-converted-space\">\u00a0<\/span><code>dir2<\/code><span class=\"Apple-converted-space\">\u00a0<\/span>on the same system, type:<\/p>\n<pre class=\"code-pre \"><code>rsync -r dir1\/ dir2\r\n<\/code><\/pre>\n<p>The<span class=\"Apple-converted-space\">\u00a0<\/span><strong>-r<\/strong><span class=\"Apple-converted-space\">\u00a0<\/span>option means recursive, which is necessary for directory syncing.<\/p>\n<p>We could also use the<span class=\"Apple-converted-space\">\u00a0<\/span><strong>-a<\/strong><span class=\"Apple-converted-space\">\u00a0<\/span>flag instead:<\/p>\n<pre class=\"code-pre \"><code>rsync -a dir1\/ dir2\r\n<\/code><\/pre>\n<p>The<span class=\"Apple-converted-space\">\u00a0<\/span><strong>-a<\/strong><span class=\"Apple-converted-space\">\u00a0<\/span>option is a combination flag.<\/p>\n<p>It stands for &#8220;archive&#8221; and syncs recursively and preserves symbolic links, special and device files, modification times, group, owner, and permissions.<\/p>\n<p>It is more commonly used than<span class=\"Apple-converted-space\">\u00a0<\/span><strong>-r<\/strong><span class=\"Apple-converted-space\">\u00a0<\/span>and is usually what you want to use.<\/p>\n<h3 id=\"an-important-note\">An Important Note<\/h3>\n<p>You may have noticed that there is a trailing slash (\/) at the end of the first argument in the above commands:<\/p>\n<pre>rsync -a dir1<span class=\"highlight\">\/<\/span> dir2\r\n<\/pre>\n<p>This is necessary to mean &#8220;the contents of<span class=\"Apple-converted-space\">\u00a0<\/span><code>dir1<\/code>&#8220;.<\/p>\n<p>The alternative, without the trailing slash, would place<span class=\"Apple-converted-space\">\u00a0<\/span><code>dir1<\/code>, including the directory, within<span class=\"Apple-converted-space\">\u00a0<\/span><code>dir2<\/code>. This would create a hierarchy that looks like:<\/p>\n<pre class=\"code-pre \"><code>~\/dir2\/dir1\/[files]\r\n<\/code><\/pre>\n<p>Always double-check your arguments before executing an rsync command.<\/p>\n<p>Rsync provides a method for doing this by passing the<span class=\"Apple-converted-space\">\u00a0<\/span><strong>-n<\/strong><span class=\"Apple-converted-space\">\u00a0<\/span>or<span class=\"Apple-converted-space\">\u00a0<\/span><strong>&#8211;dry-run<\/strong><span class=\"Apple-converted-space\">\u00a0<\/span>options. The<span class=\"Apple-converted-space\">\u00a0<\/span><strong>-v<\/strong><span class=\"Apple-converted-space\">\u00a0<\/span>flag (for verbose) is also necessary to get the appropriate output:<\/p>\n<pre class=\"code-pre \"><code>rsync -anv dir1\/ dir2\r\n<\/code><\/pre>\n<pre class=\"code-pre \"><code>sending incremental file list\r\n.\/\r\nfile1\r\nfile10\r\nfile100\r\nfile11\r\nfile12\r\nfile13\r\nfile14\r\nfile15\r\nfile16\r\nfile17\r\nfile18\r\n. . .\r\n<\/code><\/pre>\n<p>Compare this output to the output we get when we remove the trailing slash:<\/p>\n<pre class=\"code-pre \"><code>rsync -anv dir1 dir2\r\n<\/code><\/pre>\n<pre class=\"code-pre \"><code>sending incremental file list\r\ndir1\/\r\ndir1\/file1\r\ndir1\/file10\r\ndir1\/file100\r\ndir1\/file11\r\ndir1\/file12\r\ndir1\/file13\r\ndir1\/file14\r\ndir1\/file15\r\ndir1\/file16\r\ndir1\/file17\r\ndir1\/file18\r\n. . .\r\n<\/code><\/pre>\n<p>You can see here that the directory itself is transfered.<\/p>\n<h2 id=\"how-to-use-rsync-to-sync-with-a-remote-system\">How To Use Rsync to Sync with a Remote System<\/h2>\n<p>Syncing 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<span class=\"Apple-converted-space\">\u00a0<\/span><a href=\"https:\/\/www.digitalocean.com\/community\/articles\/how-to-set-up-ssh-keys--2\">set up SSH keys<\/a>, click here.<\/p>\n<p>Once you have SSH access verified on between the two machines, you can sync the<span class=\"Apple-converted-space\">\u00a0<\/span><code>dir1<\/code><span class=\"Apple-converted-space\">\u00a0<\/span>folder from earlier to a remote computer by using this syntax (note that we<span class=\"Apple-converted-space\">\u00a0<\/span><em>want<\/em><span class=\"Apple-converted-space\">\u00a0<\/span>to transfer the actual directory in this case, so we omit the trailing slash):<\/p>\n<pre>rsync -a ~\/dir1 <span class=\"highlight\">username<\/span>@<span class=\"highlight\">remote_host<\/span>:<span class=\"highlight\">destination_directory<\/span>\r\n<\/pre>\n<p>This is called a &#8220;push&#8221; operation because it pushes a directory from the local system to a remote system.<\/p>\n<p>The opposite operation is &#8220;pull&#8221;. It is used to sync a remote directory to the local system. If the<span class=\"Apple-converted-space\">\u00a0<\/span><code>dir1<\/code><span class=\"Apple-converted-space\">\u00a0<\/span>were on the remote system instead of our local system, the syntax would be:<\/p>\n<pre>rsync -a <span class=\"highlight\">username<\/span>@<span class=\"highlight\">remote_host<\/span>:<span class=\"highlight\">\/home\/username\/dir1<\/span> <span class=\"highlight\">place_to_sync_on_local_machine<\/span>\r\n<\/pre>\n<p>Like &#8220;cp&#8221; and similar tools, the source is always the first argument, and the destination is always the second.<\/p>\n<h2 id=\"useful-options-for-rsync\">Useful Options for Rsync<\/h2>\n<p>Rsync provides many options for altering the default behavior of the utility. We have already discussed some of the more necessary flags.<\/p>\n<p>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<span class=\"Apple-converted-space\">\u00a0<\/span><strong>-z<\/strong><span class=\"Apple-converted-space\">\u00a0<\/span>option:<\/p>\n<pre>rsync -az <span class=\"highlight\">source<\/span> <span class=\"highlight\">destination<\/span>\r\n<\/pre>\n<p>The<span class=\"Apple-converted-space\">\u00a0<\/span><strong>-P<\/strong><span class=\"Apple-converted-space\">\u00a0<\/span>flag is very helpful. It combines the flags<span class=\"Apple-converted-space\">\u00a0<\/span><strong>&#8211;progress<\/strong><span class=\"Apple-converted-space\">\u00a0<\/span>and<span class=\"Apple-converted-space\">\u00a0<\/span><strong>&#8211;partial<\/strong>. The first of these gives you a progress bar for the transfers and the second allows you to resume interrupted transfers:<\/p>\n<pre>rsync -azP <span class=\"highlight\">source<\/span> <span class=\"highlight\">destination<\/span>\r\n<\/pre>\n<pre class=\"code-pre \"><code>sending incremental file list\r\n.\/\r\nfile1\r\n           0 100%    0.00kB\/s    0:00:00 (xfer#1, to-check=99\/101)\r\nfile10\r\n           0 100%    0.00kB\/s    0:00:00 (xfer#2, to-check=98\/101)\r\nfile100\r\n           0 100%    0.00kB\/s    0:00:00 (xfer#3, to-check=97\/101)\r\nfile11\r\n           0 100%    0.00kB\/s    0:00:00 (xfer#4, to-check=96\/101)\r\n. . .\r\n<\/code><\/pre>\n<p>If we run the command again, we will get a shorter output, because no changes have been made.<\/p>\n<p>This illustrates rsync&#8217;s ability to use modification times to determine if changes have been made.<\/p>\n<pre>rsync -azP <span class=\"highlight\">source<\/span> <span class=\"highlight\">destination<\/span>\r\n<\/pre>\n<pre class=\"code-pre \"><code>sending incremental file list\r\n\r\nsent 818 bytes received 12 bytes 1660.00 bytes\/sec\r\ntotal size is 0 speedup is 0.00\r\n<\/code><\/pre>\n<p>We can update the modification time on some of the files and see that rsync intelligently re-copies only the changed files:<\/p>\n<pre>touch dir1\/file{1..10}\r\nrsync -azP <span class=\"highlight\">source<\/span> <span class=\"highlight\">destination<\/span>\r\n<\/pre>\n<pre class=\"code-pre \"><code>sending incremental file list\r\nfile1\r\n           0 100%    0.00kB\/s    0:00:00 (xfer#1, to-check=99\/101)\r\nfile10\r\n           0 100%    0.00kB\/s    0:00:00 (xfer#2, to-check=98\/101)\r\nfile2\r\n           0 100%    0.00kB\/s    0:00:00 (xfer#3, to-check=87\/101)\r\nfile3\r\n           0 100%    0.00kB\/s    0:00:00 (xfer#4, to-check=76\/101)\r\n. . .\r\n<\/code><\/pre>\n<p>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.<\/p>\n<p>We can change this behavior with the<span class=\"Apple-converted-space\">\u00a0<\/span><strong>&#8211;delete<\/strong><span class=\"Apple-converted-space\">\u00a0<\/span>option. Before using this option, use the<span class=\"Apple-converted-space\">\u00a0<\/span><strong>&#8211;dry-run<\/strong><span class=\"Apple-converted-space\">\u00a0<\/span>option and do testing to prevent data loss:<\/p>\n<pre>rsync -a --delete <span class=\"highlight\">source<\/span> <span class=\"highlight\">destination<\/span>\r\n<\/pre>\n<p>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<span class=\"Apple-converted-space\">\u00a0<\/span><strong>&#8211;exclude=<\/strong><span class=\"Apple-converted-space\">\u00a0<\/span>option:<\/p>\n<pre>rsync -a --exclude=<span class=\"highlight\">pattern_to_exclude<\/span> <span class=\"highlight\">source<\/span> <span class=\"highlight\">destination<\/span>\r\n<\/pre>\n<p>If we have specified a pattern to exclude, we can override that exclusion for files that match a different pattern by using the<span class=\"Apple-converted-space\">\u00a0<\/span><strong>&#8211;include=<\/strong><span class=\"Apple-converted-space\">\u00a0<\/span>option.<\/p>\n<pre>rsync -a --exclude=<span class=\"highlight\">pattern_to_exclude<\/span> --include=<span class=\"highlight\">pattern_to_include<\/span> <span class=\"highlight\">source<\/span> <span class=\"highlight\">destination<\/span>\r\n<\/pre>\n<h2 id=\"conclusion\">Conclusion<\/h2>\n<p>Rsync 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.<\/p>\n<p>A mastery of rsync allows you to design complex backup operations and obtain fine-grained control over what is transferred and how.<\/p>\n<div class=\"author\">By Justin Ellingwood<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>How To Use Rsync to Sync Local and Remote Directories on a VPS Sep 10, 2013\u00a0\u00a0Linux Basics,\u00a0Backups Introduction Rsync, which stands for &#8220;remote sync&#8221;, is a remote and local file synchronization tool. It uses an algorithm that minimizes the amount &#8230; <a class=\"more-link\" href=\"https:\/\/www.wildow.com\/blog\/?p=1715\">Read More &raquo;<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[12],"tags":[31],"class_list":["post-1715","post","type-post","status-publish","format-standard","hentry","category-linux","tag-rsync"],"_links":{"self":[{"href":"https:\/\/www.wildow.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1715","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wildow.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wildow.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wildow.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wildow.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1715"}],"version-history":[{"count":1,"href":"https:\/\/www.wildow.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1715\/revisions"}],"predecessor-version":[{"id":1716,"href":"https:\/\/www.wildow.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1715\/revisions\/1716"}],"wp:attachment":[{"href":"https:\/\/www.wildow.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1715"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wildow.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1715"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wildow.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1715"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}