{"id":11707,"date":"2019-03-15T23:03:28","date_gmt":"2019-03-15T23:03:28","guid":{"rendered":"http:\/\/www.appservgrid.com\/paw92\/?p=11707"},"modified":"2019-03-15T23:03:28","modified_gmt":"2019-03-15T23:03:28","slug":"how-to-sync-two-apache-web-servers-websites-using-rsync","status":"publish","type":"post","link":"https:\/\/www.appservgrid.com\/paw92\/index.php\/2019\/03\/15\/how-to-sync-two-apache-web-servers-websites-using-rsync\/","title":{"rendered":"How to Sync Two Apache Web Servers\/Websites Using Rsync"},"content":{"rendered":"<p>There are so many tutorials available on web to mirror or take a backup of your web files with different methods, here I am creating this article for my future reference and here I\u2019ll be using a very simple and versatile command of\u00a0<strong>Linux<\/strong>\u00a0to create a backup of your website. This tutorial will help you to sync data between your two web servers with \u201c<strong>Rsync<\/strong>\u201c.<\/p>\n<div id=\"attachment_4702\" class=\"wp-caption aligncenter\">\n<p><a href=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2013\/11\/Sync-Apache-Server.jpeg\"><img loading=\"lazy\" decoding=\"async\" class=\"size-medium wp-image-4702\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2013\/11\/Sync-Apache-Server-620x423.jpeg\" sizes=\"auto, (max-width: 620px) 100vw, 620px\" srcset=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2013\/11\/Sync-Apache-Server-620x423.jpeg 620w, https:\/\/www.tecmint.com\/wp-content\/uploads\/2013\/11\/Sync-Apache-Server.jpeg 655w\" alt=\"Sync Apache Web Server\" width=\"620\" height=\"423\" data-lazy-loaded=\"true\" \/><\/a><\/p>\n<p class=\"wp-caption-text\">Sync Two Apache Web Server<\/p>\n<\/div>\n<p>The purpose of creating a mirror of your\u00a0<strong>Web Server<\/strong>\u00a0with\u00a0<strong>Rsync<\/strong>\u00a0is if your main web server fails, your backup server can take over to reduce downtime of your website. This way of creating a web server backup is very good and effective for small and medium size web businesses.<\/p>\n<h3>Advantages of Syncing Web Servers<\/h3>\n<p>The main advantages of creating a web server backup with rsync are as follows:<\/p>\n<ol>\n<li>Rsync syncs only those bytes and blocks of data that have changed.<\/li>\n<li>Rsync has the ability to check and delete those files and directories at backup server that have been deleted from the main web server.<\/li>\n<li>It takes care of permissions, ownerships and special attributes while copying data remotely.<\/li>\n<li>It also supports SSH protocol to transfer data in an encrypted manner so that you will be assured that all data is safe.<\/li>\n<li>Rsync uses compression and decompression method while transferring data which consumes less bandwidth.<\/li>\n<\/ol>\n<h3>How To Sync Two Apache Web Servers<\/h3>\n<p>Let\u2019s proceed with setting up rsync to create a mirror of your web server. Here, I\u2019ll be using two servers.<\/p>\n<h5>Main Server<\/h5>\n<ol>\n<li><strong>IP Address<\/strong>: 192.168.0.100<\/li>\n<li><strong>Hostname<\/strong>: webserver.example.com<\/li>\n<\/ol>\n<h5>Backup Server<\/h5>\n<ol>\n<li><strong>IP Address<\/strong>: 192.168.0.101<\/li>\n<li><strong>Hostname<\/strong>: backup.example.com<\/li>\n<\/ol>\n<h4>Step 1: Install Rsync Tool<\/h4>\n<p>Here in this case web server data of\u00a0<strong>webserver.example.com<\/strong>\u00a0will be mirrored on\u00a0<strong>backup.example.com<\/strong>. And to do so first, we need to install\u00a0<strong>Rsync<\/strong>\u00a0on both the server with the help of following command.<\/p>\n<pre>[root@tecmint]# yum install rsync        [On <strong>Red Hat<\/strong> based systems]\r\n[root@tecmint]# apt-get install rsync    [On <strong>Debian<\/strong> based systems]<\/pre>\n<h4>Step 2: Create a User to run Rsync<\/h4>\n<p>We can setup rsync with\u00a0<strong>root<\/strong>\u00a0user, but for security reasons, you can create an unprivileged user on main webserver i.e\u00a0<strong>webserver.example.com<\/strong>\u00a0to run rsync.<\/p>\n<pre>[root@tecmint]# useradd tecmint\r\n[root@tecmint]# passwd tecmint<\/pre>\n<p>Here I have created a user \u201c<strong>tecmint<\/strong>\u201d and assigned a password to user.<\/p>\n<h4>Step 3: Test Rsync Setup<\/h4>\n<p>It\u2019s time to test your rsync setup on your backup server (i.e.\u00a0<strong>backup.example.com<\/strong>) and to do so, please type following command.<\/p>\n<pre>[root@backup www]# rsync -avzhe ssh tecmint@webserver.example.com:\/var\/www\/ \/var\/www<\/pre>\n<h5>Sample Output<\/h5>\n<pre>tecmint@webserver.example.com's password:\r\n\r\nreceiving incremental file list\r\nsent 128 bytes  received 32.67K bytes  5.96K bytes\/sec\r\ntotal size is 12.78M  speedup is 389.70<\/pre>\n<p>You can see that your rsync is now working absolutely fine and syncing data. I have used \u201c<strong>\/var\/www<\/strong>\u201d to transfer; you can change the folder location according to your needs.<\/p>\n<h4>Step 4: Automate Sync with SSH Passwordless Login<\/h4>\n<p>Now, we are done with rsync setups and now its time to setup a\u00a0<strong>cron<\/strong>\u00a0for rsync. As we are going to use rsync with\u00a0<strong>SSH<\/strong>\u00a0protocol, ssh will be asking for authentication and if we won\u2019t provide a password to cron it will not work. In order to work cron smoothly, we need to setup\u00a0<strong>passwordless ssh logins<\/strong>\u00a0for rsync.<\/p>\n<p>Here in this example, I am doing it as\u00a0<strong>root<\/strong>\u00a0to preserve\u00a0<strong>file ownerships<\/strong>\u00a0as well, you can do it for alternative users too.<\/p>\n<p>First, we\u2019ll generate a\u00a0<strong>public<\/strong>\u00a0and<strong>\u00a0private<\/strong>\u00a0key with following commands on backups server (i.e.\u00a0<strong>backup.example.com<\/strong>).<\/p>\n<pre>[root@backup]# ssh-keygen -t rsa -b 2048<\/pre>\n<p>When you enter this command, please don\u2019t provide passphrase and click enter for\u00a0<strong>Empty passphrase<\/strong>\u00a0so that rsync cron will not need any password for syncing data.<\/p>\n<h5>Sample Output<\/h5>\n<pre>Generating public\/private rsa key pair.\r\nEnter file in which to save the key (\/root\/.ssh\/id_rsa):\r\nEnter passphrase (empty for no passphrase):\r\nEnter same passphrase again:\r\nYour identification has been saved in \/root\/.ssh\/id_rsa.\r\nYour public key has been saved in \/root\/.ssh\/id_rsa.pub.\r\nThe key fingerprint is:\r\n9a:33:a9:5d:f4:e1:41:26:57:d0:9a:68:5b:37:9c:23 root@backup.exmple.com\r\nThe key's randomart image is:\r\n+--[ RSA 2048]----+\r\n|          .o.    |\r\n|           ..    |\r\n|        ..++ .   |\r\n|        o=E *    |\r\n|       .Sooo o   |\r\n|       =.o o     |\r\n|      * . o      |\r\n|     o +         |\r\n|    . .          |\r\n+-----------------+<\/pre>\n<p>Now, our\u00a0<strong>Public<\/strong>\u00a0and\u00a0<strong>Private<\/strong>\u00a0key has been generated and we will have to share it with main server so that main web server will recognize this backup machine and will allow it to login without asking any password while syncing data.<\/p>\n<pre>[root@backup html]# ssh-copy-id -i \/root\/.ssh\/id_rsa.pub root@webserver.example.com<\/pre>\n<p>Now try logging into the machine, with \u201c<strong>ssh \u2018root@webserver.example.com<\/strong>\u2018\u201d, and check in<strong>.ssh\/authorized_keys<\/strong>.<\/p>\n<pre>[root@backup html]# root@webserver.example.com<\/pre>\n<p>Now, we are done with sharing keys. To know more in-depth about\u00a0<strong>SSH password less login<\/strong>, you can read our article on it.<\/p>\n<ol>\n<li><a href=\"https:\/\/www.tecmint.com\/ssh-passwordless-login-using-ssh-keygen-in-5-easy-steps\/\" target=\"_blank\" rel=\"noopener\">SSH Passwordless Login in in 5 Easy Steps<\/a><\/li>\n<\/ol>\n<h4>Step 5: Schedule Cron To Automate Sync<\/h4>\n<p>Let\u2019s setup a cron for this. To setup a cron, please open crontab file with the following command.<\/p>\n<pre>[root@backup ~]# crontab \u2013e<\/pre>\n<p>It will open up \/etc\/crontab file to edit with your default editor. Here In this example, I am writing a cron to run it every 5 minutes to sync the data.<\/p>\n<pre>*\/5        *        *        *        *   rsync -avzhe ssh root@webserver.example.com:\/var\/www\/ \/var\/www\/<\/pre>\n<p>The above cron and rsync command simply syncing \u201c<strong>\/var\/www\/<\/strong>\u201d from the main\u00a0<strong>web server<\/strong>\u00a0to a\u00a0<strong>backup server<\/strong>in every\u00a0<strong>5 minutes<\/strong>. You can change the time and folder location configuration according to your needs. To be more creative and customize with\u00a0<strong>Rsync<\/strong>\u00a0and\u00a0<strong>Cron<\/strong>\u00a0command, you can check out our more detailed articles at:<\/p>\n<ol>\n<li><a href=\"https:\/\/www.tecmint.com\/rsync-local-remote-file-synchronization-commands\/\" target=\"_blank\" rel=\"noopener\">10 Rsync Commands to Sync Files\/Folders in Linux<\/a><\/li>\n<li><a href=\"https:\/\/www.tecmint.com\/11-cron-scheduling-task-examples-in-linux\/\" target=\"_blank\" rel=\"noopener\">11 Cron Scheduling Examples in Linux<\/a><\/li>\n<\/ol>\n<p><a href=\"https:\/\/www.tecmint.com\/sync-two-apache-websites-using-rsync\/\" target=\"_blank\" rel=\"noopener\">Source<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>There are so many tutorials available on web to mirror or take a backup of your web files with different methods, here I am creating this article for my future reference and here I\u2019ll be using a very simple and versatile command of\u00a0Linux\u00a0to create a backup of your website. This tutorial will help you to &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/www.appservgrid.com\/paw92\/index.php\/2019\/03\/15\/how-to-sync-two-apache-web-servers-websites-using-rsync\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;How to Sync Two Apache Web Servers\/Websites Using Rsync&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-11707","post","type-post","status-publish","format-standard","hentry","category-linux"],"_links":{"self":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts\/11707","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/comments?post=11707"}],"version-history":[{"count":1,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts\/11707\/revisions"}],"predecessor-version":[{"id":11708,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts\/11707\/revisions\/11708"}],"wp:attachment":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/media?parent=11707"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/categories?post=11707"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/tags?post=11707"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}