{"id":7847,"date":"2019-06-19T08:29:17","date_gmt":"2019-06-19T00:29:17","guid":{"rendered":"http:\/\/rmohan.com\/?p=7847"},"modified":"2019-06-19T08:29:19","modified_gmt":"2019-06-19T00:29:19","slug":"mysqldump-gzip-aws","status":"publish","type":"post","link":"https:\/\/mohan.sg\/?p=7847","title":{"rendered":"mysqldump + gzip + aws"},"content":{"rendered":"\n<p>A shell script to backup MYSQL database and upload it to Amazon S3.<\/p>\n\n\n\n<p>Note<br>\nMake sure the AWS CLI is installed properly<\/p>\n\n\n\n<ol><li>mysqldump + gzip + aws<br>\nDump the database with mysqldump and gzip it into a folder, later uses the aws command to upload the file to Amazon S3<\/li><\/ol>\n\n\n\n<p>backup-script.sh<\/p>\n\n\n\n<h1 class=\"wp-block-heading\">!\/bin\/bash<\/h1>\n\n\n\n<h6 class=\"wp-block-heading\">#<\/h6>\n\n\n\n<h1 class=\"wp-block-heading\">#<\/h1>\n\n\n\n<h2 class=\"wp-block-heading\">MySQL Database To Amazon S3<\/h2>\n\n\n\n<h6 class=\"wp-block-heading\">#<\/h6>\n\n\n\n<p>NOW=$(date +&#8221;%Y-%m-%d&#8221;)<\/p>\n\n\n\n<p>BACKUP_DIR=&#8221;\/home\/mohan\/backup&#8221;<br>\nMYSQL_HOST=&#8221;localhost&#8221;<br>\nMYSQL_PORT=&#8221;3306&#8243;<br>\nMYSQL_USER=&#8221;YOUR_DB_USER&#8221;<br>\nMYSQL_PASSWORD=&#8221;YOUR_DB_PASSWORD&#8221;<br>\nDATABASE_NAME=&#8221;YOUR_DB_NAME&#8221;<\/p>\n\n\n\n<p>AMAZON_S3_BUCKET=&#8221;s3:\/\/mohan\/backup\/mysql\/&#8221;<br>\nAMAZON_S3_BIN=&#8221;\/home\/mohan\/.local\/bin\/aws&#8221;<\/p>\n\n\n\n<p>FOLDERS_TO_BACKUP=(&#8220;\/home\/mohan\/bk1&#8221; &#8220;\/home\/mohan\/bk2&#8221;)<\/p>\n\n\n\n<h6 class=\"wp-block-heading\">#<\/h6>\n\n\n\n<p>mkdir -p ${BACKUP_DIR}<\/p>\n\n\n\n<p>backup_mysql(){<br>\n         mysqldump -h ${MYSQL_HOST} \\<br>\n           -P ${MYSQL_PORT} \\<br>\n           -u ${MYSQL_USER} \\<br>\n           -p${MYSQL_PASSWORD} ${DATABASE_NAME} | gzip &gt; ${BACKUP_DIR}\/${DATABASE_NAME}-${NOW}.sql.gz<br>\n}<\/p>\n\n\n\n<h1 class=\"wp-block-heading\">backup any folders?<\/h1>\n\n\n\n<p>backup_files(){<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>    tar -cvzf ${BACKUP_DIR}\/backup-files-${NOW}.tar.gz ${FOLDERS_TO_BACKUP[@]}<\/code><\/pre>\n\n\n\n<p>}<\/p>\n\n\n\n<p>upload_s3(){<br>\n        ${AMAZON_S3_BIN} s3 cp ${BACKUP_DIR}\/${DATABASE_NAME}-${NOW}.sql.gz ${AMAZON_S3_BUCKET}<br>\n}<\/p>\n\n\n\n<p>backup_mysql<br>\nupload_s3<br>\nCopy<\/p>\n\n\n\n<ol><li>How to run?<br>\nAssign execute permission to the shell script, and run it directly.<\/li><\/ol>\n\n\n\n<p>Terminal<br>\n$ chmod +x backup-script.sh<\/p>\n\n\n\n<h1 class=\"wp-block-heading\">run it<\/h1>\n\n\n\n<p>$ .\/backup-script.sh<br>\nCopy<\/p>\n\n\n\n<ol><li>Run it daily<br>\n3.1 cron schedule to run the script daily.<\/li><\/ol>\n\n\n\n<p>Terminal<br>\n$ crontab -e<\/p>\n\n\n\n<h1 class=\"wp-block-heading\">Daily, 7pm<\/h1>\n\n\n\n<p>0 19 * * * \/path.to\/backup-script.sh &gt; \/dev\/null 2&gt;&amp;1<\/p>\n","protected":false},"excerpt":{"rendered":"\n<p>A shell script to backup MYSQL database and upload it to Amazon S3.<\/p>\n<p>Note Make sure the AWS CLI is installed properly<\/p>\n<p> mysqldump + gzip + aws Dump the database with mysqldump and gzip it into a folder, later uses the aws command to upload the file to Amazon S3 <\/p>\n<p>backup-script.sh<\/p>\n<p> !\/bin\/bash # # [&#8230;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[49],"tags":[],"_links":{"self":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts\/7847"}],"collection":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=7847"}],"version-history":[{"count":1,"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts\/7847\/revisions"}],"predecessor-version":[{"id":7848,"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts\/7847\/revisions\/7848"}],"wp:attachment":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=7847"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=7847"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=7847"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}