Python MySQL Backup Script

Here is a simple python based MySQL backup script. It will individually dump all your MySQL databases and then tar gzip them to a specified location.

Having Python installed is the only real prerequisite.

Script Details

Note, that the script isn’t verbose unless there is an error. This is so you can schedule it as a cron job, and only get an email when something goes wrong.

It also uses a configuration file as a security precaution. The reason for using a config file, it prevents the user and password being visible in the running processes list. So for example if you were running a mysqldump and put password into your command line, another user could query the running processes and see the password.

I typically store my scripts in the /usr/local/bin directory of my Linux systems. In general, it’s a good place to store custom scripts and applications.

You’ll also need to create your configuration file, like below:

In my case, I store the config file as /etc/mysql.cnf however make sure that it’s only readable by the root user.

Script Usage

You simply execute the script as below

The Script



  1. Serge   •  

    Nice script, thank you !
    Is there any it’s “brother” :) to restore ALL MySql databases from /var/backups back to MySQL (not one by one – but every one) ?
    Thank you in advance !
    Best regards,

  2. Saigan   •  

    Thank you so much for the script. I have tested it and working well.


Leave a Reply to Saigan Cancel reply

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

six − = 3

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