Backup script runs but doesnt work as a Scheduled Job

In my scheduled task tab i have

# run backup midnight and noon

0 0,12 * * *   /mnt/usb1/shareroot/software/openwrt/flint2/scripts/backup_with_email.sh

the backup job itself is a combination of a few scripts

backup_with_email.sh:
#!/bin/sh
LOG_FILE="/mnt/usb1/shareroot/software/openwrt/flint2/scripts/backuplog.log"
START=$(date +%s)

# Use full path to ensure it runs regardless of where you call the script from
/mnt/usb1/shareroot/software/openwrt/flint2/scripts/backup_with_log.sh

END=$(date +%s)

# 1. Added "$" to variable: $(cat "$LOG_FILE")
# 2. Used printf for reliable newlines (\n) in sh
# 3. Quoted variables to handle potential spaces
printf "Subject: Skittles backup finished in $((END-START)) seconds\n\nLOG:\n$(cat "$LOG_FILE")" | sendmail -t og@hotmail.com

backup_with_log.sh:

LOG_FILE="/mnt/usb1/shareroot/software/openwrt/flint2/scripts/backuplog.log"
{
./backup.sh
}> ${LOG_FILE}

backup.sh:

echo "starting backup "$(date)  
echo "/mnt/usb1/shareroot/ TO /mnt/usb2/shareroot "$(date)  
rsync -avhzz --delete /mnt/usb1/shareroot/ /mnt/usb2/shareroot/
echo "/etc/ TO /mnt/usb2/openwrt_backup/etc "$(date)  
rsync -avhzz --delete /etc/ /mnt/usb2/openwrt_backup/etc/
echo "/var/ TO /mnt/usb2/openwrt_backup/etc "$(date)  
rsync -avhzz --delete /var/ /mnt/usb2/openwrt_backup/var/
echo "/mnt/usb1/minidlna/ TO /mnt/usb2/minidlna "$(date)  
rsync -avhzz --delete /mnt/usb1/minidlna/ /mnt/usb2/minidlna/
echo "/mnt/usb1/torrent/ TO /mnt/usb2/torrent/ "$(date) 
rsync -avhzz --delete /mnt/usb1/torrent/ /mnt/usb2/torrent/
echo "/mnt/usb1/gitrepos/ TO /mnt/usb2/gitrepos/ "$(date)  
rsync -avhzz --delete /mnt/usb1/gitrepos/ /mnt/usb2/gitrepos/
echo "finished backup "$(date)  

when i run this within an ssh session, or even in the Custom Commands tab it runs properly. I see the log message of what new changes where synced and the email sends the log in the body!!!

however as a Scheduled Job the log is completely empty as well as the body of the email and things are NOT synced.

permissions:

root@skittles:/mnt/usb1/shareroot/software/openwrt/flint2/scripts# ll
drwxrwxrwx 4 root root 4096 Jul 9 2025 ./
drwxrwxrwx 3 root root 4096 Dec 7 2023 ../
-rwxrwxrwx 1 root root 777 Jul 15 2025 backup.sh*
-rwxrwxrwx 1 root root 552 Mar 3 00:08 backup_with_email.sh*
-rwxrwxrwx 1 root root 106 Jan 4 2024 backup_with_log.sh*
-rwxrwxrwx 1 root root 0 Mar 9 12:00 backuplog.log*

runtime env issue?

#!/bin/sh /etc/rc.common
2 Likes

should i update all the sh files or just the starting one (backup_with_email.sh) ?

Please stop spreading chadbot halluconations.
Try to fix your script without chatbot "ssistance"
firstly:
date command is always in /bin
ref https://refspecs.linuxfoundation.org/FHS_3.0/fhs/ch03s04.html

secondly:
rsync between two usb sticks is stupid 2x slower than cp -R - it reads and checksums destination files. You could use git or orher RCS where destination files are alr#ady checksummed.

end result/fix

no context because ppl want to complain about ai help

backup.sh

#!/bin/sh
# Ensure the script knows where to look
export PATH="/usr/sbin:/usr/bin:/sbin:/bin"

echo "Starting backup: $(date)"
echo "Syncing USB1 to USB2..."

# Added --quiet or --stats if you want cleaner logs, 
# but keeping your original flags for now.
rsync -avhzz --delete /mnt/usb1/shareroot/ /mnt/usb2/shareroot/
rsync -avhzz --delete /etc/ /mnt/usb2/openwrt_backup/etc/
rsync -avhzz --delete /var/ /mnt/usb2/openwrt_backup/var/
rsync -avhzz --delete /mnt/usb1/minidlna/ /mnt/usb2/minidlna/
rsync -avhzz --delete /mnt/usb1/torrent/ /mnt/usb2/torrent/
rsync -avhzz --delete /mnt/usb1/gitrepos/ /mnt/usb2/gitrepos/

echo "Finished backup: $(date)"


backup_with_log.sh

#!/bin/sh
export PATH="/usr/sbin:/usr/bin:/sbin:/bin"
LOG_FILE="/mnt/usb1/shareroot/software/openwrt/flint2/scripts/backuplog.log"
SCRIPT_PATH="/mnt/usb1/shareroot/software/openwrt/flint2/scripts/backup.sh"

{
  # Calling the script by its full path
  /bin/sh "$SCRIPT_PATH"
} > "${LOG_FILE}" 2>&1


backup_with_email.sh

#!/bin/sh
# This line tells the script where to find 'date', 'rsync', and 'sendmail'
export PATH="/usr/sbin:/usr/bin:/sbin:/bin"

LOG_FILE="/mnt/usb1/shareroot/software/openwrt/flint2/scripts/backuplog.log"
# No need for /usr/bin/ here; the PATH export above handles it
START=$(date +%s)

# Use the FULL path to the next script
/bin/sh /mnt/usb1/shareroot/software/openwrt/flint2/scripts/backup_with_log.sh

END=$(date +%s)

# Building the email with standard sh-compatible printf
printf "Subject: Skittles backup finished in $((END-START)) seconds\n\nLOG:\n$(cat "$LOG_FILE")" | sendmail -t og@hotmail.com

How it mysteriously beta-copies without reading full destination file?

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.