{"id":1418,"date":"2013-10-06T12:30:30","date_gmt":"2013-10-06T10:30:30","guid":{"rendered":"http:\/\/www.extradrm.com\/?p=1418"},"modified":"2013-10-06T15:57:15","modified_gmt":"2013-10-06T13:57:15","slug":"ubuntu-linux-backup-mysql-server-shell-script","status":"publish","type":"post","link":"http:\/\/www.extradrm.com\/?p=1418","title":{"rendered":"Ubuntu Linux Backup MySQL Server Shell Script"},"content":{"rendered":"<p>You can use mysqldump command to backup database. The mysqldump client is a backup program. It can be used to dump a database or a collection of databases for backup or for transferring the data to another SQL server. The dump contains SQL statements to create the table or populate it, or both.<\/p>\n<p>Once database is dumped, you need to upload the same to ftp server. Use lftp client to upload all files.<\/p>\n<h2>Install lftp<\/h2>\n<p>lftp is a file transfer program that allows sophisticated ftp, http and other connections to other hosts. If site is specified then lftp will connect to that site otherwise a connection has to be established with the open command. To install lftp, enter:<\/p>\n<pre>sudo apt-get install lftp<\/pre>\n<h2>Shell script to backup MySQL database server<\/h2>\n<p>Following is the shell script. It will dump all database to \/backup\/mysql and later it will upload to FTP server. You need to setup correct username and password before using the script:<\/p>\n<pre>#!\/bin\/bash\r\n#\r\n## go to folder where to backup\r\n#\r\ncd \/root\/scripts\/backups\/\r\n\r\nfor i in mon_site mon_forum mon_blog; do\r\n\r\n## Save mysql databases in .sql files\r\nmysqldump -uroot -pmonpass $i &gt; ${i}_`date +\"%Y-%m-%d\"`.sql\r\n\r\n## Compression with tar.bz2 (best rate compression)\r\ntar jcf ${i}_`date +\"%Y-%m-%d\"`.sql.tar.bz2 ${i}_`date +\"%Y-%m-%d\"`.sql\r\n\r\n## delete unzipped exports\r\nrm ${i}_`date +\"%Y-%m-%d\"`.sql\r\ndone\r\nlftp -u $FTPU,$FTPP -e \"mkdir \/mysql\/;cd \/mysql\/; mput \/root\/scripts\/backups\/*; quit\" $FTPS<\/pre>\n<p>Save script as \/home\/your-name\/backup_bdd.sh file. Setup executable permission:<\/p>\n<pre>$ chmod +x \/home\/your-name\/backup_bdd.sh<\/pre>\n<p>To backup MySQL, enter:<\/p>\n<pre>\/home\/your-name\/backup_bdd.sh<\/pre>\n<p>OR<\/p>\n<pre>sudo \/home\/your-name\/backup_bdd.sh<\/pre>\n<h3>Clean older files :<\/h3>\n<p>To clean old sql files we create a bash\/script file clean_backups.sh :<\/p>\n<pre>#!\/bin\/bash\r\n#\r\n## Delete Files older than 5 days\r\n#\r\nfind \/root\/scripts\/backups\/ -type f -mtime +4 -delete<\/pre>\n<h2>Run MySQL backup script as cron job<\/h2>\n<p><a href=\"http:\/\/www.extradrm.com\/wp-content\/uploads\/2013\/10\/cronjob.jpg\"><img loading=\"lazy\" class=\"alignnone size-full wp-image-1432\" alt=\"cronjob\" src=\"http:\/\/www.extradrm.com\/wp-content\/uploads\/2013\/10\/cronjob.jpg\" width=\"392\" height=\"214\" srcset=\"http:\/\/www.extradrm.com\/wp-content\/uploads\/2013\/10\/cronjob.jpg 392w, http:\/\/www.extradrm.com\/wp-content\/uploads\/2013\/10\/cronjob-300x163.jpg 300w\" sizes=\"(max-width: 392px) 100vw, 392px\" \/><\/a><\/p>\n<p><a href=\"http:\/\/www.extradrm.com\/wp-content\/uploads\/2013\/10\/cronjob01.jpg\"><img loading=\"lazy\" alt=\"cronjob01\" src=\"http:\/\/www.extradrm.com\/wp-content\/uploads\/2013\/10\/cronjob01.jpg\" width=\"434\" height=\"241\" \/><\/a><\/p>\n<p>To automate procedure setup a cron job. For example run backup everyday at midnight (i.e once a day), enter:<\/p>\n<pre>$ sudo crontab -e<\/pre>\n<p>Append following cron job (will open with nano):<\/p>\n<pre>@midnight \/home\/you\/backup_bdd.sh &gt;\/dev\/null 2&gt;&amp;1<\/pre>\n<pre><code># run five minutes after midnight, every day\r\n   5 0 * * *       <\/code>\/home\/you\/clean_backups.sh &gt;\/dev\/null 2&gt;&amp;1<\/pre>\n<p>Save and close the file. Please note that above script should work with other Linux distros or UNIX like oses.<\/p>\n<h2>What is \/dev\/null 2&gt;&amp;1?<\/h2>\n<p>This is the way to execute a program quietly, and hide all its output.<\/p>\n<p>\/dev\/null is a special filesystem object that throws away everything written into it. Redirecting a stream into it means hiding an output.<\/p>\n<p>The 2&gt;&amp;1 part means &#8220;redirect both the output and the error streams&#8221;. Even if your program writes to stderr, that output will not be shown.<\/p>\n<p>Cheers<\/p>\n","protected":false},"excerpt":{"rendered":"<p>You can use mysqldump command to backup database. The mysqldump client is a backup program. It can be used to dump a database or a collection of databases for backup or for transferring the&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":2843,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[35,14],"tags":[],"youtube_video":null,"_links":{"self":[{"href":"http:\/\/www.extradrm.com\/index.php?rest_route=\/wp\/v2\/posts\/1418"}],"collection":[{"href":"http:\/\/www.extradrm.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.extradrm.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.extradrm.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.extradrm.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1418"}],"version-history":[{"count":0,"href":"http:\/\/www.extradrm.com\/index.php?rest_route=\/wp\/v2\/posts\/1418\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"http:\/\/www.extradrm.com\/index.php?rest_route=\/wp\/v2\/media\/2843"}],"wp:attachment":[{"href":"http:\/\/www.extradrm.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1418"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.extradrm.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1418"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.extradrm.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1418"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}