The Ant backup script example below is designed to backup a MySQL database name called Test. It will compress and store it in archive_db/Test_YYYY-MM-DD_HH.MM.SS.sql.tar.gz.
Before you run this script, change the db.username
, db.password
, db.name
and the path of mysqldump
accordingly.
To backup, run the following in the Command prompt:
ant -f build_backup.xml backup_db
Here is the Ant backup script for MySQL:
<?xml version="1.0" encoding="UTF-8" standalone="no"?> <!-- Author: Xuan Ngo Description: Backup MySQL database name called Test and put in it archive_db/Test_YYYY-MM-DD_HH.MM.SS directory. Before you run this script, change the username, password and the path of mysqldump accordingly. --> <project basedir="." default="nothing" name="Test"> <!-- Change the following properties accordingly. --> <property name="db.username" value="root"/> <property name="db.password" value="mypassword"/> <property name="db.name" value="${ant.project.name}"/><!-- The name of the database to backup. --> <property name="mysqldump" value="C:\wamp\bin\mysql\mysql5.1.33\bin\mysqldump"/><!-- Path to mysqldump program. --> <property name="archive.dir" value="archive_db"/> <tstamp><format property="DAY_TIME_NOW" pattern="yyyy-MM-dd_HH.mm.ss" /></tstamp> <target name="nothing"> <echo message="By default, it does nothing."/> </target> <!-- *************************************** Backup MySQL database **************************************** --> <target name="backup_db"> <mkdir dir="${archive.dir}"/><!-- Create the archive directory anyway. --> <!-- Create a directory to dump the backup --> <property name="backup.dir" value="${archive.dir}/${ant.project.name}_${DAY_TIME_NOW}"/> <mkdir dir="${backup.dir}"/> <!-- Command to dump the database to *.sql file.--> <exec executable="${mysqldump}" output="${backup.dir}/${ant.project.name}_${DAY_TIME_NOW}.sql"> <arg value="--hex-blob"/> <arg value="--extended-insert=false"/> <arg value="--complete-insert=true"/> <arg value="--user=${db.username}"/> <arg value="--password=${db.password}"/> <arg value="${db.name}"/> </exec> <!-- Compress the dumped file(*.sql) --> <tar destfile="${backup.dir}/${ant.project.name}_${DAY_TIME_NOW}.sql.tar.gz" compression="gzip"> <tarfileset dir="${backup.dir}"> <include name="${ant.project.name}_${DAY_TIME_NOW}.sql"/> </tarfileset> </tar> <!-- Delete the dumped file(*.sql) --> <delete file="${backup.dir}/${ant.project.name}_${DAY_TIME_NOW}.sql"/> </target> </project>