Skip to content

Instantly share code, notes, and snippets.

@TheYkk

TheYkk/.database.cnf

Last active Nov 14, 2018
Embed
What would you like to do?
mysql backup
[mysqldump]
host=localhost
user=user
password=password
import datetime, os
today = str(datetime.datetime.today()).split(" ")[0]
backups = os.listdir("files")
def days_between(d1, d2):
d1 = datetime.datetime.strptime(d1, "%Y-%m-%d")
d2 = datetime.datetime.strptime(d2, "%Y-%m-%d")
return abs((d2 - d1).days)
cmd = 'mysqldump --defaults-extra-file=".database.cnf" --all-databases --events > files/'+today+'.sql'
os.system(cmd)
for backup in backups:
day = backup.replace(".sql", "")
diff = days_between(day, today)
if diff > 7:
os.remove('files/'+backup)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment