If you read Backing up Your Data with cwRsync and snyc2nas you learned how to send your data to an off site server. Now I’ll show you a couple ways to make incremental and dated zipped backups that you can store on your server to transfer with sync2nas or FTP the backup to your home box.

I’ll attach the batch files at the end of the post for those that don’t want to copy and paste the code. Now lets get started.

For the first method you’ll need the batch file and a text file with you FTP login information and you’ll need a command line zip program. You can use Winzip Command Line Tools if you have Winzip installed on the sever or7zip if you have it installed. I use GNU Gzip, you download it at the projects Sourceforge page, so this tutorial will be specific to it.

First the batch file to create the backup.

Copy and paste this into your text editor and save as mysql-ftp.bat:

@ECHO OFF

@REM Set directory variables.
SET basedir=C:\Backup
SET workdir=C:\Temp
SET mysqldir=C:\path\to\mysql\bin
SET gzipdir=C:\PROGRA~1\GnuWin32\bin
SET mysqlpassword=mysqlpassword
SET mysqluser=mysqluser

@REM Change to mySQL directory
CD %mysqldir%

@REM dump all databases.
mysqldump -u %mysqluser% -p%mysqlpassword% –all-databases >%workdir%\backup.sql

@REM Change to working directory
CD %workdir%

@REM Zip up databases
%gzipdir%\gzip.exe backup.sql

@REM Chage the file name to a random name
MOVE backup.sql.gz backup.%random%.gz

@REM FTP file offsite
FTP -n -s:%basedir%\ftp.txt

@REM Remove old backup files
del backup.sql
del backup.*.gz

@REM Change back to base dir
CD %basedir%

Some notes on the above.

  • basedir is the directory you are launching the batch file from.
  • workdir is the temp directory where the files are stored until the job is done.
  • mysqldir is where your mysql binaries are.
  • gzipdir is where you installed GNU gzip and this is default for the installer so no need to change it.
  • mysqlpassword is the password to connect to mysql.
  • mysqluser is mysql usr tht can connect to mysql.

Once the backup is made and FTP was sent the script will delete the backup file. If you want to keep the backups it created remove these lines.

@REM Remove old backup files
del backup.sql
del backup.*.gz

If you want to backup only one database instead of all of them change this line:

mysqldump -u %mysqluser% -p%mysqlpassword% --all-databases >%workdir%\backup.sql

To This:

mysqldump -u %mysqluser% -p%mysqlpassword% databasename >%workdir%\backup.sql

If you noticed this line,

FTP -n -s:%basedir%\ftp-commands.txt

You’ll now need the contents of the FTP script which you will place in the basedir where the batch file is. Here it is.

open
ftp.yourftpserver.com
user
username
password
bin
put backup.*.gz
quit

That’s it. Copy the batch file code, make any of the changes outlined above and call it backuptoftp.bat and copy the FTP script and name it ftp.txt.

Read on and I’ll give you another example that will make a date based backup and store it on your server for later transfer.

Batch File to Backup Your Windows Server Database

You May Also Like