{"id":1887,"date":"2016-09-19T15:37:39","date_gmt":"2016-09-19T20:37:39","guid":{"rendered":"http:\/\/swildow.darktech.org\/wp\/?p=1887"},"modified":"2016-09-19T15:37:39","modified_gmt":"2016-09-19T20:37:39","slug":"use-rsync-to-copysync-files-between-servers","status":"publish","type":"post","link":"https:\/\/www.wildow.com\/blog\/?p=1887","title":{"rendered":"Use Rsync To Copy\/Sync Files Between Servers"},"content":{"rendered":"<header>\n<h1 class=\"entry-title\">Use Rsync To Copy\/Sync Files Between Servers<\/h1>\n<div class=\"entry\">\n<footer class=\"entry-meta\"><span class=\"tag-links\"><i class=\"fa fa-tags\"><\/i><br \/>\n<\/span><\/footer>\n<footer class=\"entry-meta\"><a href=\"https:\/\/www.atlantic.net\/community\/howto\/use-rsync-copysync-files-servers\/\">https:\/\/www.atlantic.net\/community\/howto\/use-rsync-copysync-files-servers\/<\/a><\/footer>\n<footer class=\"entry-meta\"><\/footer>\n<\/div>\n<\/header>\n<div class=\"entry\">\n<div class=\"post-content\">\n<h3>Introduction<\/h3>\n<p>Rsync is a great way to synchronize files between servers or to simply move files between servers without the need of FTP. It connects two servers via the SSH protocol, allowing for the transfer of data between them. The rsync daemon, covered later in this article, uses its own protocol and runs on a specified port. Below we\u2019ll go over the basics; copying between servers, copying to\/from rsync daemons, and keeping files in sync with each other on different servers.<\/p>\n<h3>Prerequisites<\/h3>\n<p>\u2013 2 <a href=\"http:\/\/atlantic.net\/cloud-hosting\/\">Linux servers <\/a>that both have rsync installed.<br \/>\n\u2013 The server doing the initiating of the transfer needs to be able to access the receiving server\u2019s SSH port (or rsync port, if connecting to an rsync daemon).<br \/>\n\u2013 The user needs to have permissions to use rsync (the examples below will be run as the root user).<\/p>\n<h2>Basics of Rsync<\/h2>\n<p>Rsync can push files to another server or pull files from another server. The difference between the two is in the syntax. If you specify your local file first, it will initiate a push. If you specify your remote host first, it will initiate a pull. Let\u2019s see the exact type of syntax below.<\/p>\n<p>Push:<\/p>\n<div class=\"EnlighterJSWrapper classicEnlighterJSWrapper\">\n<ul class=\"hoverEnabled classicEnlighterJS EnlighterJS\">\n<li class=\" odd\"><span class=\"\">rsync <\/span><span class=\"br0\">[<\/span><span class=\"\">-options<\/span><span class=\"br0\">]<\/span><span class=\"\"> SOURCE user@x.x.x.x:DESTINATION<\/span><\/li>\n<\/ul>\n<\/div>\n<p>Pull:<\/p>\n<div class=\"EnlighterJSWrapper classicEnlighterJSWrapper\">\n<ul class=\"hoverEnabled classicEnlighterJS EnlighterJS\">\n<li class=\" odd\"><span class=\"\">rsync <\/span><span class=\"br0\">[<\/span><span class=\"\">-options<\/span><span class=\"br0\">]<\/span><span class=\"\"> user@x.x.x.x:SOURCE DESTINATION<\/span><\/li>\n<\/ul>\n<\/div>\n<p>The <span class=\"enlighterEnlighterJS EnlighterJS\"><span class=\"br0\">[<\/span><span class=\"\">-options<\/span><span class=\"br0\">]<\/span><\/span> are all the different options that can be used. Some I\u2019ll reference and explain in examples below. For a full list of all the options, review the <a href=\"http:\/\/linux.die.net\/man\/1\/rsync\" target=\"_blank\">manual page<\/a>.<\/p>\n<p><span class=\"enlighterEnlighterJS EnlighterJS\"><span class=\"\">SOURCE<\/span><\/span> stands for the <span class=\"enlighterEnlighterJS EnlighterJS\"><span class=\"\">\/directory\/path\/to\/the\/file\/or\/directory<\/span><\/span> which will be copied.<\/p>\n<p><span class=\"enlighterEnlighterJS EnlighterJS\"><span class=\"\">DESTINATION<\/span><\/span> stands for the <span class=\"enlighterEnlighterJS EnlighterJS\"><span class=\"\">\/directory\/path\/to\/the\/new\/location<\/span><\/span> on the receiving host.<\/p>\n<p>The <span class=\"enlighterEnlighterJS EnlighterJS\"><span class=\"\">user@<\/span><\/span> is the user on the remote host for rsync to connect with.<\/p>\n<p><span class=\"enlighterEnlighterJS EnlighterJS\"><span class=\"\">x.x.x.x<\/span><\/span> stands for the IP address of the remote host. It can also be a hostname if you have DNS configured.<\/p>\n<p>&nbsp;<\/p>\n<p>Now, say you need to specify a port outside of the default port 22 for rsync to connect to. You would do this with the option <span class=\"enlighterEnlighterJS EnlighterJS\"><span class=\"\">-e<\/span><\/span>. See the example below (replace <span class=\"enlighterEnlighterJS EnlighterJS\"><span class=\"\">PORT<\/span><\/span> with the port number you need to connect to).<\/p>\n<p>Push:<\/p>\n<div class=\"EnlighterJSWrapper classicEnlighterJSWrapper\">\n<ul class=\"hoverEnabled classicEnlighterJS EnlighterJS\">\n<li class=\" odd\"><span class=\"\">rsync <\/span><span class=\"br0\">[<\/span><span class=\"\">-other options<\/span><span class=\"br0\">]<\/span><span class=\"\"> -e <\/span><span class=\"st0\">&#8216;ssh -p PORT&#8217;<\/span><span class=\"\"> SOURCE user@x.x.x.x:DESTINATION<\/span><\/li>\n<\/ul>\n<\/div>\n<p>Pull:<\/p>\n<div class=\"EnlighterJSWrapper classicEnlighterJSWrapper\">\n<ul class=\"hoverEnabled classicEnlighterJS EnlighterJS\">\n<li class=\" odd\"><span class=\"\">rsync <\/span><span class=\"br0\">[<\/span><span class=\"\">-other options<\/span><span class=\"br0\">]<\/span><span class=\"\"> -e <\/span><span class=\"st0\">&#8216;ssh -p PORT&#8217;<\/span><span class=\"\"> user@x.x.x.x:SOURCE DESTINATION<\/span><\/li>\n<\/ul>\n<\/div>\n<p>&nbsp;<\/p>\n<h2>Simple File Transfer Examples<\/h2>\n<p>First, we will initiate a push of a .txt file from one server to another.<\/p>\n<div class=\"EnlighterJSWrapper classicEnlighterJSWrapper\">\n<ul class=\"hoverEnabled classicEnlighterJS EnlighterJS\">\n<li class=\" odd\"><span class=\"\">rsync \/home\/simple.txt root@x.x.x.x:\/home<\/span><\/li>\n<\/ul>\n<\/div>\n<p>This command will copy the file <span class=\"enlighterEnlighterJS EnlighterJS\"><span class=\"\">simple.txt<\/span><\/span> from our local server, to our remote server\u2019s <span class=\"enlighterEnlighterJS EnlighterJS\"><span class=\"\">\/home<\/span><\/span>directory.<\/p>\n<p><a href=\"https:\/\/www.atlantic.net\/community\/wp-content\/uploads\/2015\/06\/anet-rsync-001.png\"><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-4708 size-full\" src=\"https:\/\/www.atlantic.net\/community\/wp-content\/uploads\/2015\/06\/anet-rsync-001.png\" srcset=\"https:\/\/www.atlantic.net\/community\/wp-content\/uploads\/2015\/06\/anet-rsync-001.png 1322w, https:\/\/www.atlantic.net\/community\/wp-content\/uploads\/2015\/06\/anet-rsync-001-300x96.png 300w, https:\/\/www.atlantic.net\/community\/wp-content\/uploads\/2015\/06\/anet-rsync-001-1024x328.png 1024w\" alt=\"Transfer between two servers\" width=\"1322\" height=\"424\" \/><\/a><\/p>\n<p>On the right, we did an <span class=\"enlighterEnlighterJS EnlighterJS\"><span class=\"\">ls<\/span><\/span> before the rsync and then again after it was run, so we can see the file was copied.<\/p>\n<p>Here is an example of a pull:<\/p>\n<div class=\"EnlighterJSWrapper classicEnlighterJSWrapper\">\n<ul class=\"hoverEnabled classicEnlighterJS EnlighterJS\">\n<li class=\" odd\"><span class=\"\">rsync root@x.x.x.x:\/home\/pullme.txt \/home<\/span><\/li>\n<\/ul>\n<\/div>\n<p>The file <span class=\"enlighterEnlighterJS EnlighterJS\"><span class=\"\">pullme.txt<\/span><\/span> is on the remote server (x.x.x.x) in the <span class=\"enlighterEnlighterJS EnlighterJS\"><span class=\"\">\/home<\/span><\/span> directory. This command will copy the file to the local server\u2019s <span class=\"enlighterEnlighterJS EnlighterJS\"><span class=\"\">\/home<\/span><\/span> directory.<\/p>\n<p>You can push\/pull any single file you specify using this method. Now, let\u2019s get into directories.<\/p>\n<p>&nbsp;<\/p>\n<h2>Directory File Transfer<\/h2>\n<p>Moving a directory is simple. Add the <span class=\"enlighterEnlighterJS EnlighterJS\"><span class=\"\">-d<\/span><\/span> option, and it will move only the directory. If you want to move the directory and its contents, use <span class=\"enlighterEnlighterJS EnlighterJS\"><span class=\"\">-r<\/span><\/span> instead to recursively copy over all files and subfolders. While we\u2019re at it, let\u2019s tack on the <span class=\"enlighterEnlighterJS EnlighterJS\"><span class=\"\">-t<\/span><\/span> option. This option will copy the date and time that the file was last changed. Using <span class=\"enlighterEnlighterJS EnlighterJS\"><span class=\"\">-t<\/span><\/span> is good for keeping accurate records of when a file was last edited. These options will work with pushing or pulling.<\/p>\n<p>In the below example, I have moved the <span class=\"enlighterEnlighterJS EnlighterJS\"><span class=\"\">pullme.txt<\/span><\/span> file into a <span class=\"enlighterEnlighterJS EnlighterJS\"><span class=\"\">\/transfer_me<\/span><\/span> directory. I have also created the directory <span class=\"enlighterEnlighterJS EnlighterJS\"><span class=\"\">\/simple<\/span><\/span> (which contains the file <span class=\"enlighterEnlighterJS EnlighterJS\"><span class=\"\">simple.txt<\/span><\/span>) inside <span class=\"enlighterEnlighterJS EnlighterJS\"><span class=\"\">\/transfer_me<\/span><\/span>. I then pushed the <span class=\"enlighterEnlighterJS EnlighterJS\"><span class=\"\">\/transfer_me<\/span><\/span> directory to the remote server.<\/p>\n<div class=\"EnlighterJSWrapper classicEnlighterJSWrapper\">\n<ul class=\"hoverEnabled classicEnlighterJS EnlighterJS\">\n<li class=\" odd\"><span class=\"\">rsync -rt \/home\/transfer_me root@x.x.x.x:\/home<\/span><\/li>\n<\/ul>\n<\/div>\n<p><a href=\"https:\/\/www.atlantic.net\/community\/wp-content\/uploads\/2015\/06\/anet-rsync-021.png\"><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-4709 size-full\" src=\"https:\/\/www.atlantic.net\/community\/wp-content\/uploads\/2015\/06\/anet-rsync-021.png\" srcset=\"https:\/\/www.atlantic.net\/community\/wp-content\/uploads\/2015\/06\/anet-rsync-021.png 1260w, https:\/\/www.atlantic.net\/community\/wp-content\/uploads\/2015\/06\/anet-rsync-021-300x101.png 300w, https:\/\/www.atlantic.net\/community\/wp-content\/uploads\/2015\/06\/anet-rsync-021-1024x346.png 1024w\" alt=\"transfering a directory\" width=\"1260\" height=\"426\" \/><\/a><\/p>\n<p>As you can see, the folder on the remote (receiving) server contains all the subdirectories and files that the local folder did on the initiating server.<\/p>\n<p>&nbsp;<\/p>\n<h2>Connecting to an Rsync Daemon<\/h2>\n<p>This section requires a server running an rsync daemon. To test these instructions out, see our article on <a href=\"https:\/\/www.atlantic.net\/community\/howto\/setup-rsync-daemon\" target=\"_blank\">setting up an rsync daemon<\/a>.<\/p>\n<p>Now that we\u2019ve gone over basic file\/directory transferring with rsync, we can go over connecting to an rsync daemon. An rsync daemon is running constantly on a server so you can push to and\/or pull from that server\u2019s assigned rsync daemon directory at any time. There are two formats to pulling and pushing to an rsync daemon.<\/p>\n<p>Pull:<\/p>\n<div class=\"EnlighterJSWrapper classicEnlighterJSWrapper\">\n<ul class=\"hoverEnabled classicEnlighterJS EnlighterJS\">\n<li class=\" odd\"><span class=\"\">rsync <\/span><span class=\"br0\">[<\/span><span class=\"\">-options<\/span><span class=\"br0\">]<\/span><span class=\"\"> rsync<\/span><span class=\"co1\">:\/\/user@x.x.x.x[:PORT]SOURCE DESTINATION<\/span><\/li>\n<\/ul>\n<\/div>\n<p>Pull (note the double colons after the IP address\/hostname):<\/p>\n<div class=\"EnlighterJSWrapper classicEnlighterJSWrapper\">\n<ul class=\"hoverEnabled classicEnlighterJS EnlighterJS\">\n<li class=\" odd\"><span class=\"\">rsync <\/span><span class=\"br0\">[<\/span><span class=\"\">-options<\/span><span class=\"br0\">]<\/span><span class=\"\"> user@x.x.x.x::SOURCE DESTINATION<\/span><\/li>\n<\/ul>\n<\/div>\n<p>Push:<\/p>\n<div class=\"EnlighterJSWrapper classicEnlighterJSWrapper\">\n<ul class=\"hoverEnabled classicEnlighterJS EnlighterJS\">\n<li class=\" odd\"><span class=\"\">rsync <\/span><span class=\"br0\">[<\/span><span class=\"\">-options<\/span><span class=\"br0\">]<\/span><span class=\"\"> SOURCE rsync<\/span><span class=\"co1\">:\/\/user@x.x.x.x[:PORT]DESTINATION<\/span><\/li>\n<\/ul>\n<\/div>\n<p>Push (note the double colons after the IP address\/hostname):<\/p>\n<div class=\"EnlighterJSWrapper classicEnlighterJSWrapper\">\n<ul class=\"hoverEnabled classicEnlighterJS EnlighterJS\">\n<li class=\" odd\"><span class=\"\">rsync <\/span><span class=\"br0\">[<\/span><span class=\"\">-options<\/span><span class=\"br0\">]<\/span><span class=\"\"> SOURCE user@x.x.x.x::DESTINATION<\/span><\/li>\n<\/ul>\n<\/div>\n<p>&nbsp;<\/p>\n<p>See below how I pulled from the rsync daemon (the use of port number 873 below is redundant, since that\u2019s the default rsync daemon connection port\u2013I include it to demonstrate how you would format the command if you had set up a non-default port on your rsync daemon):<\/p>\n<div class=\"EnlighterJSWrapper classicEnlighterJSWrapper\">\n<ul class=\"hoverEnabled classicEnlighterJS EnlighterJS\">\n<li class=\" odd\"><span class=\"\">rsync -rt rsync<\/span><span class=\"co1\">:\/\/root@x.x.x.x:873\/files\/ \/home\/pulledfromdaemon<\/span><\/li>\n<\/ul>\n<\/div>\n<p><a href=\"https:\/\/www.atlantic.net\/community\/wp-content\/uploads\/2015\/06\/anet-rsync-031.png\"><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-4710 size-full\" src=\"https:\/\/www.atlantic.net\/community\/wp-content\/uploads\/2015\/06\/anet-rsync-031.png\" srcset=\"https:\/\/www.atlantic.net\/community\/wp-content\/uploads\/2015\/06\/anet-rsync-031.png 671w, https:\/\/www.atlantic.net\/community\/wp-content\/uploads\/2015\/06\/anet-rsync-031-300x189.png 300w\" alt=\"Rsync daemon pull\" width=\"671\" height=\"423\" \/><\/a><\/p>\n<p>Here, <span class=\"enlighterEnlighterJS EnlighterJS\"><span class=\"\">files<\/span><\/span> is the module name. Since we\u2019re pulling everything in that directory (<span class=\"enlighterEnlighterJS EnlighterJS\"><span class=\"\">pullme.txt<\/span><\/span> and <span class=\"enlighterEnlighterJS EnlighterJS\"><span class=\"\">simple.txt<\/span><\/span>) we simply leave it at the module name for the source.<\/p>\n<p>&nbsp;<\/p>\n<h2>Syncing Files with Rsync<\/h2>\n<p>Now we\u2019ll discuss how to sync files. You can perform a sync with a single rsync command, with the right options. You can put this command in a script or cron job to run on demand or automatically. The command varies if you are pushing or pulling or using the daemon, but the options remain the same.<\/p>\n<div class=\"EnlighterJSWrapper classicEnlighterJSWrapper\">\n<ul class=\"hoverEnabled classicEnlighterJS EnlighterJS\">\n<li class=\" odd\"><span class=\"\">rsync -rtu &#8211;delete SOURCE root@x.x.x.x:\/DESTINATION<\/span><\/li>\n<\/ul>\n<\/div>\n<p>In this example, pay attention to the <span class=\"enlighterEnlighterJS EnlighterJS\"><span class=\"\">-rtu &#8211;delete<\/span><\/span> options. We have already discussed the <span class=\"enlighterEnlighterJS EnlighterJS\"><span class=\"\">-r<\/span><\/span> and <span class=\"enlighterEnlighterJS EnlighterJS\"><span class=\"\">-t<\/span><\/span> options\u2013recursive copy and time stamp copy. The <span class=\"enlighterEnlighterJS EnlighterJS\"><span class=\"\">-u<\/span><\/span> option tells rsync to update only different\/changed\/added files from the <span class=\"enlighterEnlighterJS EnlighterJS\"><span class=\"\">SOURCE<\/span><\/span> to the <span class=\"enlighterEnlighterJS EnlighterJS\"><span class=\"\">DESTINATION<\/span><\/span>. The option <span class=\"enlighterEnlighterJS EnlighterJS\"><span class=\"\">&#8211;delete<\/span><\/span> will remove any files from the <span class=\"enlighterEnlighterJS EnlighterJS\"><span class=\"\">DESTINATION<\/span><\/span> that the <span class=\"enlighterEnlighterJS EnlighterJS\"><span class=\"\">SOURCE<\/span><\/span> may no longer have. With this (and other options, like <span class=\"enlighterEnlighterJS EnlighterJS\"><span class=\"\">-l<\/span><\/span> for symlinks) you can keep directories in multiple locations up to date with the source.<\/p>\n<p>&nbsp;<\/p>\n<p>You can now rsync files between servers, rsync daemons, and keep files synced with each other with ease. Thank you for following this how-to! Please check back here for more updates, and to consider a <a href=\"http:\/\/atlantic.net\/cloud-hosting\/\">market-leading cloud server<\/a> from Atlantic.Net.<\/p>\n<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Use Rsync To Copy\/Sync Files Between Servers https:\/\/www.atlantic.net\/community\/howto\/use-rsync-copysync-files-servers\/ Introduction Rsync is a great way to synchronize files between servers or to simply move files between servers without the need of FTP. It connects two servers via the SSH protocol, allowing &#8230; <a class=\"more-link\" href=\"https:\/\/www.wildow.com\/blog\/?p=1887\">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":[1],"tags":[],"class_list":["post-1887","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/www.wildow.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1887","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=1887"}],"version-history":[{"count":1,"href":"https:\/\/www.wildow.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1887\/revisions"}],"predecessor-version":[{"id":1888,"href":"https:\/\/www.wildow.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1887\/revisions\/1888"}],"wp:attachment":[{"href":"https:\/\/www.wildow.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1887"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wildow.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1887"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wildow.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1887"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}