Backup your DB and files using batch file

I’ve been given a small task to backup our issue database on daily basis, for which I created a tiny backup file and would like to share with you.

The script below will do the following

  1. Create MySQL dump of your database using configuration set
  2. Compress files and MySQL dump into single archive file and copy to destination folder
  3. It will always create old backup so you always have previous version
  4. Copy both old and new backups to network shared resourse

@ECHO OFF
 REM ----------- START OF CONFIGURATION HERE
 SET DESTINATION_FOLDER=c:\dest\
 SET BACKUP_FILENAME=yourBackup.rar
 SET BACKUP_FILENAME_OLD=yourBackup.old.rar
 SET BACKUP_DB_FILENAME=backup.sql
 SET MYSQL_FOLDER=C:\wamp\bin\mysql\mysql5.1.30\bin\
 SET MYSQL_DB_NAME=yourdbname
 SET MYSQL_DB_USER=yourdbuser
 SET MYSQL_DB_PASS=yourdbpass
 SET NETWORK_PATH=\\your_backup_pc_ip\path
 SET NETWORK_USERNAME=your_backup_pc_user
 SET NETWORK_PASSWORD=your_backup_pc_pass
 REM ----------- END OF CONFIGURATION HERE

CD %~dp0

%MYSQL_FOLDER%mysqldump.exe -u %MYSQL_DB_USER% -p%MYSQL_DB_PASS% %MYSQL_DB_NAME% >%BACKUP_DB_FILENAME%
 ECHO DB Done

rar a %BACKUP_FILENAME% @files.lst > NUL
 @del %DESTINATION_FOLDER%%BACKUP_FILENAME_OLD% 2>NUL
 @rename %DESTINATION_FOLDER%%BACKUP_FILENAME% %BACKUP_FILENAME_OLD% 2>NUL
 copy %BACKUP_FILENAME% %DESTINATION_FOLDER% > NUL
 ECHO Files Done

@del %BACKUP_FILENAME% 2>NUL
 @del %BACKUP_DB_FILENAME% 2>NUL
 ECHO Removing Temporary Files Done

ECHO Accessing Network...
 net use %NETWORK_PATH% %NETWORK_PASSWORD% /user:%NETWORK_USERNAME%
 copy %DESTINATION_FOLDER%%BACKUP_FILENAME% %NETWORK_PATH% > NUL
 copy %DESTINATION_FOLDER%%BACKUP_FILENAME_OLD% %NETWORK_PATH% > NUL
 ECHO Network Copy Done

ECHO All Done

To make backup regular I’ve used Cron NT service for Windows because I honestly don’t like Windows Scheduler and always disable it 😉
Here is the crontab line that suits my needs.

10 23 * * * cmd /c C:\wamp\backup\backup.bat

This will backup my stuff every day at 23:10.

That’s pretty much it.
Cheers!

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: