Skip to content

Instantly share code, notes, and snippets.

@elik1001
Created November 15, 2016 20:24
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 elik1001/e1aaec695045e3e5a293489cff9d06b6 to your computer and use it in GitHub Desktop.
Save elik1001/e1aaec695045e3e5a293489cff9d06b6 to your computer and use it in GitHub Desktop.
#!/bin/python
import os
import sys
import time
import datetime
import subprocess
import smtplib
from email.mime.text import MIMEText
def mysqlBackup():
mysql_host = 'localhost'
mysql_user = 'root'
mysql_user_passwd = 'password'
backup_path = '/mystuff/mysql_backup/'
dblist = ['wordpress']
datetime = time.strftime('%m%d%Y-%H%M%S')
for db in dblist:
backupdate = backup_path + db + "_" + datetime
logger("creating backup folder - " + backupdate)
if not os.path.exists(backupdate):
os.makedirs(backupdate)
xmldumpcmd = "/bin/mysqldump -u " + mysql_user + " -p" + mysql_user_passwd + " " + db + " --xml" + " > " + backupdate + "/" + db + ".xml"
os.system(xmldumpcmd)
logger(db + " XML backup complete, as... " + db + ".xml")
sqldumpcmd = "/bin/mysqldump -u " + mysql_user + " -p" + mysql_user_passwd + " " + db + " > " + backupdate + "/" + db + ".sql"
os.system(sqldumpcmd)
logger(db + " SQL backup complete, as... " + db + ".sql")
logger("Backup script completed")
logger("Backups has been created in '" + backupdate + "' directory")
def syncRemote():
logger("****************** rsync starting " + time.strftime("%m-%d-%Y %H:%M:%S") + "***********************")
dir_list = ['/mystuff', '/var/tmp/elik']
for i in dir_list:
logger("****************** Now syncing: " + i + " ******************")
port = "'ssh -p 60022 -i /mystuff/backup_scripts/rsync-key'"
#key = ' -i /mystuff/backup_scripts/rsync-key'
cmd = '/bin/rsync -auvz -e ' + port + ' --progress ' + i + ' elik-remote@eli102.asuscomm.com:backup/'
logger(cmd)
progress = subprocess.check_output(cmd, shell=True)
#progress = subprocess.Popen(cmd, shell=True, stdin=subprocess.PIPE, stdout=subprocess.PIPE)
#for line in iter(progress.stdout.readline, b''):
#logger(line.rstrip())
logger(progress)
logger("****************** rsync completed " + time.strftime("%m-%d-%Y %H:%M:%S") + " ***********************")
def logger(log):
f.write(log + "\n")
sys.stdout.flush()
def mailer():
backupfile = '/var/tmp/backup.log'
me = "eli.kleinman@gmail.com"
you = "eli.kleinman@gmail.com"
if (os.path.isfile(backupfile)):
fp = open(backupfile, 'rb')
msg = MIMEText(fp.read())
fp.close()
msg['Subject'] = 'Backup status - output %s' % backupfile
msg['From'] = me
msg['To'] = you
s = smtplib.SMTP('localhost')
s.sendmail(me, [you], msg.as_string())
s.quit()
os.rename(backupfile, backupfile+"-old")
else:
msg = MIMEText("There was an error completing the backup")
msg['Subject'] = 'Backup error opening file - %s' % backupfile
msg['Subject'] = 'Backup error '
s = smtplib.SMTP('localhost')
s.sendmail(me, [you], msg.as_string())
s.quit()
if __name__ == '__main__':
f = open('/var/tmp/backup.log','a+')
f.write("+++++++++++++++++++++ START TIME ** " + time.strftime("%m-%d-%Y %H:%M:%S") + " +++++++++++++++++++++\n")
sys.stdout.flush()
mysqlBackup()
syncRemote()
f.write("+++++++++++++++++++++ END TIME ** " + time.strftime("%m-%d-%Y %H:%M:%S") + " +++++++++++++++++++++\n")
f.close()
mailer()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment