gkisiel / python-mysql-backup

Script that creates a backup of a list of MySQL databases

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Python MySQL Backup


A python script to create MySQL backups using mysqldump, mysql_config_editor, and tar utility. The original version was taken from http://tecadmin.net/python-script-for-mysql-database-backup/ by Rahul Kumar but I have added major changes since.

Requirements:

  1. Python 2.7
  2. mysqldump
  • Windows: download the "MySQL Product Archives" from https://downloads.mysql.com/archives/community/. After extracting the file, add the "bin" folder (This includes mysqldump.exe) to Windows PATH so as to have it available from command line.
  • Linux: Install the MySQL Client.
  1. mysql_config_editor Passing passwords within the command line is a bad practice. mysql_config_editor enables you to store authentication in a .mylogin.cnf file so you don't need to pass it to the command line. For more info, go to https://dev.mysql.com/doc/refman/5.7/en/mysql-config-editor.html
  • Windows: It's also included in the "MySQL Product Archives"
  • Linux: It's part of the MySQL Client tools
  1. tar Any Windows 10 after build 17063 come with tar by default. For older versions, you can download it from http://gnuwin32.sourceforge.net/packages/gtar.htm

Steps:

Define login paths with mysql_config_editor

From command line execute the following.

shell> mysql_config_editor set --login-path=loginpath1
         --host=yourdbhost1 --user=dbuser1 --password
Enter password: enter password "dbpass1" here

This will store your authentication in .mylogin.cnf for future use. Repeat previous steps for every DB you want to include in your back up.

To check your login paths execute the following. These login paths will be used in your JSON file.

shell> mysql_config_editor print --all
[loginpath1]
user = dbuser1
password = *****
host = yourdbhost1
[loginpath2]
user = dbuser2
password = *****
host = yourdbhost2
JSON File

Create a file containing a list of DBs to be backed up. The DB list must be in JSON format. See example below.

[
    {
        "loginPath": "loginpath1",
        "dbName": "dbname1",
        "bkpPath": "/path/to/backup/folder1/"
    },
    {
        "loginPath": "loginpath2",
        "dbName": "dbname2",
        "bkpPath": "/path/to/backup/folder2/"
    }
]
Running the script (dbbackup.py)

Execute the following:

shell> /path/to/dbbackup.py /path/to/your_database_list.json

Or

shell> python /path/to/dbbackup.py /path/to/your_database_list.json

The script will output something like.

Backing up DB: dbname1
/path/to/backup/folder1/20200422-152835/dbname1.sql
Backup script completed
Your backups has been created in '/path/to/backup/folder1/20200422-152835' directory

Backing up DB: dbname2
/path/to/backup/folder1/20200422-152835/dbname2.sql
Backup script completed
Your backups has been created in '/path/to/backup/folder2/20200422-152835' directory

Backup results:
--------------
dbname1          ==> Succeeded 
dbname2          ==> Succeeded

About

Script that creates a backup of a list of MySQL databases


Languages

Language:Python 100.0%