shellbackupshqnap

QNAP 4.1.0 & Using own backup script with crontab


i have a problem, executing my script by crontab on a qnap nas. it is very confusing, because other test scripts work AND executing this script manually works, too.

here is the script:

#!/bin/sh

[[ ! -d /mnt/backup-cr/daily.0 ]] && mount -t nfs -o nolock 192.168.178.2:/volume1/backup-cr /mnt/backup-cr

#1
[[ -d /mnt/backup-cr/daily.7 ]] && rm -rf /mnt/backup-cr/daily.7

#2
[[ -d /mnt/backup-cr/daily.6 ]] && mv /mnt/backup-cr/daily.6 /mnt/backup-cr/daily.7
[[ -d /mnt/backup-cr/daily.5 ]] && mv /mnt/backup-cr/daily.5 /mnt/backup-cr/daily.6
[[ -d /mnt/backup-cr/daily.4 ]] && mv /mnt/backup-cr/daily.4 /mnt/backup-cr/daily.5
[[ -d /mnt/backup-cr/daily.3 ]] && mv /mnt/backup-cr/daily.3 /mnt/backup-cr/daily.4
[[ -d /mnt/backup-cr/daily.2 ]] && mv /mnt/backup-cr/daily.2 /mnt/backup-cr/daily.3
[[ -d /mnt/backup-cr/daily.1 ]] && mv /mnt/backup-cr/daily.1 /mnt/backup-cr/daily.2

#3
[[ -d /mnt/backup-cr/daily.0 ]] && cp -al /mnt/backup-cr/daily.0 /mnt/backup-cr/daily.1

#4
bakdate=$(date +%Y%m%d%H%M)

/usr/bin/rsync -av \
        --stats \
        --delete \
        --human-readable \
        --log-file=/mnt/backup-cr/logs/rsync-cr.$bakdate.log \
        /share/cr/ \
        /mnt/backup-cr/daily.0 \

MAILFILE=rsync-cr.$bakdate.log.tmp

echo "Subject: rsync-log for cr from srv" > $MAILFILE
echo "To: x@x.com" >> $MAILFILE
echo "From: y@y.com" >> $MAILFILE
echo "" >> $MAILFILE
/usr/bin/tail -13 /mnt/backup-cr/logs/rsync-cr.$bakdate.log >> $MAILFILE
echo "" >> $MAILFILE
echo "" >> $MAILFILE

cat $MAILFILE | ssmtp x@x.com
rm $MAILFILE

And here is my crontab entry:

15 0 * * * /share/CACHEDEV1_DATA/.scripts/backup.sh

The script has the executable-flag, and as I said other scripts within the same folder works.

Does someone has an idea? Because if this works manually on QNAP and also works in crontab on another UBUNTU server, then I think I am getting dumb and paranoid :-)


Solution

    1. Use echo to store your command in the crontab file from the command line

      $ echo "1 4 * * * /bin/sh /share/CACHEDEV1_DATA/your-backup-folder/backup.sh" >> /etc/config/crontab

    This command will run backup.sh 4 minutes past 1 AM.

    1. To make the crontab persistent during reboot, you have to execute this command

      $ crontab /etc/config/crontab

    Please note that you cannot save the script in /etc/, or /bin/ or some other directory outside of your HDD directories. In other words, always save your script in /share/CACHEDEV1_DATA/your-backup-folder. If you don’t, the script will be deleted upon reboot.

    1. Restart crontab

      $ /etc/init.d/crond.sh restart

    2. Set correct permissions

      chmod +x /share/CACHEDEV1_DATA/your-backup-folder/backup.sh

    Wait for the cron to run, and see if it works

    For the full guide, please visit: https://www.en0ch.se/qnap-and-cron/