{"id":367,"date":"2009-06-04T17:26:35","date_gmt":"2009-06-04T10:26:35","guid":{"rendered":"http:\/\/adityo.blog.binusian.org\/?p=367"},"modified":"2009-07-15T19:05:52","modified_gmt":"2009-07-15T12:05:52","slug":"mysql-and-file-incremental-backup-script-with-remote-ftp-script","status":"publish","type":"post","link":"https:\/\/adityo.blog.binusian.org\/?p=367","title":{"rendered":"Mysql and file incremental Backup With Remote FTP Script"},"content":{"rendered":"<p>Here is my script to backup a certain MySql db and certain directory files<\/p>\n<p>FYI: this script only can working on Unix\/Linux environtment<\/p>\n<p>Example, We will backup one mysql db &amp; directory files under \/var\/www and transfer it to remote ftp to another server and save it on incremental folder by date<\/p>\n<p>db : test<\/p>\n<p>user : root<\/p>\n<p>pass : passwordrootmysql<\/p>\n<p>Remote Ftp Server (Vsftpd):<\/p>\n<p>ip : 10.10.1.3<\/p>\n<p>user : usertest<\/p>\n<p>pass : usertestpassword<\/p>\n<h2>Here is my script<\/h2>\n<p>#!\/bin\/bash<\/p>\n<p># Shell script to backup MySql database<\/p>\n<p># By Adityo<\/p>\n<p>MyUSER=&#8221;root&#8221; \u00a0 \u00a0 # USERNAME<\/p>\n<p>MyPASS=&#8221;passwordrootmysql&#8221; \u00a0 \u00a0 \u00a0 # PASSWORD<\/p>\n<p>MyHOST=&#8221;localhost&#8221; \u00a0 \u00a0 \u00a0 \u00a0 \u00a0# Hostname<\/p>\n<p># Linux bin paths, change this if it can&#8217;t be autodetected via which command<\/p>\n<p>MYSQL=&#8221;$(which mysql)&#8221;<\/p>\n<p>MYSQLDUMP=&#8221;$(which mysqldump)&#8221;<\/p>\n<p>CHOWN=&#8221;$(which chown)&#8221;<\/p>\n<p>CHMOD=&#8221;$(which chmod)&#8221;<\/p>\n<p>GZIP=&#8221;$(which gzip)&#8221;<\/p>\n<p># Get data in dd-mm-yyyy format<\/p>\n<p>NOW=&#8221;$(date +&#8221;%d-%m-%Y&#8221;)&#8221;<\/p>\n<p># Backup Dest directory, change this if you have someother location<\/p>\n<p>DEST=&#8221;\/backup\/mysqlscript&#8221;<\/p>\n<p># Main directory inside the backup Dest directory (DEST) where backup will be stored<\/p>\n<p>MBD=&#8221;$DEST\/mysql\/$NOW&#8221;<\/p>\n<p># Get hostname<\/p>\n<p>HOST=&#8221;$(hostname)&#8221;<\/p>\n<p># File to store current backup file<\/p>\n<p>FILE=&#8221;&#8221;<\/p>\n<p># Store list of databases<\/p>\n<p>DBS=&#8221;&#8221;<\/p>\n<p># DO NOT BACKUP these databases , you can choose another db that you did not want to backup and split it with space in this example i exclude the mysql , information_schema and backup database an di only backup the test db<\/p>\n<p>IGGY=&#8221;mysql information_schema backup&#8221;<\/p>\n<p>[ ! -d $MBD ] &amp;&amp; mkdir -p $MBD || :<\/p>\n<p># Only root can access it!<\/p>\n<p>$CHOWN 0.0 -R $DEST<\/p>\n<p>$CHMOD 0600 $DEST<\/p>\n<p># Get all database list first<\/p>\n<p>DBS=&#8221;$($MYSQL -u $MyUSER -h $MyHOST -p$MyPASS -Bse &#8216;show databases&#8217;)&#8221;<\/p>\n<p>for db in $DBS<\/p>\n<p>do<\/p>\n<p>skipdb=-1<\/p>\n<p>if [ &#8220;$IGGY&#8221; != &#8220;&#8221; ];<\/p>\n<p>then<\/p>\n<p>for i in $IGGY<\/p>\n<p>do<\/p>\n<p>[ &#8220;$db&#8221; == &#8220;$i&#8221; ] &amp;&amp; skipdb=1 || :<\/p>\n<p>done<\/p>\n<p>fi<\/p>\n<p>if [ &#8220;$skipdb&#8221; == &#8220;-1&#8243; ] ; then<\/p>\n<p>FILE=&#8221;$MBD\/$db.$HOST.$NOW.sql.gz&#8221;<\/p>\n<p># do all inone job in pipe,<\/p>\n<p># connect to mysql using mysqldump for select mysql database<\/p>\n<p># and pipe it out to gz file in backup dir \ud83d\ude42<\/p>\n<p>$MYSQLDUMP -u $MyUSER -h $MyHOST -p$MyPASS $db &#8211;lock-tables=false \u00a0| $GZIP -9 &gt; $FILE<\/p>\n<p>fi<\/p>\n<p>done<\/p>\n<p>### Start backup files on didirectory \/var\/www<\/p>\n<p>mkdir -p \/backup\/mysqlscript\/data\/$NOW<\/p>\n<p>cd \/backup\/mysqlscript\/data\/$NOW<\/p>\n<p>FILE=&#8221;blogbinusian-file-www-$NOW.tar.gz&#8221;<\/p>\n<p>tar -cvf $FILE \/var\/www<\/p>\n<p>### FTP server Setup ###<\/p>\n<p>FTPD=&#8221;\/\/incremental&#8221;<\/p>\n<p>FTPU=&#8221;usertest&#8221;<\/p>\n<p>FTPP=&#8221;usertestpassword&#8221;<\/p>\n<p>FTPS=&#8221;10.10.1.3&#8243;<\/p>\n<p>NCFTP=&#8221;$(which ncftpput)&#8221;<\/p>\n<p>### Dump backup using FTP ###<\/p>\n<p>#Start FTP backup using ncftp and transfer file to remote server<\/p>\n<p>ncftp -u&#8221;$FTPU&#8221; -p&#8221;$FTPP&#8221; $FTPS&lt;&lt;EOF # connect to target ftp service<\/p>\n<p>mkdir $FTPD\/$NOW\/ \u00a0 \u00a0 \u00a0# it will create directory \/home\/usertest\/incremental\/$NOW<\/p>\n<p>cd \u00a0 \u00a0$FTPD\/$NOW\/ \u00a0 \u00a0 \u00a0 \u00a0#\u00a0\u00a0it will changes directory \/home\/usertest\/incremental\/$NOW<\/p>\n<p>lcd \/backup\/mysqlscript\/mysql\/$NOW \u00a0# it will goes to source server directory\u00a0\/backup\/mysqlscript\/mysql\/$NOW<\/p>\n<p>mput * \u00a0# upload all files on\u00a0\/backup\/mysqlscript\/mysql\/$NOW<\/p>\n<p>lcd \/backup\/mysqlscript\/data\/$NOW\u00a0# it will goes to source server directory\u00a0\/backup\/mysqlscript\/data\/$NOW<\/p>\n<p>mput *\u00a0\u00a0# upload all files on\u00a0\/backup\/mysqlscript\/mysql\/$NOW<\/p>\n<p>quit # stop<\/p>\n<p>EOF<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Here is my script to backup a certain MySql db and certain directory files FYI: this script only can working on Unix\/Linux environtment Example, We will backup one mysql db &amp; directory files under \/var\/www and transfer it to remote ftp to another server and save it on incremental folder by date db : test [&hellip;]<\/p>\n","protected":false},"author":386,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[491],"tags":[],"class_list":["post-367","post","type-post","status-publish","format-standard","hentry","category-mysql"],"_links":{"self":[{"href":"https:\/\/adityo.blog.binusian.org\/index.php?rest_route=\/wp\/v2\/posts\/367","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/adityo.blog.binusian.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/adityo.blog.binusian.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/adityo.blog.binusian.org\/index.php?rest_route=\/wp\/v2\/users\/386"}],"replies":[{"embeddable":true,"href":"https:\/\/adityo.blog.binusian.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=367"}],"version-history":[{"count":9,"href":"https:\/\/adityo.blog.binusian.org\/index.php?rest_route=\/wp\/v2\/posts\/367\/revisions"}],"predecessor-version":[{"id":519,"href":"https:\/\/adityo.blog.binusian.org\/index.php?rest_route=\/wp\/v2\/posts\/367\/revisions\/519"}],"wp:attachment":[{"href":"https:\/\/adityo.blog.binusian.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=367"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/adityo.blog.binusian.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=367"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/adityo.blog.binusian.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=367"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}