Script to Backup MySQL Database

Being that that this blog is “Proudly Powered by WordPress,” which uses a MySQL database, I wanted to find a way to schedule regular backups of MySQL databases. I am not that well versed with MySQL – all of the DB’s which I work with professionally are either SQL Server or Oracle – so I was looking to MySQL WorkBench to do the scheduling of regular backups, and to my dismay, you can’t schedule backups there.

I am not sure if this is something that Oracle has stripped out, or what, but it’s pretty crazy. So, I created this quick and easy PowerShell script to use the MySQLDump.exe to export the DB to a specified location, and cleanup any backups older than 1 month old.

Use the Task Scheduler to create a task to execute this script daily or weekly, and you’re set with your local backups of your MySQL DB.

Hope someone new to MySQL stumbles across this in less time than I took to realize that this functionality was not in MySQL WorkBench and to write the script. Enjoy!

############################################
# Author: Brad Payne
# Purpose: Backup MySQL databases
# Date: 5/1/2012
############################################

############################################
#  VARIABLES WHICH NEED TO BE DEFINED
############################################
$mysqlpath = <PATH TO FOLDER CONTAINING MYSQLDUMP.EXE>
$backuppath =<BACKUP LOCATION>
$username = <DB USERNAME>
$password = <DB PASSWORD>
$database = <DB NAME>
$errorLog = <LOCATION OF ERROR LOG (INCLUDING FILE NAME)>
############################################
# END OF VARIABLES NEEDING DEFINED
############################################

$date = Get-Date
$timestamp = "" + $date.day + $date.month + $date.year + "_" + $date.hour + $date.minute

$backupfile = $backuppath + $database + "_" + $timestamp +".sql"

CD $mysqlpath
.\mysqldump.exe --user=$username --password=$password --log-error=$errorLog --result-file=$backupfile --databases $database 

CD $backuppath
$oldbackups = gci *.sql*

for($i=0; $i -lt $oldbackups.count; $i++){
	if ($oldbackups[$i].CreationTime -lt $date.AddMonths(-1)){
		$oldbackups[$i] | Remove-Item -Confirm:$false
	}
}
This entry was posted in Uncategorized and tagged , , , . Bookmark the permalink.

One Response to Script to Backup MySQL Database

  1. Murali says:

    I’ve used the Ultimate Deployment Appliance (2.0b5) to install ESXi with the defualt settings then configure it with the PowerCLI or host profiles.Getting a true hands-off scripted install that can be pushed down to a box with all configs is a little trickier with ESXi, but a quick way uses the vMA/vCLI:Capture the configuration with vicfg-cfgbackup.pl server -s once you have it configured the way you want it.Once you have the defualt image pushed back onto it, you can use vicfg-cfgbackup.pl server -l to drop the saved config back onto it.The drawback is that vicfg-cfgbackup only likes to drop the config back onto the SAME version of ESXi that it captured the config from. The reasoning is pretty clear, but there is a -f (force) option if you’d like to use it.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>