备份脚本
#!/usr/bin/env python#_*_coding:utf-8_*_"""@File: backup_db.py@Author: OldTan@Email: 996298929@qq.com@Last Modified: 20180408"""import osimport datetimefrom threading import Threadfrom logs import logHOST = '10.36.1.101'USER = 'root'PW = '1qaz#EDC'PATH = '/mnt/tantianran_mysql_backup/'NOW = datetime.datetime.now()NOWSTR = NOW.strftime('%Y%m%d')CLEANSTR = (NOW - datetime.timedelta(days=5)).strftime('%Y%m%d')DBS = [ 'urun_private_cloud', 'urun-asr', 'ambari', 'hive', 'oozie', 'storm-etl', 'urun-sms', 'analyzer-etl', 'zabbix', 'zabbix-key']after_backup = []def clean(db): database = '{dbs}'.format(dbs=db) format = 'rm -rf %(path)s%(db)s.%(date)s.sql' kv = {'path': PATH, 'date': CLEANSTR, 'db' : database} expiredDbFile = ('%(path)s%(db)s.%(date)s.sql' % kv) if os.path.exists(expiredDbFile): msg = 'Start Delete expired database file: %s' % (expiredDbFile) log(msg) excute( format % kv)def backup(db): database = '{dbs}'.format(dbs=db) format = 'mysqldump -u %(user)s -h %(host)s -p%(pw)s %(db)s > %(path)s%(db)s.%(date)s.sql' kv = {'user': USER, 'host': HOST, 'pw': PW, 'path': PATH, 'date': NOWSTR, 'db' : database} dbname = ('%(db)s.%(date)s.sql' % kv) after_backup.append(dbname) excute(format % kv)def excute(cmd): os.system(cmd)def check(): for i in after_backup: if os.path.exists(i): m = 'Backup success DB: %s' % i log(m) else: m = 'Backup failed DB: %s' % i log(m)def main(): dbs = range(len(DBS)) def exec_task(th): for i in dbs: th[i].start() for i in dbs: th[i].join() backup_t = [] clean_t = [] for i in dbs: t1 = Thread(target=backup, args=(DBS[i],)) t2 = Thread(target=clean, args=(DBS[i],)) backup_t.append(t1) clean_t.append(t2) exec_task(backup_t) exec_task(clean_t) check() if __name__ == "__main__": main()
scp sql文件脚本
#!/usr/bin/env python#_*_coding:utf-8_*_"""@File: backup_db.py@Author: OldTan@Email: 996298929@qq.com@Last Modified: 20180408"""import osimport datetimefrom threading import Threadfrom logs import logREMOTE_PATH = '/mnt/tantianran_mysql_backup/'REMOTE_HOST = '10.36.1.55'REMOTE_USER = 'root'PATH = '/mnt/tantianran_mysql_backup/'NOW = datetime.datetime.now()NOWSTR = NOW.strftime('%Y%m%d')CLEANSTR = NOW.strftime('%Y%m%d')DBS = [ 'urun_private_cloud', 'urun-asr', 'ambari', 'hive', 'oozie', 'storm-etl', 'urun-sms', 'analyzer-etl', 'zabbix', 'zabbix-key']def scp_db_file(db): database = '{dbs}'.format(dbs=db) format = 'scp %(path)s%(db)s.%(date)s.sql %(remote_user)s@%(remote_host)s:%(remote_path)s' kv = {'path': PATH, 'date': CLEANSTR, 'db' : database, 'remote_user': REMOTE_USER, 'remote_host': REMOTE_HOST, 'remote_path': REMOTE_PATH} d = ('%(path)s%(db)s.%(date)s.sql' % kv ) if os.path.exists(d): m = "Start SCP {} database sql file...".format(db) log(m) excute( format % kv)def excute(cmd): os.system(cmd)def main(): print(''' -------------------------------------------- Transfer sql backup files -------------------------------------------- ''') thread_list = [] db_number = range(len(DBS)) for i in db_number: scp_t = Thread(target=scp_db_file, args=(DBS[i],)) thread_list.append(scp_t) for i in db_number: thread_list[i].start() for i in db_number: thread_list[i].join()if __name__ == "__main__": main()
清除到期sql文件脚本
#!/usr/bin/env pythonimport osimport datetimefrom threading import Threadfrom logs import logPATH = '/mnt/tantianran_mysql_backup/'NOW = datetime.datetime.now()NOWSTR = NOW.strftime('%Y%m%d')CLEANSTR = (NOW - datetime.timedelta(days=5)).strftime('%Y%m%d')DBS = [ 'urun_private_cloud', 'urun-asr', 'ambari', 'hive', 'oozie', 'storm-etl', 'urun-sms', 'analyzer-etl', 'zabbix', 'zabbix-key']def clean(db): database = '{dbs}'.format(dbs=db) format = 'rm -rf %(path)s%(db)s.%(date)s.sql' kv = {'path': PATH, 'date': CLEANSTR, 'db' : database} expiredDbFile = ('%(path)s%(db)s.%(date)s.sql' % kv) if os.path.exists(expiredDbFile): msg = 'Start Delete expired database file: %s' % (expiredDbFile) log(msg) excute( format % kv)def excute(cmd): os.system(cmd)def main(): thread_list = [] dbs = range(len(DBS)) for i in dbs: clean_t = Thread(target=clean, args=(DBS[i],)) thread_list.append(clean_t) for i in dbs: thread_list[i].start() for i in dbs: thread_list[i].join()if __name__ == "__main__": main()
日志模块
import loggingdef log(news): logging.basicConfig( level=logging.INFO, format='%(asctime)s %(levelname)s %(message)s', datefmt='%a, %d %b %Y %H:%M:%S', filename='global.log', filemode='a' ) logging.info(news)