简体   繁体   English

通过Cron作业将Python脚本备份到目录

[英]Python script to backup directory via Cron job

I am trying to run the code below as cronjob without any luck... 我正在尝试将下面的代码作为cronjob运行而没有任何运气...

import sys
import time
import tarfile

def main(argv):
    #f = open('/tmp/backup-log.txt', 'a')
    #f.write('variable start\n')
    timeStamp = time.strftime('%y%m%d')
    nagiosFolder = '/app/nagios/'
    fileName = '/app/nagios_install/backup/nagios-backup-%s.tar.gz' % timeStamp
    #f.write('variable end\n')

    try:
        #f.write('tar start\n')
        tarGeza = tarfile.open(fileName, 'w:gz')
        tarGeza.add(nagiosFolder)
        tarGeza.close()
        #f.write('tar end\n')
        #f.close()
        sys.exit(0)
    except tarfile.TarError, tarexc:
        #f.write('exception error')
        #f.close()
        print tarexc
        sys.exit(1)

if __name__ == '__main__':
    main(sys.argv[1:])

The commented sections are for debbuging purposes and whenever the code runs, it shows as the code has finished without errors: 带注释的部分用于调试目的,每当代码运行时,它就会显示代码完成而没有错误:

variable start
variable end
tar start
tar end

My crontab settings are: 我的crontab设置是:

HOME=/usr/nagios/
LOGNAME=nagios
PATH=/usr/lib64/qt-.3/bin:/usr/local/bin:/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/sbin:/usr/bin/python
SHELL=/usr/bin/python

17 12 * * * /usr/bin/python /app/nagios_install/backup/nagios_backup.py

And permissions are the following: 权限如下:

-rwxrwxr-x 1 nagios root 1009 Jan 17 11:00 /app/nagios_install/backup/nagios_backup.py

Can anyone please highlight what I might be doing wrong? 谁能强调我可能做错了什么? Thanks in advance! 提前致谢!

Just some tips from my end - how I would try to schedule the whole thing 我最后的一些技巧-我将如何安排整个事情

1) Include a shebang line at the top of the python script for picking up the python executable and remove the python executable path from the cron entry - there is always the off chance that the paths for the executables are different on productions server from development environment. 1)在python脚本的顶部包括一个shebang行,用于拾取python可执行文件并从cron条目中删除python可执行文件路径-始终不可能在生产服务器上将可执行文件的路径与开发环境上的不同。

#!/usr/bin/env python

2) Change the mode of the script to 755 to make the script executable 2)将脚本的模式更改为755以使脚本可执行

sudo chmod 755 /app/nagios_install/backup/nagios_backup.py

3) Schedule the cron job from the root user's crontab 3)从root用户的crontab安排cron作业

sudo crontab -e

crontab -e opens up current user's crontab by default, and not root user's crontab. crontab -e默认情况下打开当前用户的crontab,而不打开root用户的crontab。 And the nagios directory may not be acceessible by that current user. 该当前用户可能无法访问nagios目录。

4) Remove the SHELL variable from your crontab, it isn't needed in the first place. 4)从您的crontab中删除SHELL变量,首先不需要它。 You are not using variables LOGNAME , HOME either, so they can also be removed, I don't think they were needed either. 您也没有使用变量LOGNAMEHOME ,因此也可以将其删除,我也不认为它们是必需的。

5) Schedule the cron job like following in your root user's crontab 5)像在根用户的crontab中一样安排cron作业

17 12 * * * /app/nagios_install/backup/nagios_backup.py >> /var/log/nagios_backup.log 2>&1 

I think the above setup should work. 我认为上述设置应该可以工作。 If it doesn't, try running the script directly and let me know what error(s) it throws. 如果不是,请尝试直接运行脚本,让我知道它引发了什么错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM