MySQL Powershell Backup Script

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.

Download the script (ZIP file)

Configuration

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 Path

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

  1. Download and install the connector from http://dev.mysql.com/downloads/connector/net/
  2. Uncomment Line 38 of the script.  This should say system.reflection.assembly]::LoadWithPartialName(“MySql.Data”)
Loading the library directly (the default method)
If you don’t wish to install the .Net driver, you can directly load the DLL file instead.  The required DLL file is included as part of the ZIP file.
  1. Uncomment line 43 of the script.  This should say Add-Type -Path “C:\Data\Backups\MySQL\MySql.Data.dll”
  2. 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

%windir%\System32\WindowsPowerShell\v1.0\powershell.exe “c:\backups\Generic7DayBackup.ps1”

Lastly…

This is by design a very simple script.  If you have any suggestions on how to improve it, please let me know by posting a comment.

 

2 thoughts on “MySQL Powershell Backup Script

  1. James

    Hi Alexander,

    I am hoping you can help me out as I am new to PowerShell. I have tried to run your script for backing up MySQL db’s and have got the following error:

    Backing up database: mysql
    cmd : The term ‘cmd’ is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
    At C:\Backups\Generic7DayBackup.ps1:81 char:2
    + cmd /c ” `”$pathToMySqlDump`” -h $mysqlServerAddress -u $mysqlUsername -p$mysql …
    + ~~~
    + CategoryInfo : ObjectNotFound: (cmd:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

    Can you help?

    Kind regards

    James

  2. alexanderjohn Post author

    Hi James,

    The line beginning ‘cmd /c’ is actually calling a standard Windows command prompt. The /c switch instructs the command prompt to close once the specified command has completed. In this script, the specified command is calling mysqldump passing it the necessary parameters to backup a specific database.

    There are a couple of things you can check:

    1. A bit obvious, but are you able to run the command ‘cmd’ from within Powershell?

    2. At the top of the script there are several paths that need to be specified, in particular $pathToMySqlDump. Something that has caught me out before is that the path being correct, but the user account that is running the script does not have access to the path.

    3. Have you configured the Execution Policy for Powershell (see http://technet.microsoft.com/en-us/library/ee176961.aspx)?

    Alex

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.