This is easily one of my most commonly used Powershell scripts, and a variation on it runs on my servers at least once a day. It is essentially a very simple script: it contacts a specified MySQL server and then generates a .SQL backup files for each database found. It prefixes each backup filename with the day name (monday, tuesday etc.) so if you run it on daily basis, it gives you a seven day backup rotation for your databases.
As always, the use of this script and the accompanying files is done entirely at your own risk. No guarantee either direct or indirect is implied regarding the security, stability, reliability, impact or performance of this script.
The zip file includes copies of mysqldump.exe and mysql.data.dll that you may require. For details on how to configure and use the script, please see below.
There are a few items you will need to configure before you can use the script in anger. All of the configuration options are presented at the top of the script, so you don’t need to delve into the main chunk of code.
Backup Store Folder
This is where the MySQL backup files will be placed. Please enter the path here, but do not include any trailing ‘\’
MySQL User Account
You will require a MySQL user account that has SHOW DATABASES, SELECT, LOCK TABLES and RELOAD permissions. It is also highly advised – especially if performing backups from a remote machine – that this user’s permissions are also limit to readonly.
mysqldump is the application that actually creates the backup file. It is typically installed with MySQL, but if you are running the script remotely or don’t know the file’s location, mysqldump.exe is included in the ZIP file.
MySQL .Net Connector
The script requires the MySQL .Net Connector driver. This can either be installed, or you can directly load the DLL file.
Using the connector
- Download and install the connector from http://dev.mysql.com/downloads/connector/net/
- Uncomment Line 38 of the script. This should say system.reflection.assembly]::LoadWithPartialName(“MySql.Data”)
- Uncomment line 43 of the script. This should say Add-Type -Path “C:\Data\Backups\MySQL\MySql.Data.dll”
- Amend the path of line 43 to point to the location of the DLL file
Running the script
You can either run the script manually (through Powershell), or automate it through Task Scheduler. To make life a little easier I tend to create a batch file containing the Powershell command and then invoke the batch file through the task scheduler.
The general format of the command to include in the batch file is