2010
04.12

Backups are crucial!
Since that is fact I wrote a little script in Python to easily backup mysql databases on a linux server.

  Python Mysqldump Backup 1.1 (858 bytes, 497 hits)


How do i configure it?
After you downloaded the script to your Linux server you will need a few Linux packages. Execute the following commands as root:

apt-get update
apt-get install python mysqldump bzip2

What does it do?
This script dumps a certain database (or all databases) into a single mysql file, which is zipped (bzip2) afterwards.

Now edit the script with your preferred editor.

vim mysqldump-backup.py

Customize the following lines:

# 2. Config
mysqlUser = “root”
mysqlPwd = “YOUR_PASSWORD”
mysqlDatabase = “–all-databases”
dumpDir = “/tmp/”
dumpFile = “mysqldump-” + strftime(“%Y-%m-%d-%H-%M-%S”, gmtime()) + “.sql”

The last step is to make the script executable:

chmod u +x mysqldump-backup-1.1.py

Thats it!

You can now run the script:

/path/to/the/script/mysqldump-backup-1.1.py

To run it daily (crontab):

crontab -e

Add at the bottom:

0 3 * * * /path/to/the/script/mysqldump-backup.py

Now your databases will be backupped daily at 03:00 am

Download

  Python Mysqldump Backup 1.1 (858 bytes, 497 hits)

Changelog
1.1

  • Removed trailing semicolons
  • Renamed variables

1.0

  • Initial release

Have fun!
Andreas Glaser aka JaZz

6 comments so far

Add Your Comment
  1. Why the semicolons at the end of every line? None of them is needed!

  2. Well-commented and not unnecessarily verbose! I was looking to do this from scratch but I’ll just customize this to work within my (mt) account.


    Brie

  3. @ Keith
    Thanks… i removed them!

  4. When I download that gives me a 404, I
    think your work is excellent.

    A hug and thanks from Mexico

  5. alright i fixed it @ almsx

  6. Solution ->
    ———————————————————
    #!/usr/bin/python
    # -*- coding: UTF-8 -*-
    import string
    import time
    import datetime
    import os

    mysqlUser = “user”
    mysqlPwd = “password”
    mysqlDatabase = “database_name”
    dumpDir = “/destination/”
    dumpFile = “mysqldump_name” + strftime(“%Y-%m-%d-%H-%M-%S”, gmtime()) + “.sql”
    os.system(“mysqldump %s > %s%s” % (mysqlDatabase, dumpDir, dumpFile))
    ———————————————————

    Thanks
    Infrapoa.com.br

*