QNAP 4.1.0 & Using own backup script with crontab

5.6k Views Asked by At

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: [email protected]" >> $MAILFILE
echo "From: [email protected]" >> $MAILFILE
echo "" >> $MAILFILE
/usr/bin/tail -13 /mnt/backup-cr/logs/rsync-cr.$bakdate.log >> $MAILFILE
echo "" >> $MAILFILE
echo "" >> $MAILFILE

cat $MAILFILE | ssmtp [email protected]
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 :-)

3

There are 3 best solutions below

1
On BEST ANSWER
  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/

0
On

Do not call your script something contain nc.

init.d/nc.sh would removed one line contain this letters from /etc/config/crontab every reboot system

There are a lot of pattern in many sh & init scripts that clean & write into crontab

You can find it by grep command by contain "/etc/config/crontab"

0
On

I solved a similar problem by calling the script with the sh command, as shown here:

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

EDIT: the sh command must be invoked with full path (i.e. /bin/sh) otherwise it won't work.