CronTab中的.sh文件无效

时间:2017-06-28 17:06:43

标签: shell

我从网上获得了以下脚本。我改变了一些东西(函数init()有我的详细信息,并且在每个函数之前都写了函数,我删除了)但是现在它不能用于crontab。它在终端上运行良好。

crontab的:

30 2 * * * bash /var/www/html/final.sh

.sh文件:

 #!/bin/bash

main(){ init chkBackupDir }

init() { BACKUP_DIR="/var/www/html/backups" DB_USER="wordpressU" DB_PASSWORD="wordpressP" DB_NAME="wordpress" DOC_ROOT="/var/www/html/wordpress" DAYS_TO_KEEP_BACKUP="5" }

chkBackupDir() { if [ ! -d "$BACKUP_DIR" ]; then mkdir "$BACKUP_DIR" fi bakupDocRoot }

bakupDocRoot() { tar -cjf "$BACKUP_DIR"/doc_root_backup-$(date
+%d-%m-%y).tar.bz2 --directory="$DOC_ROOT" . if [[ -e "$BACKUP_DIR"/doc_root_backup-$(date +%d-%m-%y).tar.bz2 ]]; then bakupDatabase else echo "Backup failed for document root on your server. Please check manually" fi }

bakupDatabase() { `which mysqldump` -u "$DB_USER" -p"$DB_PASSWORD" "$DB_NAME" > "$BACKUP_DIR"/"$DB_NAME"-$(date +%d-%m-%y).sql | tee "$BACKUP_DIR"/backup.log tar -cjf "$BACKUP_DIR"/"$DB_NAME"-$(date
+%d-%m-%y).tar.bz2 "$BACKUP_DIR"/"$DB_NAME"-$(date +%d-%m-%y).sql --remove-files 2> $BACKUP_DIR/tar.log if [[ -e "$BACKUP_DIR"/"$DB_NAME"-$(date +%d-%m-%y).tar.bz2 ]]; then createTar else echo "Backup failed for database on your server. Please check manually"  fi }

createTar() { cd "$BACKUP_DIR" tar -cf backup-$(date +%d-%m-%y).tar doc_root_backup-$(date +%d-%m-%y).tar.bz2 "$DB_NAME"-$(date
+%d-%m-%y).tar.bz2 --remove-files 2> final.log deleteOldBackup }

deleteOldBackup() { `which find` "$BACKUP_DIR"/ -mtime
+"$DAYS_TO_KEEP_BACKUP" -exec rm {} \; >> "$BACKUP_DIR"/delete.log }

main  # End

请问解决方案是什么?

0 个答案:

没有答案