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.
- Python 2.7
- 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.
- 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
- 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
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
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/"
}
]
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