{"id":2415,"date":"2013-07-24T11:24:33","date_gmt":"2013-07-24T03:24:33","guid":{"rendered":"http:\/\/rmohan.com\/?p=2415"},"modified":"2013-07-24T11:30:04","modified_gmt":"2013-07-24T03:30:04","slug":"backup-mysql-databases-using-amazon-s3","status":"publish","type":"post","link":"https:\/\/mohan.sg\/?p=2415","title":{"rendered":"BackUp MySQL Databases using Amazon S3"},"content":{"rendered":"<p>Backup MySQL to Amazon S3<\/p>\n<p>&nbsp;<\/p>\n<p>Yum install s3cmd -y &#8211; If you can&#8217;t install s3cmd then please follow the instructions<\/p>\n<pre>cd \/etc\/yum.repos.d<\/pre>\n<pre>touch s3cmd.repo<\/pre>\n<pre>nano s3cmd.repo<\/pre>\n<p>AND COPY THE CODE BELOW &#8211;<\/p>\n<pre># \r\n# Save this file to \/etc\/yum.repos.d on your system\r\n# and run \"yum install s3cmd\"\r\n# \r\n[s3tools]\r\nname=Tools for managing Amazon S3 - Simple Storage Service (RHEL_6)\r\ntype=rpm-md\r\nbaseurl=http:\/\/s3tools.org\/repo\/RHEL_6\/\r\ngpgcheck=1\r\ngpgkey=http:\/\/s3tools.org\/repo\/RHEL_6\/repodata\/repomd.xml.key\r\nenabled=1<\/pre>\n<p>Try Above YUM Command again &#8211; This time it will install S3CMD, Once installation is completed try running the following command &#8211; It will configure your S3CMD using your AMAZON KEY AND Access ID.<\/p>\n<pre>s3cmd --configure<\/pre>\n<pre>#!\/bin\/bash\r\nS3_BUCKET=YOUR BUCKET NAME HERE\r\nDATE=`date +%d%m%Y_%H%M`\r\nBACKUP_LOC=\/home\/admin\/user_backups\/$DATE\r\n\r\nmysql_backup(){\r\nmkdir $BACKUP_LOC\r\nmysqldump -uUSERNAME -pPASSWORD DBNAMEHERE &gt; $BACKUP_LOC\/databasename_\r\n$DATE.sql\r\n\r\n\u00a0 \u00a0s3cmd ls s3:\/\/$S3_BUCKET\/database\/$DATE &gt; \/tmp\/log.txt\r\n\u00a0 \u00a0grep -lr \"$DATE\" \/tmp\/log.txt\r\n\u00a0 if [ $? -ne 0 ]\r\n\u00a0 \u00a0 then\r\n\u00a0 \u00a0 mkdir \/tmp\/$DATE\r\n\u00a0 \u00a0 s3cmd put -r \/tmp\/$DATE s3:\/\/$S3_BUCKET\/datbase\/\r\n\r\n\u00a0 \u00a0 s3cmd sync -r $BACKUP_LOC s3:\/\/$S3_BUCKET\/database\/$DATE\/\r\n\r\n\u00a0 else\r\n\u00a0 \u00a0 s3cmd sync -r $BACKUP_LOC s3:\/\/$S3_BUCKET\/database\/$DATE\/\r\n\r\n\u00a0 fi\r\n\r\n}\r\nmysql_backup\r\nexit 0<\/pre>\n<p>This is a simple way to backup your MySQL tables to Amazon S3 for a nightly backup &#8211; this is all to be done on your server \ud83d\ude42<\/p>\n<p>Sister Document &#8211; Restore MySQL from Amazon S3 &#8211; read that next<br \/>\n1 &#8211; Install s3cmd<\/p>\n<p>this is for Centos 5.6, see http:\/\/s3tools.org\/repositories for other systems like ubuntu etc<\/p>\n<p># Install s3cmd<br \/>\ncd \/etc\/yum.repos.d\/<br \/>\nwget http:\/\/s3tools.org\/repo\/CentOS_5\/s3tools.repo<br \/>\nyum install s3cmd<br \/>\n# Setup s3cmd<br \/>\ns3cmd &#8211;configure<br \/>\n# You\u2019ll need to enter your AWS access key and secret key here, everything is optional and can be i<\/p>\n<p>2 &#8211; Add your script<\/p>\n<p>Upload a copy of s3mysqlbackup.sh (it will need some tweaks for your setup), make it executable and test it<\/p>\n<p># Add the executable bit<br \/>\nchmod +x s3mysqlbackup.sh<br \/>\n# Run the script to make sure it&#8217;s all tickety boo<br \/>\n.\/s3mysqlbackup.sh<\/p>\n<p>3 &#8211; Run it every night with CRON<\/p>\n<p>Assuming the backup script is stored in \/var\/www\/s3mysqlbackup.sh we need to add a crontask to run it automatically:<\/p>\n<p># Edit the crontab<br \/>\nenv EDITOR=nano crontab -e<br \/>\n# Add the following lines:<br \/>\n# Run the database backup script at 3am<br \/>\n0 3 * * * bash \/var\/www\/s3mysqlbackup.sh &gt;\/dev\/null 2&gt;&amp;1<\/p>\n<p>4 &#8211; Don&#8217;t expose the script!<\/p>\n<p>If for some reason you put this script in a public folder (not sure why you would do this), you should add the following to your .htaccess or httpd.conf file to prevent public access to the files:<\/p>\n<p>### Deny public access to shell files<br \/>\n&lt;Files *.sh&gt;<br \/>\nOrder allow,deny<br \/>\nDeny from all<br \/>\n&lt;\/Files&gt;<\/p>\n<p>s3mysqlbackup.sh<br \/>\nShell<br \/>\n12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152<br \/>\n#!\/bin\/bash<\/p>\n<p># Based on https:\/\/gist.github.com\/2206527<\/p>\n<p># Be pretty<br \/>\necho -e &#8221; &#8221;<br \/>\necho -e &#8221; . ____ . ______________________________&#8221;<br \/>\necho -e &#8221; |\/ \\| | |&#8221;<br \/>\necho -e &#8220;[| \\e[1;31m? ?\\e[00m |] | S3 MySQL Backup Script v.0.1 |&#8221;<br \/>\necho -e &#8221; |___==___| \/ \u00a9 oodavid 2012 |&#8221;<br \/>\necho -e &#8221; |______________________________|&#8221;<br \/>\necho -e &#8221; &#8221;<\/p>\n<p># Basic variables<br \/>\nmysqlpass=&#8221;ROOTPASSWORD&#8221;<br \/>\nbucket=&#8221;s3:\/\/bucketname&#8221;<\/p>\n<p># Timestamp (sortable AND readable)<br \/>\nstamp=`date +&#8221;%s &#8211; %A %d %B %Y @ %H%M&#8221;`<\/p>\n<p># List all the databases<br \/>\ndatabases=`mysql -u root -p$mysqlpass -e &#8220;SHOW DATABASES;&#8221; | tr -d &#8220;| &#8221; | grep -v &#8220;\\(Database\\|information_schema\\|performance_schema\\|mysql\\|test\\)&#8221;`<\/p>\n<p># Feedback<br \/>\necho -e &#8220;Dumping to \\e[1;32m$bucket\/$stamp\/\\e[00m&#8221;<\/p>\n<p># Loop the databases<br \/>\nfor db in $databases; do<\/p>\n<p># Define our filenames<br \/>\nfilename=&#8221;$stamp &#8211; $db.sql.gz&#8221;<br \/>\ntmpfile=&#8221;\/tmp\/$filename&#8221;<br \/>\nobject=&#8221;$bucket\/$stamp\/$filename&#8221;<\/p>\n<p># Feedback<br \/>\necho -e &#8220;\\e[1;34m$db\\e[00m&#8221;<\/p>\n<p># Dump and zip<br \/>\necho -e &#8221; creating \\e[0;35m$tmpfile\\e[00m&#8221;<br \/>\nmysqldump -u root -p$mysqlpass &#8211;force &#8211;opt &#8211;databases &#8220;$db&#8221; | gzip -c &gt; &#8220;$tmpfile&#8221;<\/p>\n<p># Upload<br \/>\necho -e &#8221; uploading&#8230;&#8221;<br \/>\ns3cmd put &#8220;$tmpfile&#8221; &#8220;$object&#8221;<\/p>\n<p># Delete<br \/>\nrm -f &#8220;$tmpfile&#8221;<\/p>\n<p>done;<\/p>\n<p># Jobs a goodun<br \/>\necho -e &#8220;\\e[1;32mJobs a goodun\\e[00m&#8221;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<div id=\"file-sql_backup-sh-LC1\">!\/bin\/bash<\/div>\n<div id=\"file-sql_backup-sh-LC2\"># Shell script to backup MySql database<\/div>\n<div id=\"file-sql_backup-sh-LC3\"><\/div>\n<div id=\"file-sql_backup-sh-LC4\"># CONFIG &#8211; Only edit the below lines to setup the script<\/div>\n<div id=\"file-sql_backup-sh-LC5\"># ===============================<\/div>\n<div id=\"file-sql_backup-sh-LC6\"><\/div>\n<div id=\"file-sql_backup-sh-LC7\">MyUSER=&#8221;root&#8221; # USERNAME<\/div>\n<div id=\"file-sql_backup-sh-LC8\">MyPASS=&#8221;password&#8221; # PASSWORD<\/div>\n<div id=\"file-sql_backup-sh-LC9\">MyHOST=&#8221;localhost&#8221; # Hostname<\/div>\n<div id=\"file-sql_backup-sh-LC10\"><\/div>\n<div id=\"file-sql_backup-sh-LC11\">S3Bucket=&#8221;mysql-backup&#8221; # S3 Bucket<\/div>\n<div id=\"file-sql_backup-sh-LC12\"><\/div>\n<div id=\"file-sql_backup-sh-LC13\"># DO NOT BACKUP these databases<\/div>\n<div id=\"file-sql_backup-sh-LC14\">IGNORE=&#8221;test&#8221;<\/div>\n<div id=\"file-sql_backup-sh-LC15\"><\/div>\n<div id=\"file-sql_backup-sh-LC16\"># DO NOT EDIT BELOW THIS LINE UNLESS YOU KNOW WHAT YOU ARE DOING<\/div>\n<div id=\"file-sql_backup-sh-LC17\"># ===============================<\/div>\n<div id=\"file-sql_backup-sh-LC18\"><\/div>\n<div id=\"file-sql_backup-sh-LC19\"># Linux bin paths, change this if it can not be autodetected via which command<\/div>\n<div id=\"file-sql_backup-sh-LC20\">MYSQL=&#8221;$(which mysql)&#8221;<\/div>\n<div id=\"file-sql_backup-sh-LC21\">MYSQLDUMP=&#8221;$(which mysqldump)&#8221;<\/div>\n<div id=\"file-sql_backup-sh-LC22\">CHOWN=&#8221;$(which chown)&#8221;<\/div>\n<div id=\"file-sql_backup-sh-LC23\">CHMOD=&#8221;$(which chmod)&#8221;<\/div>\n<div id=\"file-sql_backup-sh-LC24\">GZIP=&#8221;$(which gzip)&#8221;<\/div>\n<div id=\"file-sql_backup-sh-LC25\"><\/div>\n<div id=\"file-sql_backup-sh-LC26\"># Backup Dest directory, change this if you have someother location<\/div>\n<div id=\"file-sql_backup-sh-LC27\">DEST=&#8221;\/backup&#8221;<\/div>\n<div id=\"file-sql_backup-sh-LC28\"><\/div>\n<div id=\"file-sql_backup-sh-LC29\"># Main directory where backup will be stored<\/div>\n<div id=\"file-sql_backup-sh-LC30\">MBD=&#8221;$DEST\/mysql-$(date +&#8221;%d-%m-%Y&#8221;)&#8221;<\/div>\n<div id=\"file-sql_backup-sh-LC31\"><\/div>\n<div id=\"file-sql_backup-sh-LC32\"># Get hostname<\/div>\n<div id=\"file-sql_backup-sh-LC33\">HOST=&#8221;$(hostname)&#8221;<\/div>\n<div id=\"file-sql_backup-sh-LC34\"><\/div>\n<div id=\"file-sql_backup-sh-LC35\"># Get data in dd-mm-yyyy format<\/div>\n<div id=\"file-sql_backup-sh-LC36\">NOW=&#8221;$(date +&#8221;%d-%m-%Y&#8221;)&#8221;<\/div>\n<div id=\"file-sql_backup-sh-LC37\"><\/div>\n<div id=\"file-sql_backup-sh-LC38\"># File to store current backup file<\/div>\n<div id=\"file-sql_backup-sh-LC39\">FILE=&#8221;&#8221;<\/div>\n<div id=\"file-sql_backup-sh-LC40\"><\/div>\n<div id=\"file-sql_backup-sh-LC41\"># Store list of databases<\/div>\n<div id=\"file-sql_backup-sh-LC42\">DBS=&#8221;&#8221;<\/div>\n<div id=\"file-sql_backup-sh-LC43\"><\/div>\n<div id=\"file-sql_backup-sh-LC44\">[ ! -d $MBD ] &amp;&amp; mkdir -p $MBD || :<\/div>\n<div id=\"file-sql_backup-sh-LC45\"><\/div>\n<div id=\"file-sql_backup-sh-LC46\"># Only root can access it!<\/div>\n<div id=\"file-sql_backup-sh-LC47\">$CHOWN 0.0 -R $DEST<\/div>\n<div id=\"file-sql_backup-sh-LC48\">$CHMOD 0600 $DEST<\/div>\n<div id=\"file-sql_backup-sh-LC49\"><\/div>\n<div id=\"file-sql_backup-sh-LC50\"># Get all database list first<\/div>\n<div id=\"file-sql_backup-sh-LC51\"><\/div>\n<div id=\"file-sql_backup-sh-LC52\">if [ &#8220;$MyPASS&#8221; == &#8220;&#8221; ];<\/div>\n<div id=\"file-sql_backup-sh-LC53\">then<\/div>\n<div id=\"file-sql_backup-sh-LC54\">DBS=&#8221;$($MYSQL -u $MyUSER -h $MyHOST -Bse &#8216;show databases&#8217;)&#8221;<\/div>\n<div id=\"file-sql_backup-sh-LC55\">else<\/div>\n<div id=\"file-sql_backup-sh-LC56\">DBS=&#8221;$($MYSQL -u $MyUSER -h $MyHOST -p$MyPASS -Bse &#8216;show databases&#8217;)&#8221;<\/div>\n<div id=\"file-sql_backup-sh-LC57\">fi<\/div>\n<div id=\"file-sql_backup-sh-LC58\"><\/div>\n<div id=\"file-sql_backup-sh-LC59\">for db in $DBS<\/div>\n<div id=\"file-sql_backup-sh-LC60\">do<\/div>\n<div id=\"file-sql_backup-sh-LC61\">skipdb=-1<\/div>\n<div id=\"file-sql_backup-sh-LC62\">if [ &#8220;$IGNORE&#8221; != &#8220;&#8221; ];<\/div>\n<div id=\"file-sql_backup-sh-LC63\">then<\/div>\n<div id=\"file-sql_backup-sh-LC64\">for i in $IGNORE<\/div>\n<div id=\"file-sql_backup-sh-LC65\">do<\/div>\n<div id=\"file-sql_backup-sh-LC66\">[ &#8220;$db&#8221; == &#8220;$i&#8221; ] &amp;&amp; skipdb=1 || :<\/div>\n<div id=\"file-sql_backup-sh-LC67\">done<\/div>\n<div id=\"file-sql_backup-sh-LC68\">fi<\/div>\n<div id=\"file-sql_backup-sh-LC69\"><\/div>\n<div id=\"file-sql_backup-sh-LC70\">if [ &#8220;$skipdb&#8221; == &#8220;-1&#8221; ] ; then<\/div>\n<div id=\"file-sql_backup-sh-LC71\">FILE=&#8221;$MBD\/$db.$HOST.$NOW.gz&#8221;<\/div>\n<div id=\"file-sql_backup-sh-LC72\"># dump database to file and gzip<\/div>\n<div id=\"file-sql_backup-sh-LC73\">if [ &#8220;$MyPASS&#8221; == &#8220;&#8221; ]; then<\/div>\n<div id=\"file-sql_backup-sh-LC74\">$MYSQLDUMP -u $MyUSER -h $MyHOST $db | $GZIP -9 &gt; $FILE<\/div>\n<div id=\"file-sql_backup-sh-LC75\">else<\/div>\n<div id=\"file-sql_backup-sh-LC76\">$MYSQLDUMP -u $MyUSER -h $MyHOST -p$MyPASS $db | $GZIP -9 &gt; $FILE<\/div>\n<div id=\"file-sql_backup-sh-LC77\">fi<\/div>\n<div id=\"file-sql_backup-sh-LC78\">fi<\/div>\n<div id=\"file-sql_backup-sh-LC79\">done<\/div>\n<div id=\"file-sql_backup-sh-LC80\"><\/div>\n<div id=\"file-sql_backup-sh-LC81\"># copy mysql backup directory to S3<\/div>\n<div id=\"file-sql_backup-sh-LC82\">s3cmd sync -rv &#8211;skip-existing $MBD s3:\/\/$S3Bucket\/<\/div>\n<div><\/div>\n<div><\/div>\n<div>\n<p>u can use the above script in a cron too, so your server is backed up\u00a0regularly. The below cronjob will run the MySQL database backup script everyday at 2am:<\/p>\n<div id=\"gist5419860\">\n<div>\n<div>\n<div>\n<table cellspacing=\"0\" cellpadding=\"0\">\n<tbody>\n<tr>\n<td>12<\/td>\n<td>\n<div id=\"file-mysql_backup_cron-sh-LC1\"># Run everday at 2am<\/div>\n<div id=\"file-mysql_backup_cron-sh-LC2\">0 2 * * * \/path\/to\/sql_backup.sh<\/div>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Backup MySQL to Amazon S3<\/p>\n<p>&nbsp;<\/p>\n<p>Yum install s3cmd -y &#8211; If you can&#8217;t install s3cmd then please follow the instructions<\/p>\n<p> cd \/etc\/yum.repos.d touch s3cmd.repo nano s3cmd.repo <\/p>\n<p>AND COPY THE CODE BELOW &#8211;<\/p>\n<p> # # Save this file to \/etc\/yum.repos.d on your system # and run &#8220;yum install s3cmd&#8221; # [s3tools] name=Tools for managing Amazon [&#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\/2415"}],"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=2415"}],"version-history":[{"count":5,"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts\/2415\/revisions"}],"predecessor-version":[{"id":2418,"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts\/2415\/revisions\/2418"}],"wp:attachment":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2415"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2415"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2415"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}