#coding=utf8
import os
import shutil
import time
import ftplib
import sys
import subprocess
import tarfile
import datetime
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
sender = ' ' #发件人邮箱登录账号
sender_pass = ' ' #发件人邮箱登录密码
success_receiver = [' '] #成功时收件人邮箱账号
fail_receiver = [' '] #失败时收件人邮箱账号
ftp_host = ' ' #ftp地址
src_dir = ' ' #FTP的子目录
ftp_user=' ' #ftp登录账号
ftp_passwd=' ' #ftp登录密码
A = " " #源文件路径
B = " " #要保存的文件路径
log_path=os.path.join(B,'move_log_{}.log'.format(time.strftime("%Y-%m-%d", time.localtime(time.time()))))
exec_log=os.path.join(B,'exec_log.log')
success_file_list=os.path.join(B,'success_file_list.txt')
if not os.path.exists(B):
os.makedirs(B)
def get_time(path,set_day):
day = time.gmtime(os.path.getmtime(path))
# print day
#print time.strftime("%Y-%m-%d", day)
day_time= time.mktime(day)
set_time= time.mktime(time.strptime(set_day,'%Y-%m-%d'))
today = datetime.date.today()
today=time.mktime(time.strptime(str(today), '%Y-%m-%d'))
if day_time>=set_time and day_time<today:
return True
else:
return False
def run_cmd(cmd, cwd=None, runas=None):
if not sys.platform.startswith('win') and runas and runas != 'root':
cmd = 'su - {} -c "{}"'.format(runas, cmd)
proc = subprocess.Popen(cmd,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
shell=True,
cwd=cwd)
return proc
def paths(path,dest,set_day):
global log_path,success_file_list
try:
src_dir=''
for dirpath, dirnames, filenames in os.walk(path):
file_logs = []
if not src_dir:
src_dir=dirpath
dest_dir = os.path.join(dest, dirpath.split(src_dir)[1].lstrip('/'))
if not os.path.exists(dest_dir):
os.makedirs(dest_dir)
file_logs.append('copy dir {}->{}
'.format(src_dir, dest_dir))
level = dirpath.replace(path, '').count(os.sep)+1
dir_indent = "| " * (level - 1) + "|-- "
with open(success_file_list, 'a') as f:
f.write('{}{}
'.format(dir_indent, os.path.basename(dirpath)))
for file in filenames:
fullpath = os.path.join(dirpath, file)
dest_path = dest.rstrip('/') +fullpath.split(src_dir)[1]
if not os.path.exists(dest_dir):
os.makedirs(dest_dir)
if os.path.isdir(fullpath):
if not os.path.exists(dest_path):
os.makedirs(dest_path)
else:
if get_time(fullpath, set_day):
if not os.path.exists(dest_path):
file_logs.append('move {}->{}
'.format(fullpath, dest_path))
file_indent = "| " * level + "|-- "
with open(success_file_list, 'a') as f:
f.write(('{}{}'.format(file_indent, file)))
shutil.copy(fullpath,dest_path)
else:
file_logs.append('time pass {}->{}
'.format(fullpath, dest_path))
if os.path.exists(log_path):
with open(log_path,'a') as f:
f.writelines(file_logs)
else:
with open(log_path,'w') as f:
f.writelines(file_logs)
except Exception as e:
print (e)
sys.exit(1)
def up_ftp(file_name,file_path):
global src_dir
try:
f = ftplib.FTP(ftp_host)
except ftplib.error_perm:
print('无法连接到"%s"' % ftp_host)
return
print('连接到"%s"' % ftp_host)
try:
f.login(ftp_user,ftp_passwd)
except ftplib.error_perm:
print('登录失败')
f.quit()
return
print('登陆成功')
try:
f.cwd(src_dir)
except:
f.mkd(src_dir)
f.cwd(src_dir)
try:
bufsize = 1024
fp = open(file_path, 'rb')
print ('开始传输文件',file_name)
f.storbinary('STOR %s' % file_name, fp, bufsize)
print ('传输完毕')
f.set_debuglevel(0)
fp.close()
except ftplib.error_perm:
print('传输失败')
return False
else:
print('文件上传完毕!')
f.quit()
return True
def tar(fnames,filename,realdir):
t = tarfile.open(filename, "w:gz")
for fname in fnames:
for root, dir, files in os.walk(fname):
for file in files:
fullpath = os.path.join(root, file)
t.add(fullpath,arcname=fullpath.split(realdir)[1])
t.close()
def send_mail(sender, sender_pass, receiver,content,file=None):
SMTP_SERVER = 'mail.broada.com'
message = MIMEMultipart()
message.attach(MIMEText(content , "plain", "utf-8"))
message['from'] = sender
message['to'] = receiver
message['subject'] = 'maven仓库备份结果'.decode('utf8')
if file:
att1 = MIMEText(open(success_file_list, 'rb').read(), 'base64', 'utf-8')
att1["Content-Type"] = 'application/octet-stream'
att1["Content-Disposition"] = 'attachment; filename="success_file_list.txt"'
message.attach(att1)
server = smtplib.SMTP_SSL(SMTP_SERVER, 465)
server.set_debuglevel(1)
server.login(sender, sender_pass)
server.sendmail(from_addr=sender, to_addrs=[receiver], msg=message.as_string())
server.quit()
print("邮件发送结束")
if __name__ == '__main__':
print ('开始执行备份')
if not os.path.exists(exec_log):
set_day='2019-04-29'
else:
with open(exec_log, 'r') as f:
content=f.read()
if 'end time ' in content:
set_day = content.split('end time ')[1].strip()
else:
set_day = '2019-04-29'
print('上一次执行日期'.decode('utf8'),set_day)
fnames=[]
if os.path.exists(success_file_list):
os.remove(success_file_list)
with open(success_file_list, 'w') as f:
f.write('.
')
dir_lists=['proxy-aliyun','public','releases','thirdparty']
for i in dir_lists:
if os.path.exists(os.path.join(B,i)):
shutil.rmtree(os.path.join(B,i))
paths(os.path.join(A,i),os.path.join(B,i),set_day)
fnames.append(os.path.join(B,i))
print ('备份完成,开始打包')
new_name='storage_{}.tar.gz'.format(time.strftime("%Y%m%d", time.localtime(time.time())))
new_name_path=os.path.join(B,new_name)
tar(fnames, new_name_path,B)
print ('打包完成,开始上传ftp')
res=up_ftp(new_name,new_name_path)
if res:
for i in success_receiver:
send_mail(sender, sender_pass, i,"执行成功!".decode('utf8'),file=success_file_list)
with open(exec_log, 'w') as f:
f.write('start time {},end time {}
'.format(set_day,time.strftime("%Y-%m-%d", time.localtime(time.time()))))
else:
for i in fail_receiver:
send_mail(sender, sender_pass, i,"执行失败!".decode('utf8'))