Skip to content

Instantly share code, notes, and snippets.

Last active May 8, 2023 14:34
What would you like to do?
Python script for taking mysqldump
Will backup all the databases listed, will put files in same DIR as script'
To run: $ python OR python3
import configparser
import os
import time
import getpass
# if using one database... ('database1',)
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:
Copy link

Will need more information to figure out the issue, what OS are you on, what settings?

Man got it running all good. Thanks for the fast reply tho my g.

Copy link

retiels commented Apr 9, 2022

Cuando ejecuto el script, no reconoce la base de datos porque lee cada letra por sí sola. Por ejemplo :

Base de datos: Prueba

Lee: T,e,s,t = 4 bases de datos.

if you database_name is "Test", try to:

databases = ('Test',)

because it´s a tuple!

Copy link

qq516249940 commented Apr 20, 2022

os.popen is not close connect,you can use with like with os.popen(command, "r") as p:
r =

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.

Copy link

Add mysql path into your Environment variables

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