copying logs in python using the command line function

為{幸葍}努か 提交于 2019-12-13 08:07:12

问题


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.

My 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)

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 .

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?


回答1:


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

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.




回答2:


Consider using rsync instead of scp




回答3:


Do your logs have the same filenames on both machines? scp will overwrite them if they do.

Personally I would have two directories, one for each machine. Or use a timestamp as suggested by Sylar in his answer.



来源:https://stackoverflow.com/questions/11009308/copying-logs-in-python-using-the-command-line-function

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!