{"id":2343,"date":"2010-12-14T00:36:17","date_gmt":"2010-12-13T19:06:17","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=2343"},"modified":"2016-12-19T15:06:35","modified_gmt":"2016-12-19T09:36:35","slug":"database-backup-script-for-windows","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/database-backup-script-for-windows\/","title":{"rendered":"Database Backup Script for windows"},"content":{"rendered":"<p>In one of the project I am working on, the application needs to be deployed on windows server. To take the database backup, I wrote the script which does the following :<\/p>\n<ol>\n<li>Takes database dump and copy it to the backup folder.<\/li>\n<li>Zip that backup file and rename it to the format &#8220;YYYYMMDD_HHMMSS&#8221;<\/li>\n<li>Removes\u00a0 all the backups older than 30 days.<\/li>\n<\/ol>\n<p>I used <a href=\"http:\/\/www.tothenew.com\/blog\/592\/\">mysqldump<\/a> command to take database backup. To zip the backup file using the command line, I found 7-zip (download) which is freely available.\u00a0 We need to have forfiles.exe (download) which enables us to remove old backup files (say older than 30 days).<\/p>\n<p>[bash]<br \/>\n@echo off<br \/>\nCLS<br \/>\nset hour=%time:~0,2%<br \/>\nif &amp;amp;quot;%hour:~0,1%&amp;amp;quot; == &amp;amp;quot; &amp;amp;quot; set hour=0%hour:~1,1%<br \/>\nset min=%time:~3,2%<br \/>\nif &amp;amp;quot;%min:~0,1%&amp;amp;quot; == &amp;amp;quot; &amp;amp;quot; set min=0%min:~1,1%<br \/>\nset secs=%time:~6,2%<br \/>\nif &amp;amp;quot;%secs:~0,1%&amp;amp;quot; == &amp;amp;quot; &amp;amp;quot; set secs=0%secs:~1,1%<br \/>\nset year=%date:~-4%<br \/>\nset month=%date:~3,2%<br \/>\nif &amp;amp;quot;%month:~0,1%&amp;amp;quot; == &amp;amp;quot; &amp;amp;quot; set month=0%month:~1,1%<br \/>\nset day=%date:~0,2%<br \/>\nif &amp;amp;quot;%day:~0,1%&amp;amp;quot; == &amp;amp;quot; &amp;amp;quot; set day=0%day:~1,1%<br \/>\nset datetimef=%year%%month%%day%_%hour%%min%%secs%<br \/>\n&amp;amp;quot;MYSQL_BIN_PATH\\mysqldump&amp;amp;quot; &#8211;user=root &#8211;password=MY_PASSWORD MY_DATABASE_NAME &amp;amp;gt; &amp;amp;quot;BACKUP_FOLDER_PATH\\backup.sql&amp;amp;quot;<br \/>\n&amp;amp;quot;7ZIP_EXE_PATH\\7z&amp;amp;quot; a -tzip &amp;amp;quot;BACKUP_FOLDER_PATH\\&amp;amp;quot;%datetimef%&amp;amp;quot;.zip&amp;amp;quot; &amp;amp;quot;BACKUP_FOLDER_PATH\\backup.sql&amp;amp;quot;<br \/>\ndel &amp;amp;quot;BACKUP_FOLDER_PATH\\backup.sql&amp;amp;quot;<br \/>\n&amp;amp;quot;FORFILES_EXE_PATH\\forfiles.exe&amp;amp;quot; \/p BACKUP_FOLDER_PATH \/s \/m *.* \/d -30 \/c &amp;amp;quot;cmd \/c del \/q @path<br \/>\n[\/bash]<\/p>\n<p>Prerequisites for the above script to work would be :-<\/p>\n<ol>\n<li>Mysql and 7zip installed.<\/li>\n<li>Forfiles.exe is downloaded<\/li>\n<li>All the Path&#8217;s in script are replaced correctly.<\/li>\n<\/ol>\n<p>I also used a background job to run this script automatically every hour on the working days. Though same can be achieved with windows scheduler too.<\/p>\n<p>Hope this helped.<\/p>\n<p>Cheers!<br \/>\n~~Amit Jain~~<br \/>\namit@intelligrape.com<br \/>\nhttp:\/\/intelligrape.com\/<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In one of the project I am working on, the application needs to be deployed on windows server. To take the database backup, I wrote the script which does the following : Takes database dump and copy it to the backup folder. Zip that backup file and rename it to the format &#8220;YYYYMMDD_HHMMSS&#8221; Removes\u00a0 all [&hellip;]<\/p>\n","protected":false},"author":7,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":1},"categories":[1],"tags":[473,475,4843,474,472],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/2343"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/7"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=2343"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/2343\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=2343"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=2343"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=2343"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}