{"id":192,"date":"2013-04-17T14:35:08","date_gmt":"2013-04-17T18:35:08","guid":{"rendered":"http:\/\/devopsny.com\/blog\/?p=192"},"modified":"2013-04-17T14:42:04","modified_gmt":"2013-04-17T18:42:04","slug":"rsync-between-servers-automatically","status":"publish","type":"post","link":"https:\/\/devopsny.com\/blog\/2013\/04\/17\/rsync-between-servers-automatically\/","title":{"rendered":"Rsync Between Servers Automatically"},"content":{"rendered":"<div id=\"content\">\n<h1><span style=\"font-size: 1.285714286rem; line-height: 1.6;\">Part 1 &#8211; Setting up SSH key authentication<\/span><\/h1>\n<div id=\"bodyContent\">\n<p>First, we need to make sure the DESTSERVER has the ability to use key authentication enabled. Find your sshd configuration file (usually \u2018\/etc\/ssh\/sshd_config\u2019) and enable the following options if they are not already set.<\/p>\n<pre><i>RSAAuthentication yes<\/i>\r\n<i>PubkeyAuthentication yes<\/i>\r\n<i>AuthorizedKeysFile .ssh\/authorized_keys<\/i><\/pre>\n<p>If you edit the file be sure to restart sshd afterwards.<\/p>\n<pre><i># \/etc\/init.d\/sshd restart<\/i><\/pre>\n<p>Next, on the SOURCESERVER we will create the public \/ private key pair to be used for authentication with the following command.<\/p>\n<pre><i># ssh-keygen -t rsa<\/i><\/pre>\n<ul>\n<li>Note: Do not enter a passphrase for this, just hit enter when prompted.<\/li>\n<\/ul>\n<p>This should create 2 files, a public key file and a private key file. The public key file (usually [homedir]\/.ssh\/id_rsa.pub) we will upload to the DESTSERVER. The private key file (usually [homedir]\/.ssh\/id_rsa) we will keep on the SOURCESERVER.<\/p>\n<ul>\n<li>Be sure to keep this private key safe. With it anyone will be able to connect to the DESTSERVER that contains the public key.<\/li>\n<\/ul>\n<p>Now we will plant the public key we created on to the DESTSERVER. Choose the user account which you will use to connect to on DESTSERVER, we\u2019ll call this user \u2018destuser\u2019 for now.<\/p>\n<p>In that account\u2019s home directory, create a \u2018.ssh\u2019 subdirectory, and in that directory create a new text file called \u2018authorized_keys\u2019. If it already exists, great, use the existing file. Open the \u2018authorized_keys\u2019 file and paste in the contents of the public key you created in the previous step (id_rsa.pub). It should look something like the following<\/p>\n<pre><i>ssh-rsa &lt;lots and lots of characters\u2026&gt; sourceuser@SOURCESERVER<\/i><\/pre>\n<p>Save the file and change the permissions to 600 for the file and 700 for the \u2018.ssh\u2019 directory.<\/p>\n<p>Now to test that the keys are working. From the SOURCESERVER try logging in as normal using ssh to the DESTSERVER.<\/p>\n<pre><i># ssh destuser@DESTSERVER<\/i><\/pre>\n<p>If all is working you should not be prompted for a password but instead connected directly to a shell on the DESTSERVER.<\/p>\n<div><strong><span style=\"font-size: 1.285714286rem; line-height: 1.6;\">Part 2 &#8211; Creating the rsync script<\/span><\/strong><\/div>\n<p>&nbsp;<\/p>\n<p>Now for the rsync script.<\/p>\n<pre><i>#!\/bin\/sh<\/i>\r\n\r\n<i>SOURCEPATH=\u2019\/var\/svn\/workingcopies\/beta.app.jp\/\u2019<\/i>\r\n<i>DESTPATH=\u2019\/var\/www\/lamp_root\/wwwapps\/app.jp\u2019<\/i>\r\n<i>DESTUSER=\u2019updater\u2019<\/i>\r\n<i>LOGFILE=\u2019\/var\/log\/rsync_beta.log\u2019<\/i>\r\n\r\n<i>echo $\u2019\\n\\n\u2019 &gt;&gt; $LOGFILE<\/i>\r\n<i>for SERVER in web01 web02<\/i>\r\n<i>do<\/i>\r\n<i>        rsync -av \u2013rsh=ssh $SOURCEPATH $DESTUSER@$SERVER:$DESTPATH 2&gt;&amp;1 &gt;&gt; $LOGFILE<\/i>\r\n<i>        echo \u201cCompleted at: `\/bin\/date`\u201d &gt;&gt; $LOGFILE<\/i>\r\n<i>done<\/i><\/pre>\n<div><strong><span style=\"font-size: 1.285714286rem; line-height: 1.6;\">Part 3 &#8211; Setting up the cron job<\/span><\/strong><\/div>\n<div><\/div>\n<p>This is optional. I assume web updates will be done manually.<\/p>\n<p>Assuming everything has worked so far all that\u2019s left is to setup a cron job to run the script automatically at a predefined interval.<\/p>\n<p>Use the \u2018crontab\u2019 command to create a new cron job.<\/p>\n<pre><i>&gt;sudo crontab -e -u updater<\/i><\/pre>\n<p>This will open an editor where you can schedule the job.<\/p>\n<p>Enter the following to have the script run once every hour<\/p>\n<pre><i>0 * * * * \/var\/scripts\/rsync_betajp.sh<\/i><\/pre>\n<div><\/div>\n<\/div>\n<\/div>\n<div id=\"column-one\"><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Part 1 &#8211; Setting up SSH key authentication First, we need to make sure the DESTSERVER has the ability to use key authentication enabled. Find your sshd configuration file (usually \u2018\/etc\/ssh\/sshd_config\u2019) and enable the following options if they are not already set. RSAAuthentication yes PubkeyAuthentication yes AuthorizedKeysFile .ssh\/authorized_keys If you edit the file be sure [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2},"jetpack_post_was_ever_published":false},"categories":[39,16,48],"tags":[42,21,113,50,51],"class_list":["post-192","post","type-post","status-publish","format-standard","hentry","category-bash","category-linux","category-subversion","tag-bash-2","tag-linux-2","tag-rsync","tag-subversion-2","tag-svn"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p3fcrv-36","jetpack_likes_enabled":true,"_links":{"self":[{"href":"https:\/\/devopsny.com\/blog\/wp-json\/wp\/v2\/posts\/192","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devopsny.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devopsny.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devopsny.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/devopsny.com\/blog\/wp-json\/wp\/v2\/comments?post=192"}],"version-history":[{"count":3,"href":"https:\/\/devopsny.com\/blog\/wp-json\/wp\/v2\/posts\/192\/revisions"}],"predecessor-version":[{"id":194,"href":"https:\/\/devopsny.com\/blog\/wp-json\/wp\/v2\/posts\/192\/revisions\/194"}],"wp:attachment":[{"href":"https:\/\/devopsny.com\/blog\/wp-json\/wp\/v2\/media?parent=192"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devopsny.com\/blog\/wp-json\/wp\/v2\/categories?post=192"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devopsny.com\/blog\/wp-json\/wp\/v2\/tags?post=192"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}