安排CronJob /备份和删除

时间:2014-02-28 15:18:14

标签: cron export scheduled-tasks

我正在尝试创建一个具有以下细节的cronjob;导出为每周运行并转储超过3周的文件将从路径中删除。请帮助您创建示例脚本。

1 个答案:

答案 0 :(得分:0)

脚本(未经测试)

#!/bin/bash

# Set path to backup directory
BK_PATH = /path/to/backups/

# If 3 is there remove it
if [ -f "${BK_PATH}3.tgz" ]
then
  rm -rf ${BK_PATH}3.tgz
fi

# If 2 is there, it becomes 3
if [ -f "${BK_PATH}2.tgz" ]
then
  mv ${BK_PATH}2.tgz ${BK_PATH}3.tgz
fi

# If 1 is there, it becomes 2
if [ -f "${BK_PATH}1.tgz ]
then
  mv ${BK_PATH}1.tgz ${BK_PATH}2.tgz
fi

# Create 1.
tar czvf ${BK_PATH}1.tgz $1

然后,在你的crontab中:

0 0 * * 6 /path/to/the/script /path/the/thing/you/want/backedup