Skip to content

Instantly share code, notes, and snippets.

@trafficinc
Last active March 8, 2024 04:26
Show Gist options
  • Save trafficinc/a0b7cfa58a00e794bf8c7dd0626c2765 to your computer and use it in GitHub Desktop.
Save trafficinc/a0b7cfa58a00e794bf8c7dd0626c2765 to your computer and use it in GitHub Desktop.
Python script for taking mysqldump
#!/usr/local/bin/python3
"""
Will backup all the databases listed, will put files in same DIR as script'
To run: $ python dbbackup.py OR python3 dbbackup.py
"""
import configparser
import os
import time
import getpass
HOST='localhost'
PORT='3306'
DB_USER='username'
DB_PASS='password'
# if using one database... ('database1',)
databases=('database1','database2','database3')
def get_dump(database):
filestamp = time.strftime('%Y-%m-%d-%I')
# D:/xampp/mysql/bin/mysqldump for xamp windows
os.popen("mysqldump -h %s -P %s -u %s -p%s %s > %s.sql" % (HOST,PORT,DB_USER,DB_PASS,database,database+"_"+filestamp))
print("\n|| Database dumped to "+database+"_"+filestamp+".sql || ")
if __name__=="__main__":
for database in databases:
get_dump(database)
@shahgurmeet
Copy link

@trafficinc The code works fine and the .sql files are getting generated. But the generated dump file is empty, 0 KB
Can you help me out in this.
I am using this on Windows 10 Pro and Current Version:- 3.9.7
Error Msg I am getting is:
'mysqldump' is not recognized as an internal or external command,
operable program or batch file.
'mysqldump' is not recognized as an internal or external command,
operable program or batch file.

@singhj775
Copy link

Add mysql path into your Environment variables

@codeshoel
Copy link

I'm having an empty file of 0kb when I run on the Windows task scheduler, but when run on cmd it backups the database with content in file

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment