#!/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'] 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 = "admin@domain.com" you = "admin@domain.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()
Subscribe
0 Comments