{"id":764,"date":"2015-02-20T17:03:01","date_gmt":"2015-02-21T00:03:01","guid":{"rendered":"http:\/\/homepages.uc.edu\/~yaozo\/wordpress\/?p=764"},"modified":"2015-02-20T17:03:01","modified_gmt":"2015-02-21T00:03:01","slug":"rsync-remote-sync-10-practical-examples-of-rsync-command-in-linux","status":"publish","type":"post","link":"https:\/\/zhuoyao.net\/index.php\/2015\/02\/20\/rsync-remote-sync-10-practical-examples-of-rsync-command-in-linux\/","title":{"rendered":"Rsync (Remote Sync): 10 Practical Examples of Rsync Command in Linux"},"content":{"rendered":"<p><strong>Rsync<\/strong> (<strong>Remote Sync<\/strong>) is a most commonly used command for <strong>copying<\/strong> and <strong>synchronizing<\/strong> files and directories <strong>remotely<\/strong> as well as <strong>locally<\/strong> in <strong>Linux<\/strong>\/<strong>Unix<\/strong> systems. With the help of <strong>rsync<\/strong> command you can copy and synchronize your data remotely and locally across directories, across disks and networks, perform data backups and mirroring between two Linux machines.<\/p>\n<p><a href=\"http:\/\/www.tecmint.com\/rsync-local-remote-file-synchronization-commands\/rsync-commands\/\" rel=\"attachment wp-att-4169\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-4169\" src=\"http:\/\/www.tecmint.com\/wp-content\/uploads\/2013\/09\/Rsync-Commands.png\" alt=\"Rsync Commands\" width=\"435\" height=\"321\" data-lazy-loaded=\"true\" \/><\/a><\/p>\n<p>This article explains <strong>10<\/strong> basic and advanced usage of the <strong>rsync<\/strong> command to transfer your files remotely and locally in <strong>Linux<\/strong> based machines. You don\u2019t need to be <strong>root<\/strong> user to run <strong>rsync<\/strong> command.<\/p>\n<h5>Some advantages and features of Rsync command<\/h5>\n<ol>\n<li>It efficiently copies and sync files to or from a remote system.<\/li>\n<li>Supports copying links, devices, owners, groups and permissions.<\/li>\n<li>It\u2019s faster than <strong>scp<\/strong> (<strong>Secure Copy<\/strong>) because <strong>rsync<\/strong> uses remote-update protocol which allows to transfer just the differences between two sets of files. First time, it copies the whole content of a file or a directory from source to destination but from next time, it copies only the changed blocks and bytes to the destination.<\/li>\n<li>Rsync consumes less <strong>bandwidth<\/strong> as it uses compression and decompression method while sending and receiving data both ends.<\/li>\n<\/ol>\n<h5>Basic syntax of rsync command<\/h5>\n<pre># rsync options source destination<\/pre>\n<h5>Some common options used with rsync commands<\/h5>\n<ol>\n<li><strong>-v<\/strong> : verbose<\/li>\n<li><strong>-r<\/strong> : copies data recursively (but don\u2019t preserve timestamps and permission while transferring data<\/li>\n<li><strong>-a<\/strong> : archive mode, archive mode allows copying files recursively and it also preserves symbolic links, file permissions, user &amp; group ownerships and timestamps<\/li>\n<li><strong>-z<\/strong> : compress file data<\/li>\n<li><strong>-h<\/strong> : human-readable, output numbers in a human-readable format<\/li>\n<\/ol>\n<h5>Install rsync in your Linux machine<\/h5>\n<p>We can install <strong>rsync<\/strong> package with the help of following command.<\/p>\n<pre># yum install rsync (On <strong>Red Hat<\/strong> based systems)\n# apt-get install rsync (On <strong>Debian<\/strong> based systems)<\/pre>\n<h3>1. Copy\/Sync Files and Directory Locally<\/h3>\n<h5>Copy\/Sync a File on a Local Computer<\/h5>\n<p>This following command will sync a single file on a local machine from one location to another location. Here in this example, a file name <strong>backup.tar<\/strong> needs to be copied or synced to <strong>\/tmp\/backups\/<\/strong> folder.<\/p>\n<pre>[root@tecmint]# rsync -zvh backup.tar \/tmp\/backups\/\n\ncreated directory \/tmp\/backups\n\nbackup.tar\n\nsent 14.71M bytes  received 31 bytes  3.27M bytes\/sec\n\ntotal size is 16.18M  speedup is 1.10<\/pre>\n<p>In above example, you can see that if the destination is not already exists rsync will create a directory automatically for destination.<\/p>\n<h5>Copy\/Sync a Directory on Local Computer<\/h5>\n<p>The following command will transfer or sync all the files of from one directory to a different directory in the same machine. Here in this example, <strong>\/root\/rpmpkgs<\/strong> contains some rpm package files and you want that directory to be copied inside <strong>\/tmp\/backups\/<\/strong> folder.<\/p>\n<pre>[root@tecmint]# rsync -avzh \/root\/rpmpkgs \/tmp\/backups\/\n\nsending incremental file list\n\nrpmpkgs\/\n\nrpmpkgs\/httpd-2.2.3-82.el5.centos.i386.rpm\n\nrpmpkgs\/mod_ssl-2.2.3-82.el5.centos.i386.rpm\n\nrpmpkgs\/nagios-3.5.0.tar.gz\n\nrpmpkgs\/nagios-plugins-1.4.16.tar.gz\n\nsent 4.99M bytes  received 92 bytes  3.33M bytes\/sec\n\ntotal size is 4.99M  speedup is 1.00<\/pre>\n<h3>2. Copy\/Sync Files and Directory to or From a Server<\/h3>\n<h5>Copy a Directory from Local Server to a Remote Server<\/h5>\n<p>This command will sync a directory from a local machine to a remote machine. <strong>For example<\/strong>: There is a folder in your local computer \u201c<strong>rpmpkgs<\/strong>\u201d which contains some <strong>RPM<\/strong> packages and you want that local directory\u2019s content send to a remote server, you can use following command.<\/p>\n<pre>[root@tecmint]$ rsync -avz rpmpkgs\/ root@192.168.0.101:\/home\/\n\nroot@192.168.0.101's password:\n\nsending incremental file list\n\n.\/\n\nhttpd-2.2.3-82.el5.centos.i386.rpm\n\nmod_ssl-2.2.3-82.el5.centos.i386.rpm\n\nnagios-3.5.0.tar.gz\n\nnagios-plugins-1.4.16.tar.gz\n\nsent 4993369 bytes  received 91 bytes  399476.80 bytes\/sec\n\ntotal size is 4991313  speedup is 1.00<\/pre>\n<h5>Copy\/Sync a Remote Directory to a Local Machine<\/h5>\n<p>This command will help you sync a remote directory to a local directory. Here in this example, a directory <strong>\/home\/tarunika\/rpmpkgs<\/strong> which is on a remote server is being copied in your local computer in <strong>\/tmp\/myrpms<\/strong>.<\/p>\n<pre>[root@tecmint]# rsync -avzh root@192.168.0.100:\/home\/tarunika\/rpmpkgs \/tmp\/myrpms\n\nroot@192.168.0.100's password:\n\nreceiving incremental file list\n\ncreated directory \/tmp\/myrpms\n\nrpmpkgs\/\n\nrpmpkgs\/httpd-2.2.3-82.el5.centos.i386.rpm\n\nrpmpkgs\/mod_ssl-2.2.3-82.el5.centos.i386.rpm\n\nrpmpkgs\/nagios-3.5.0.tar.gz\n\nrpmpkgs\/nagios-plugins-1.4.16.tar.gz\n\nsent 91 bytes  received 4.99M bytes  322.16K bytes\/sec\n\ntotal size is 4.99M  speedup is 1.00<\/pre>\n<h3>3. Rsync Over SSH<\/h3>\n<p>With rsync, we can use <strong>SSH<\/strong> (<strong>Secure Shell<\/strong>) for data transfer, using <strong>SSH<\/strong> protocol while transferring our data you can be ensured that your data is being transferred in a secured connection with encryption so that nobody can read your data while it is being transferred over the wire on the internet.<\/p>\n<p>Also when we use rsync we need to provide the <strong>user<\/strong>\/<strong>root<\/strong> password to accomplish that particular task, so using <strong>SSH<\/strong> option will send your logins in an encrypted manner so that your <strong>password<\/strong> will be safe.<\/p>\n<h5>Copy a File from a Remote Server to a Local Server with SSH<\/h5>\n<p>To specify a protocol with <strong>rsync<\/strong> you need to give \u201c<strong>-e<\/strong>\u201d option with protocol name you want to use. Here in this example, We will be using \u201c<strong>ssh<\/strong>\u201d with \u201c<strong>-e<\/strong>\u201d option and perform data transfer.<\/p>\n<pre>[root@tecmint]# rsync -avzhe ssh root@192.168.0.100:\/root\/install.log \/tmp\/\n\nroot@192.168.0.100's password:\n\nreceiving incremental file list\n\ninstall.log\n\nsent 30 bytes  received 8.12K bytes  1.48K bytes\/sec\n\ntotal size is 30.74K  speedup is 3.77<\/pre>\n<h5>Copy a File from a Local Server to a Remote Server with SSH<\/h5>\n<pre>[root@tecmint]# rsync -avzhe ssh backup.tar root@192.168.0.100:\/backups\/\n\nroot@192.168.0.100's password:\n\nsending incremental file list\n\nbackup.tar\n\nsent 14.71M bytes  received 31 bytes  1.28M bytes\/sec\n\ntotal size is 16.18M  speedup is 1.10<\/pre>\n<h3>4. Show Progress While Transferring Data with rsync<\/h3>\n<p>To show the progress while transferring the data from one machine to a different machine, we can use \u2018<strong>\u2013progress\u2019<\/strong> option for it. It displays the files and the time remaining to complete the transfer.<\/p>\n<pre>[root@tecmint]# rsync -avzhe ssh --progress \/home\/rpmpkgs root@192.168.0.100:\/root\/rpmpkgs\n\nroot@192.168.0.100's password:\n\nsending incremental file list\n\ncreated directory \/root\/rpmpkgs\n\nrpmpkgs\/\n\nrpmpkgs\/httpd-2.2.3-82.el5.centos.i386.rpm\n\n           1.02M 100%        2.72MB\/s        0:00:00 (xfer#1, to-check=3\/5)\n\nrpmpkgs\/mod_ssl-2.2.3-82.el5.centos.i386.rpm\n\n          99.04K 100%  241.19kB\/s        0:00:00 (xfer#2, to-check=2\/5)\n\nrpmpkgs\/nagios-3.5.0.tar.gz\n\n           1.79M 100%        1.56MB\/s        0:00:01 (xfer#3, to-check=1\/5)\n\nrpmpkgs\/nagios-plugins-1.4.16.tar.gz\n\n           2.09M 100%        1.47MB\/s        0:00:01 (xfer#4, to-check=0\/5)\n\nsent 4.99M bytes  received 92 bytes  475.56K bytes\/sec\n\ntotal size is 4.99M  speedup is 1.00<\/pre>\n<h3>5. Use of \u2013include and \u2013exclude Options<\/h3>\n<p>These two options allows us to <strong>include<\/strong> and <strong>exclude<\/strong> files by specifying parameters with these option helps us to specify those files or directories which you want to include in your sync and exclude files and folders with you don\u2019t want to be transferred.<\/p>\n<p>Here in this example, rsync command will include those files and directory only which starts with \u2018<strong>R<\/strong>\u2019 and exclude all other files and directory.<\/p>\n<pre>[root@tecmint]# rsync -avze ssh --include 'R*' --exclude '*' root@192.168.0.101:\/var\/lib\/rpm\/ \/root\/rpm\n\nroot@192.168.0.101's password:\n\nreceiving incremental file list\n\ncreated directory \/root\/rpm\n\n.\/\n\nRequirename\n\nRequireversion\n\nsent 67 bytes  received 167289 bytes  7438.04 bytes\/sec\n\ntotal size is 434176  speedup is 2.59<\/pre>\n<h3>6. Use of \u2013delete Option<\/h3>\n<p>If a file or directory not exist at the source, but already exists at the destination, you might want to delete that existing file\/directory at the target while syncing .<\/p>\n<p>We can use \u2018<strong>\u2013delete<\/strong>\u2018 option to delete files that are not there in source directory.<\/p>\n<p>Source and target are in sync. Now creating new file <strong>test.txt<\/strong> at the target.<\/p>\n<pre>[root@tecmint]# touch test.txt\n[root@tecmint]# rsync -avz --delete root@192.168.0.100:\/var\/lib\/rpm\/ .\nPassword:\nreceiving file list ... done\ndeleting test.txt\n.\/\nsent 26 bytes  received 390 bytes  48.94 bytes\/sec\ntotal size is 45305958  speedup is 108908.55<\/pre>\n<p>Target has the new file called <strong>test.txt<\/strong>, when synchronize with the source with \u2018<strong>\u2013delete<\/strong>\u2018 option, it removed the file <strong>test.txt<\/strong>.<\/p>\n<h3>7. Set the Max Size of Files to be Transferred<\/h3>\n<p>You can specify the <strong>Max<\/strong> file size to be transferred or sync. You can do it with \u201c<strong>\u2013max-size<\/strong>\u201d option. Here in this example, Max file size is <strong>200k<\/strong>, so this command will transfer only those files which are equal or smaller than <strong>200k<\/strong>.<\/p>\n<pre>[root@tecmint]# rsync -avzhe ssh --max-size='200k' \/var\/lib\/rpm\/ root@192.168.0.100:\/root\/tmprpm\n\nroot@192.168.0.100's password:\n\nsending incremental file list\n\ncreated directory \/root\/tmprpm\n\n.\/\n\nConflictname\n\nGroup\n\nInstalltid\n\nName\n\nProvideversion\n\nPubkeys\n\nRequireversion\n\nSha1header\n\nSigmd5\n\nTriggername\n\n__db.001\n\nsent 189.79K bytes  received 224 bytes  13.10K bytes\/sec\n\ntotal size is 38.08M  speedup is 200.43<\/pre>\n<h3>8. Automatically Delete source Files after successful Transfer<\/h3>\n<p>Now, suppose you have a main web server and a data backup server, you created a daily backup and synced it with your backup server, now you don\u2019t want to keep that local copy of backup in your web server.<\/p>\n<p>So, will you wait for transfer to complete and then delete those local backup file manually? Of Course NO. This automatic deletion can be done using \u2018<strong>\u2013remove-source-files<\/strong>\u2018 option.<\/p>\n<pre>[root@tecmint]# rsync --remove-source-files -zvh backup.tar \/tmp\/backups\/\n\nbackup.tar\n\nsent 14.71M bytes  received 31 bytes  4.20M bytes\/sec\n\ntotal size is 16.18M  speedup is 1.10\n\n[root@tecmint]# ll backup.tar\n\nls: backup.tar: No such file or directory<\/pre>\n<h3>9. Do a Dry Run with rsync<\/h3>\n<p>If you are a newbie and using rsync and don\u2019t know what exactly your command going do. Rsync could really mess up the things in your destination folder and then doing an undo can be a tedious job.<\/p>\n<p>Use of this option will not make any changes only do a dry run of the command and shows the output of the command, if the output shows exactly same you want to do then you can remove \u2018<strong>\u2013dry-run<\/strong>\u2018 option from your command and run on the terminal.<\/p>\n<pre>root@tecmint]# rsync --dry-run --remove-source-files -zvh backup.tar \/tmp\/backups\/\n\nbackup.tar\n\nsent 35 bytes  received 15 bytes  100.00 bytes\/sec\n\ntotal size is 16.18M  speedup is 323584.00 (DRY RUN)<\/pre>\n<h3>10. Set Bandwidth Limit and Transfer File<\/h3>\n<p>You can set the bandwidth limit while transferring data from one machine to another machine with the the help of \u2018<strong>\u2013bwlimit<\/strong>\u2018 option. This options helps us to limit <strong>I\/O<\/strong> bandwidth.<\/p>\n<pre>[root@tecmint]# rsync --bwlimit=100 -avzhe ssh  \/var\/lib\/rpm\/  root@192.168.0.100:\/root\/tmprpm\/\nroot@192.168.0.100's password:\nsending incremental file list\nsent 324 bytes  received 12 bytes  61.09 bytes\/sec\ntotal size is 38.08M  speedup is 113347.05<\/pre>\n<p>Also, by default rsync syncs changed blocks and bytes only, if you want explicitly want to sync whole file then you use \u2018<strong>-W<\/strong>\u2018 option with it.<\/p>\n<pre>[root@tecmint]# rsync -zvhW backup.tar \/tmp\/backups\/backup.tar\nbackup.tar\nsent 14.71M bytes  received 31 bytes  3.27M bytes\/sec\ntotal size is 16.18M  speedup is 1.10<\/pre>\n<p>That\u2019s all with rsync now, you can see <strong>man pages<\/strong> for more options. Stay connected with <strong>Tecmint<\/strong> for more exciting and interesting tutorials in future. Do leave your <strong>comments<\/strong> and <strong>suggestions<\/strong>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Rsync (Remote Sync) is a most commonly used command for copying and synchronizing files and directories remotely as well as locally in Linux\/Unix systems. With&hellip; <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[12],"tags":[],"class_list":["post-764","post","type-post","status-publish","format-standard","hentry","category-linux"],"_links":{"self":[{"href":"https:\/\/zhuoyao.net\/index.php\/wp-json\/wp\/v2\/posts\/764","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zhuoyao.net\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zhuoyao.net\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zhuoyao.net\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zhuoyao.net\/index.php\/wp-json\/wp\/v2\/comments?post=764"}],"version-history":[{"count":0,"href":"https:\/\/zhuoyao.net\/index.php\/wp-json\/wp\/v2\/posts\/764\/revisions"}],"wp:attachment":[{"href":"https:\/\/zhuoyao.net\/index.php\/wp-json\/wp\/v2\/media?parent=764"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zhuoyao.net\/index.php\/wp-json\/wp\/v2\/categories?post=764"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zhuoyao.net\/index.php\/wp-json\/wp\/v2\/tags?post=764"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}