Skip to content

Instantly share code, notes, and snippets.

@TheYkk
Last active November 14, 2018 14:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save TheYkk/aa819f09ce533627ea0506ab3267d2c2 to your computer and use it in GitHub Desktop.
Save TheYkk/aa819f09ce533627ea0506ab3267d2c2 to your computer and use it in GitHub Desktop.
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