我对我的代码有疑问,我想我可以在这里验证。我的要求是从两个不同的服务器复制 apache 日志和错误日志。Iv 使用 for 循环编写了一个 python 程序。
我的代码:
def copylogs(Appache,Errorlog, folder_prefix) :
root_path = '/home/tza/Desktop/LOGS/'
folders = ['Appache','Errorlog']
for folder in folders:
folder_name = folder_prefix + "_" + folder + str(int(time.time()))
mkdircmd = "mkdir -p " + root_path + "/" + folder_name
os.system(mkdircmd)
filePath = root_path + folder_name
serverPath = "/var/log/apache/*"
cmd = "scp " + "symentic@60.62.1.164:" + serverPath + " " + filePath
cmd = cmd.replace("60.62.1.164" ,myip1)
cmd = os.system(cmd)
print "Logs are at:",root_path+folder_name
time.sleep(10)
filePath = root_path + folder
serverPath = "/var/log/errorlog/*"
cmd = "scp " + "symentic@10.95.21.129:" + serverPath + " " + filePath
cmd = cmd.replace("10.95.21.129" ,myip2)
cmd = os.system(cmd)
print "Logs are at:",root_path+folder_name
现在我在程序结束时调用该函数:
folder_prefix = "Fail Case-1"
copylogs(Appache,Errorlog, folder_prefix)
我这里有个问题。程序成功执行,但日志被覆盖。我的意思是首先创建 Appache 文件夹,复制日志,然后再次被覆盖。
我需要的是:创建一个文件夹 Appachelogs [使用定义的时间戳],从机器 1 复制日志,然后从机器 2 复制错误日志,然后继续程序
如何做到这一点?