import time
import datetime
import os
import shutilfrommonitor_logger import Loggerfrommonitor_falcon import Falcon
import traceback'''fromjin.cao
cd/etc/nginx && tar -zcf nginx_conf_bak_18.tgz conf.d/ stream.d/mv/etc/nginx/nginx_conf_bak_18.tgz /usr/local/bin/monitor/nginxbak/20190711/nginx_conf_bak_18.tgz'''log_file = u"monitor_backup_up.log"
classBackup():
def __init__(self, base_path, logger=None):
self.logger= logger if logger elseLogger(log_file).get_logger()
self.falcon=Falcon(self.logger)
self.base_path=base_path
def mkdir_files(self):
path_time= self.base_path + '/'+time.strftime('%Y%m%d') + '/'
ifnot os.path.exists(path_time):
os.makedirs(path_time)returnpath_time
def command(self, cmd):
os.popen(cmd)
def backup(self,path_time):try:
filename= 'nginx_conf_bak_%s.tgz'%(time.strftime('%H'))
cmd1= "cd /etc/nginx && tar -zcf" + filename + 'conf.d/ stream.d/'self.command(cmd1)
cmd2= "mv /etc/nginx/" + filename + ' '+path_time
self.command(cmd2)
except:
self.logger.error(traceback.format_exc())
def removedirs(self):
oldpath= (datetime.datetime.now() - datetime.timedelta(days=29)).strftime("%Y%m%d")
listpath=os.listdir(self.base_path)
tag=Truewhiletag:if len(listpath) > 29:for dir inlistpath:if oldpath >dir:
rmdir= base_path + '/'+dirtry:
shutil.rmtree(rmdir)
except:
self.logger.error(traceback.format_exc())
listpath.remove(listpath[0])else:
tag=Falseelse:
tag=False
def main(self):
path_time=self.mkdir_files()
self.backup(path_time)
self.removedirs()if __name__ == '__main__':
base_path= '/usr/local/bin/monitor/nginxbak'backup=Backup(base_path)
backup.main()