I have very smiple script to do a backup, and it is working fine when I run it from the command line, but I am having problems running it as cron job.
My cron entry is this: (running it every minute for testing atm)
* * * * * /var/hosting/ctest/ptest/backup-site.sh > /var/tmp/ctest_ptest_backup_cron2.log
And I can see in /var/log/cron that it gets executed
Sep 22 11:06:01 devbox CROND: (gwesio) CMD (/var/hosting/ctest/ptest/backup-site.sh > /var/tmp/ctest_ptest_backup_cron2.log)
But it's not doing what it is supposed to do, because backup archive is not created. This is how I have set permissions on the file:
-rwxr-xr-x 1 apache developers 877 2011-09-22 10:11 /var/hosting/ctest/ptest/backup-site.sh
And the script itself is this:
#!/bin/sh PATH=/usr/bin:/bin TIME=$(date +%Y-%m-%d-%H:%M) PDIR="/var/hosting/ctest/ptest" BACKUP_DIR="$PDIR/backups" BACKUP_FOLDER="$BACKUP_DIR/$(date +%Y-%m-%d-%H%M)" RESOURCES_DIR="$PDIR/management/html/resources" sudo mkdir $BACKUP_FOLDER sudo chown apache:developers $BACKUP_FOLDER sudo chmod g+w $BACKUP_FOLDER FILENAME="ctest_ptest-$(date +%Y-%m-%d-%H%M).sql" PTH="$BACKUP_FOLDER/$FILENAME" ARCHIVE_FILENAME="$BACKUP_DIR/ctest_ptest-$(date +%Y-%m-%d-%H%M).tgz" echo "Database backup script execution" sudo /usr/bin/mysqldump -uuser -ppassword -hlocalhost --databases ctest_ptest > $PTH echo "Database dump in $PTH" echo "Resources backup" sudo /bin/tar czvfP $ARCHIVE_FILENAME $BACKUP_FOLDER $RESOURCES_DIR > /dev/null echo "Backup file: $ARCHIVE_FILENAME" echo "Cleanup" sudo /bin/rm -r $BACKUP_FOLDER exit 0
I am running out of ideas where to look for any other indications of what could be wrong. I am trying to use absolute paths everywhere as it is suggested in other posts, and script can be executed without any problems from shell script.
I have tried to debug and commented out rm -r line, and noticed that $BACKUP_FOLDER is not being created in the first place, but I have no idea why. I also tried to change crontab to be su - user /var/hosting.... but it didn't help as well.
Maybe it's something obvious I'm missing here, or there are better ways of doing it? Any ideas?