{"id":690,"date":"2019-01-29T18:38:10","date_gmt":"2019-01-29T15:38:10","guid":{"rendered":"https:\/\/artem.services\/?p=564"},"modified":"2019-02-21T15:34:20","modified_gmt":"2019-02-21T12:34:20","slug":"backup-sites-and-mysql-dbs-to-aws-s3-2","status":"publish","type":"post","link":"https:\/\/artem.services\/?p=690&lang=en","title":{"rendered":"BASH &#8212; backup sites and MySQL databases on AWS S3"},"content":{"rendered":"<p>The script finds all folders in a given directory and archives them one by one, copies them to <strong>S3 Bucket<\/strong> and deletes them locally. It also receives a list of all <strong>MySQL<\/strong> databases, excluding system databases, archives them in turn, sends them to <strong>S3 Bucket<\/strong> and deletes them locally.<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\n#!\/bin\/bash\r\n\r\nDATE=$(date +%d-%m-%Y)\r\nWEB_PATH=&quot;\/var\/www\/html\/&quot;\r\n\r\nSITE_LIST=&quot;$(ls $WEB_PATH)&quot;\r\nDB_LIST=&quot;$(mysql -u root -e &quot;SHOW DATABASES;&quot; | grep -Ev &quot;(Database|information_schema|performance_schema|mysql|sys)&quot;)&quot;\r\nS3_BUCKET=&quot;s3:\/\/artem-services&quot;\r\n\r\n################################ BACKUP SITES ################################\r\n\r\nfor SITE in $SITE_LIST\r\ndo\r\n\tif [ -f &quot;\/tmp\/$SITE.tar.gz&quot; ] # Check if there is an old archive left\r\n\tthen\r\n\t\trm \/tmp\/$SITE.tar.gz\r\n\tfi\r\n\r\n\ttar -zcvf \/tmp\/$SITE.tar.gz --directory=&quot;$WEB_PATH\/$SITE\/&quot; .\/\r\n\taws s3 cp \/tmp\/$SITE.tar.gz $S3_BUCKET\/$DATE\/SITE\/$SITE.tar.gz\r\n\trm \/tmp\/$SITE.tar.gz\r\ndone\r\n\r\n############################### BACKUP DATABASES ##############################\r\n\r\nfor DB in $DB_LIST\r\ndo\r\n\tif [ -f &quot;\/tmp\/$DB.gz&quot; ] # Check if there is an old archive left\r\n\tthen\r\n\t\trm \/tmp\/$DB.gz\r\n\tfi\r\n\r\n\tmysqldump -u root $DB | gzip -c &gt; \/tmp\/$DB.gz\r\n\taws s3 cp \/tmp\/$DB.gz $S3_BUCKET\/$DATE\/DATABASE\/$DB.gz\r\n\trm \/tmp\/$DB.gz\r\ndone\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>The script finds all folders in a given directory and archives them one by one, copies them to S3 Bucket and deletes them locally. It also receives a list of all MySQL databases, excluding system databases, archives them in turn, sends them to S3 Bucket and deletes them locally.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[477],"tags":[479,427,481,483,485],"_links":{"self":[{"href":"https:\/\/artem.services\/index.php?rest_route=\/wp\/v2\/posts\/690"}],"collection":[{"href":"https:\/\/artem.services\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/artem.services\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/artem.services\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/artem.services\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=690"}],"version-history":[{"count":8,"href":"https:\/\/artem.services\/index.php?rest_route=\/wp\/v2\/posts\/690\/revisions"}],"predecessor-version":[{"id":721,"href":"https:\/\/artem.services\/index.php?rest_route=\/wp\/v2\/posts\/690\/revisions\/721"}],"wp:attachment":[{"href":"https:\/\/artem.services\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=690"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/artem.services\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=690"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/artem.services\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=690"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}