# -*- coding:utf-8 -*-
# __author__ = "DT"
# __Date__ = "2019/03/18"
import os
import os.path
import shutil
import time
import datetime
import paramiko
import sys
import subprocess
reload(sys)
sys.setdefaultencoding('utf-8')
# 获取此次备份的日期目录
def get_copyfile_name(path):
now_time = datetime.datetime.now().strftime('%Y%m%d')
file_list = os.listdir(path)
i = 1
while now_time in file_list:
now_time = now_time[0:8] + "_" + str(i)
i += 1
else:
now_time = now_time
# print(now_time)
new_path = path + "/" + now_time
return new_path
# 旧的获取日期目录代码,
# 存在Bug,当根目录中存在日期目录,
# 用户手动创建日期+其他字符文件夹时,
# 会把文件备份到之前存在的日期目录中
# def get_copyfile_name(path):
# now_time = datetime.datetime.now().strftime('%Y%m%d')
# file_lists = os.listdir(path)
# file_lists.sort(key=lambda fn: os.path.getatime(path + "/" + fn))
# file_last = file_lists[-1]
# path_new = os.path.join(path, file_lists[-1])
#
# if len(file_last) == 10:
# if file_last[0:8] == now_time:
# new_file_last = file_last[0:9] + str(int(file_last[-1]) + 1)
# else:
# new_file_last = now_time
#
# elif len(file_last) == 8:
# if file_last == now_time:
# new_file_last = file_last + "_1"
# else:
# new_file_last = now_time
#
# else:
# new_file_last = now_time
#
# new_path = path + "/" + new_file_last
# # os.makedirs(new_path)
# # print(new_path + "创建成功")
# return new_path
# 创建此次备份的结构目录
def mkdir(new_path, cs_path): # 创建此次备份的结构目录
all_path = new_path + "/" + cs_path
folder = os.path.exists(all_path)
if not folder:
os.makedirs(all_path)
else:
print(all_path + "目录已存在")
# print all_path
return all_path
# 编写更新记录
def write_updates(new_path):
update_files = (new_path + "/" + "更新内容.txt").decode('utf-8')
ls = os.linesep
while True:
fname = update_files
if os.path.exists(fname):
print("\n" + fname + "\n\033[1;35m已经存在请检查,并手动记录更新内容\033[0m")
sys.exit()
else:
break
all = []
print("\n请输入更新内容,'.'结束输入")
while True:
entry = raw_input('>')
if entry == '.' or entry == '。':
break
else:
all.append(entry)
fobj = open(update_files, 'w')
for x in all:
fobj.writelines(['%s%s' % (x, ls)])
fobj.close()
print("更新记录已经完成,备份结束")
# 下载文件(用于备份服务器版本)
def download_file(host, user, pwd, from_path, to_path, cmd, cmd1):
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(hostname=host, port=22, username=user, password=pwd, timeout=20)
stdin, stdout, stderr = ssh.exec_command("cd %s && %s" % (from_path, cmd))
stdin1, stdout1, stderr1 = ssh.exec_command("cd %s && %s" % (from_path, cmd1))
# file_list = stdout.readlines()
file_list = []
for fn in stdout.readlines():
file_list.append(fn.strip())
information_list = []
for f_information in stdout1.readlines():
information_list.append(f_information.strip())
print("\n检查\033[1;36m服务器\033[0m备份文件是否正确")
a = 1
for i in information_list:
print(str(a) + " " + i.decode('utf-8'))
a += 1
while True:
inputExit = raw_input("\n[y:继续][n:退出][s:跳过]:")
if not len(inputExit):
continue
elif inputExit == "y" or inputExit == "Y":
tran = paramiko.Transport((host, 22))
tran.connect(username=user, password=pwd)
sftp = paramiko.SFTPClient.from_transport(tran)
for file_name in file_list:
from_path_all = from_path + "/" + file_name.strip().decode('utf-8')
if os.path.isdir(from_path_all):
print(from_path_all + "是文件夹不需要备份")
else:
to_path_all = to_path + "/" + file_name.strip().decode('utf-8')
sftp.get(from_path_all, to_path_all)
print(file_name.strip() + " 备份成功")
tran.close()
break
elif inputExit == "n" or inputExit == "N":
print("结束")
sys.exit()
elif inputExit == "s" or inputExit == "S":
print("\033[1;35m服务器备份跳过\033[0m")
break
else:
print("输入有误,请重新输入")
# continue
ssh.close()
# 拷贝文件(用于备份客户端版本)
def copy_file(from_path, to_path, pack_type):
cmd = "ls -rt zt_green_2* | tail -2"
cmd1 = "ls -l -rt zt_green_2* | tail -2"
os.chdir(from_path)
p = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE)
p1 = subprocess.Popen(cmd1, shell=True, stdout=subprocess.PIPE)
out = p.stdout.readlines()
out1 = p1.stdout.readlines()
client_file_list = []
for line in out:
client_file_list.append(line.strip())
client_information_list = []
for line1 in out1:
client_information_list.append(line1.strip())
print("\n检查\033[1;35m%s\033[0m 备份文件是否正确" % pack_type)
a = 1
for i in client_information_list:
print(str(a) + " " + i.decode('utf-8'))
a += 1
while True:
inputExit = raw_input("\n[y:继续][n:退出][s:跳过]:")
if not len(inputExit):
continue
elif inputExit == "y" or inputExit == "Y":
for f in client_file_list:
all_client_from_path = from_path + "/" + f.strip().decode('utf-8')
if os.path.isdir(all_client_from_path):
print(all_client_from_path + " 是文件夹不需要备份")
else:
all_client_to_path = to_path + "/" + f.strip().decode('utf-8')
shutil.copyfile(all_client_from_path, all_client_to_path)
print(f.strip().decode('utf-8') + " 备份成功")
break
elif inputExit == "n" or inputExit == "N":
print("结束")
sys.exit()
elif inputExit == "s" or inputExit == "S":
print("\033[1;35m%s备份跳过\033[0m" % pack_type)
break
else:
print("输入有误,请重新输入")
def main():
root_path = "/Volumes/tmp/0自走棋版本备份"
# 检查备份目录是否存在
if os.path.exists(r"%s" % root_path):
print("备份盘符正常存在")
else:
print("备份盘符不存在,请检查")
sys.exit()
# 获取此次备份的目录
copy_path = get_copyfile_name(r"%s" % root_path)
# 创建目录结构
client_ios_path = mkdir(copy_path, "client/ios")
client_android_path = mkdir(copy_path, "client/android")
server_path = mkdir(copy_path, "server")
# 下载服务器备份文件
download_file('192.168.181.205',
'sealgull',
'ZTJR123$%^@6',
'/home/sealgull/FirServer/branches_sealchess/branche_sealchess_20190401_release/dist',
server_path,
'ls -rt zt_* | tail -4',
'ls -rt zt_* du -sh| tail -4')
# 备份客户端版本
from_path_android = "/Users/zt-2207096/Documents/zt-game/FirClient/branches_sealchess/branche_sealchess_20190401_release/patch/android/business_pb"
to_path_android = client_android_path
from_path_ios = "/Users/zt-2207096/Documents/zt-game/FirClient/branches_sealchess/branche_sealchess_20190401_release/patch/ios/business_pb"
to_path_ios = client_ios_path
# android客户端版本备份
copy_file(from_path_android, to_path_android, "android")
# ios客户端版本备份
copy_file(from_path_ios, to_path_ios, "ios")
# 编写更新内容
write_updates(copy_path)
if __name__ == "__main__":
main()
version_copy.py
最新推荐文章于 2023-05-26 10:22:05 发布