Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save ilyasahsan123/a0bfcc8b5fe4de09b12c4922dfe7ac9f to your computer and use it in GitHub Desktop.
Save ilyasahsan123/a0bfcc8b5fe4de09b12c4922dfe7ac9f to your computer and use it in GitHub Desktop.
backup_database.py
import datetime
import os
import glob
now = datetime.datetime.now().strftime("%d-%m-%y")
date = now.split("-")
date_of_today = date[0]
db_user = "user"
db_user_password = "user_password"
db = "database_name"
db_file_name = "temp_database.sql"
if os.path.isfile(db_file_name):
os.remove(db_file_name)
dumpdatabase = "mysqldump -u " + db_user + " -p" + db_user_password + " " + db + " > " + db_file_name
os.system(dumpdatabase)
sql_files = glob.glob("*.sql")
for sql in sql_files:
sql_name = sql
sql_date_of_today = sql_name.split("-")[0]
if date_of_today == sql_date_of_today:
os.remove(sql_name)
break
command = "mv" + " " + db_file_name + " " + now + ".sql"
os.system(command)
exit()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment