{"id":6090,"date":"2021-01-30T15:41:46","date_gmt":"2021-01-30T10:11:46","guid":{"rendered":"https:\/\/pheonixsolutions.com\/blog\/?p=6090"},"modified":"2021-02-01T17:51:55","modified_gmt":"2021-02-01T12:21:55","slug":"automate-mysqldump-backup-from-serverbackend-to-amazon-s3","status":"publish","type":"post","link":"https:\/\/pheonixsolutions.com\/blog\/automate-mysqldump-backup-from-serverbackend-to-amazon-s3\/","title":{"rendered":"Automate MySQLdump Backup from server(backend) to Amazon S3"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Please follow the below steps to setup backup of mysql database from server to Amazon s3.<\/p>\n\n\n\n<ol class=\"wp-block-list\"><li>Login to AWS console, create IAM user, download the credentials and attach AmazonS3Full access policy to the IAM user.<\/li><li>Install AWS CLI and configure by following the below commands.<br>  <span class=\"has-inline-color has-vivid-cyan-blue-color\">apt install awscli -y<br>  aws configure<\/span><\/li><li>It will ask for Access Id and secret key. Provide the same which we downloaded in step-1  and proceed.<\/li><li>Verify AWS S3 list by following the below command.<br> <span class=\"has-inline-color has-vivid-cyan-blue-color\">aws s3 ls<\/span><\/li><li>Install S3fs and Mount S3 Bucket by following the below commands.<br><span class=\"has-inline-color has-vivid-cyan-blue-color\">apt install s3fs<\/span><br><span class=\"has-inline-color has-vivid-cyan-blue-color\">echo ACCESS_KEY_ID:SECRET_ACCESS_KEY > \/home\/ubuntu\/.s3fs-      creds<\/span><\/li><li>Your file will be saved at <span class=\"has-inline-color has-vivid-cyan-blue-color\">\/home\/ubuntu\/.s3fs-creds.<\/span><\/li><li><span class=\"has-inline-color has-vivid-cyan-blue-color\">chmod 600 \/home\/ubuntu\/.s3fs-creds<\/span><\/li><li>Create a new folder from your home directory.<br><span class=\"has-inline-color has-vivid-cyan-blue-color\">mkdir \/home\/ubuntu\/s3_uploads<\/span><\/li><li>Create an Automation Backup Script for MySQLdump<br><span class=\"has-inline-color has-vivid-cyan-blue-color\">vim \/home\/ubuntu\/.config.cnf<\/span><\/li><li>Add the following below:<br><span class=\"has-inline-color has-vivid-cyan-blue-color\">[client]<br>user = dbuser<br>password = dbpassword<br>host = localhost<\/span><br>Save and close the file.<\/li><li>create an automation backup script for MySQLdump<br><span class=\"has-inline-color has-vivid-cyan-blue-color\">vim \/home\/ubuntu\/mysqldump.sh<\/span><\/li><li>Add the following script<br><span class=\"has-inline-color has-vivid-cyan-blue-color\">!\/bin\/bash<br>DBNAME=yourdbname<br>BACKUP=\/home\/ubuntu\/s3_uploads<br>Backup date<br>date=$(date +&#8217;%Y%m%d%H%M%S&#8217;);<br>Start exec backup..<br>mysqldump &#8211;defaults-extra-file=\/home\/ubuntu\/.config.cnf $DBNAME > $BACKUP\/mysqldump_$date.sql<\/span><\/li><li>Save and close the file.<\/li><li>To make sure the file is executable, run the below command.<br><span class=\"has-inline-color has-vivid-cyan-blue-color\">chmod +x \/home\/ubuntu\/mysqldump.sh<\/span><\/li><li>To automate your backup, use to command below to assign the mysql dump script on cron scheduling jobs.<br><span class=\"has-inline-color has-vivid-cyan-blue-color\">crontab -e<\/span><\/li><li>If your first time to open the crontab, you can choose the vim editor on the screen and then add the following command to the bottom line.<br><span class=\"has-inline-color has-vivid-cyan-blue-color\">@daily sh \/home\/ubuntu\/mysqldump.sh<\/span><\/li><li>Save and close the file.<\/li><li>Testing MySQLdump to S3 Bucket<\/li><li>Go to the directory where mysqldump.sh is located and run the script.<br><span class=\"has-inline-color has-vivid-cyan-blue-color\">.\/mysqldump.sh<\/span><\/li><li>After the backups completes, open the mounted <code>s3_uploads<\/code> folder and type the below command.<br><span class=\"has-inline-color has-vivid-cyan-blue-color\">ls -lah<\/span><\/li><li>On the screen, you can see the files which were uploaded.<\/li><li>Verify the files on S3 Bucket Console.<br><\/li><\/ol>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Please follow the below steps to setup backup of mysql database from server to Amazon s3. Login to AWS console, [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"default","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","ast-disable-related-posts":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"default","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"_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":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2},"jetpack_post_was_ever_published":false},"categories":[1019],"tags":[300,813,812,324],"class_list":["post-6090","post","type-post","status-publish","format-standard","hentry","category-cloud-aws","tag-aws","tag-backup-of-mysql-databse-to-s3","tag-mysql-dump-to-s3","tag-s3","psol-cat-cloud-aws"],"jetpack_publicize_connections":[],"jetpack_shortlink":"https:\/\/wp.me\/phn2x7-1Ae","jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/posts\/6090","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/comments?post=6090"}],"version-history":[{"count":0,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/posts\/6090\/revisions"}],"wp:attachment":[{"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/media?parent=6090"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/categories?post=6090"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/tags?post=6090"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}