使用命令行功能蟒蛇复制日志 [英] copying logs in python using the command line function

查看:372
本文介绍了使用命令行功能蟒蛇复制日志的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个疑问,我的code,我认为我可以验证在这里。我的要求是到Apache日志和错误日志从两个不同的服务器上复制。 Ⅳ书面的python程序,用一个for循环。

I had a doubt with my code which i think I can verify here . My requirement is to copy the apache log and error log from two different servers . Iv written down a python program, using a for loop.

我的code:

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

现在林调用该函数在我的程序的结尾:

now Im calling the function at the end of my program :

folder_prefix = "Fail Case-1"
copylogs(Appache,Errorlog, folder_prefix)

我在这里有一个问题。 PROGRAMM成功执行,但该日志被覆盖。什么我的意思是第一Appache文件被创建,日志被复制,然后再一次它被覆盖。

I have an issue here . Programm executes successfully but the logs get overwritten .what i mean is first Appache folder gets created ,logs are copied and then once again it gets overwritten .

我的要求是:创建一个文件夹Appachelogs [与所定义的时间戳],日志从一台机器之一,下副本错误日志从机2复制,继续运行程序

What i require is : create a folder Appachelogs[with the timestamp as defined ] ,copy the logs from machine one , next copy error logs from machine2 , and continue the program

如何才能实现这一目标?

How can this be achieved?

推荐答案

如果一个相同的文件名在目标计算机中存在SCP默认覆盖。

scp by default overwrites if a same file name exists in the target computer.

我会建议使用一个错误的文件名+命名错误日志的时间戳的组合。它总是一个很好的约定日志的文件名中的时间戳,他们也prevent您所遇到的覆盖问题。

I would suggest using a combination of a error file name + the timestamp for naming the error logs. It's always a good convention for logs to have a timestamp in the name and they also prevent the overwriting problem you are experiencing.

这篇关于使用命令行功能蟒蛇复制日志的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆