1

我正在开发 django-python 项目并坚持使用 django-crontab 进行自动文件备份,这是代码:

在我的 settings.py

INSTALLED_APPS = (
.
.
.
'django-crontab',
)

CRONTAB_DJANGO_MANAGE_PATH = '/home/path/to/my/project/manage.py'

CRONJOBS = [
            ('25 15 * * *', 'cd /home/path/to/my/project && /usr/bin/sh myscript.sh'),
        ]

在 myscript.sh

#!/bin/sh
set -e
cd /home/path/to/my/project/
exec cp file some_dir/copy_file

我在我的服务器上测试这段代码在 15:25 点运行,它没有执行 myscript.sh

该脚本只是将文件复制到另一个目录(用于备份),我已经在服务器中使用 ./myscript.sh 手动测试,脚本运行良好......

我一直在尝试各种方法,但似乎没有任何效果

我从 django-crontab ( https://pypi.python.org/pypi/django-crontab/0.5.1 ) 和这个网站 ( https://superuser.com/questions/81262/how-to-execute -shell-script-via-crontab ) + http://github.com/kraiz/django-crontab

编辑

我也使用此命令添加了我的 cronjob

python manage.py crontab add

我已经使用此命令检查了工作是否存在

python manage.py crontab show

工作示例

设置.py

CRONJOBS = [
            # Auto Backup Database everyday at 4 am
            ('0 4 * * *', 'django.core.management.call_command', ['dumpdata', 'myproject'], {'indent': 4, 'use_natural_keys': True}, '> /home/mydirectory/myproject_backup/myproject_backup.json'),
        ]

myproject_backup.json

[
{
    "pk": 1, 
    "model": "contenttypes.contenttype", 
    "fields": {
        "model": "permission", 
        "name": "permission", 
        "app_label": "auth"
    }
},
{
    "pk": 2, 
    "model": "contenttypes.contenttype", 
    "fields": {
        "model": "group", 
        "name": "group", 
        "app_label": "auth"
    }
},
{
    "pk": 3, 
    "model": "contenttypes.contenttype", 
    "fields": {
        "model": "user", 
        "name": "user", 
        "app_label": "auth"
    }
},
{
    "pk": 4, 
    "model": "contenttypes.contenttype", 
    "fields": {
        "model": "contenttype", 
        "name": "content type", 
        "app_label": "contenttypes"
    }
},
{
    "pk": 5, 
    "model": "contenttypes.contenttype", 
    "fields": {
        "model": "session", 
        "name": "session", 
        "app_label": "sessions"
    }
},
{
    "pk": 6, 
    "model": "contenttypes.contenttype", 
    "fields": {
        "model": "site", 
        "name": "site", 
        "app_label": "sites"
    }
},
{
    "pk": 7, 
    "model": "contenttypes.contenttype", 
    "fields": {
        "model": "logentry", 
        "name": "log entry", 
        "app_label": "admin"
    }
},
{
    "pk": 8, 
    "model": "contenttypes.contenttype", 
    "fields": {
        "model": "poll", 
        "name": "poll", 
        "app_label": "polls"
    }
},
{
    "pk": 9, 
    "model": "contenttypes.contenttype", 
    "fields": {
        "model": "choice", 
        "name": "choice", 
        "app_label": "polls"
    }
},
{
    "pk": 10, 
    "model": "contenttypes.contenttype", 
    "fields": {
        "model": "cronjoblog", 
        "name": "cron job log", 
        "app_label": "django_cron"
    }
},
{
    "pk": "1g6s3bly0gu6pn7spllerdw1n8sus08a", 
    "model": "sessions.session", 
    "fields": {
        "expire_date": "2013-09-04T05:48:32Z", 
        "session_data": "YTg4Y2I3OWM3Nzg4NmIyOGE2NmJiZDAxYmYwMGNiYWI3Zjg5OGFmMDqAAn1xAShVEl9hdXRoX3VzZXJfYmFja2VuZHECVSlkamFuZ28uY29udHJpYi5hdXRoLmJhY2tlbmRzLk1vZGVsQmFja2VuZHEDVQ1fYXV0aF91c2VyX2lkcQSKAQF1Lg=="
    }
},
{
    "pk": "j7ifcyfzfhentb5tx9492fphdf20g2qp", 
    "model": "sessions.session", 
    "fields": {
        "expire_date": "2013-09-11T04:54:37Z", 
        "session_data": "YTg4Y2I3OWM3Nzg4NmIyOGE2NmJiZDAxYmYwMGNiYWI3Zjg5OGFmMDqAAn1xAShVEl9hdXRoX3VzZXJfYmFja2VuZHECVSlkamFuZ28uY29udHJpYi5hdXRoLmJhY2tlbmRzLk1vZGVsQmFja2VuZHEDVQ1fYXV0aF91c2VyX2lkcQSKAQF1Lg=="
    }
},
{
    "pk": "m21on2le3kti3ezexzei1vpox57x8qgv", 
    "model": "sessions.session", 
    "fields": {
        "expire_date": "2013-09-05T06:29:00Z", 
        "session_data": "YTg4Y2I3OWM3Nzg4NmIyOGE2NmJiZDAxYmYwMGNiYWI3Zjg5OGFmMDqAAn1xAShVEl9hdXRoX3VzZXJfYmFja2VuZHECVSlkamFuZ28uY29udHJpYi5hdXRoLmJhY2tlbmRzLk1vZGVsQmFja2VuZHEDVQ1fYXV0aF91c2VyX2lkcQSKAQF1Lg=="
    }
},
{
    "pk": "x16t71c19o93kukwu5mvog1g76ynv3ip", 
    "model": "sessions.session", 
    "fields": {
        "expire_date": "2013-09-04T07:07:46Z", 
        "session_data": "YTg4Y2I3OWM3Nzg4NmIyOGE2NmJiZDAxYmYwMGNiYWI3Zjg5OGFmMDqAAn1xAShVEl9hdXRoX3VzZXJfYmFja2VuZHECVSlkamFuZ28uY29udHJpYi5hdXRoLmJhY2tlbmRzLk1vZGVsQmFja2VuZHEDVQ1fYXV0aF91c2VyX2lkcQSKAQF1Lg=="
    }
},
{
    "pk": 1, 
    "model": "sites.site", 
    "fields": {
        "domain": "example.com", 
        "name": "example.com"
    }
},
{
    "pk": 1, 
    "model": "polls.poll", 
    "fields": {
        "pub_date": "2013-08-21T05:56:00Z", 
        "question": "What are you doing?"
    }
},
{
    "pk": 2, 
    "model": "polls.poll", 
    "fields": {
        "pub_date": "2013-08-21T05:58:00Z", 
        "question": "What's on your mind?"
    }
},
.
.
]

如果有人可以提供帮助,我将不胜感激:S

4

3 回答 3

1

在您运行之前,您在设置中安排的 cronjobs 实际上不会添加到 crontab 中python manage.py crontab add

热电阻

于 2013-08-26T12:38:22.173 回答
1

查看django crontab的格式:

#format 1
required: cron timing (in usual format: http://en.wikipedia.org/wiki/Cron#Format)
required: the python module path to the method
optional: a job specific suffix (f.e. to redirect out/err to a file, default: '')

#format 2
required: cron timing (in usual format: http://en.wikipedia.org/wiki/Cron#Format)
required: the python module path to the method
optional: list of positional arguments for the method (default: [])
optional: dict of keyword arguments for the method (default: {})
optional: a job specific suffix (f.e. to redirect out/err to a file, default: '')

在这两种情况下,您都需要使用python 方法,而不仅仅是来自 shell 的命令。你不能写像'cd ...; 。/命令'; 您应该将此行包装到代码中的 python 模块中。

更多关于它:

于 2013-08-28T03:29:48.810 回答
0

终于在拔掉头发几天后,我找到了可行的解决方案。

  1. 我使用腻子(SSH)登录到服务器
  2. 键入命令:crontab -e
  3. 您将看到 crontab 作业列表
  4. 直接添加我的工作,我输入:

    0 3 * * * cp -pRu /home/path/to/my/project/file_dir /home/path/to/my/backup/dir

这只会在每天凌晨 3 点根据 file_dir 中的时间戳将新文件/目录复制到 backup_dir

  1. 输入完成后,按 ESC,然后 CTRL+x,它会问你要保存,只需输入 Y 然后按 ENTER 保存文件。
  2. 按类型检查所有 crontab 作业:crontab -l
  3. 而已!干杯人!

希望它可以帮助遇到类似问题的人:D

PS:也感谢 Igor 和 zsquare。

于 2013-08-28T16:58:11.950 回答