Search code examples
bashubuntucron

Script works fine standalone, but is incomplete as cron


I have this backup script that I am trying to run overnight as a cron job. It works fine as standalone script. Backs up every folder. I have it setup as root (sudo crontab -e). But as a cron job it only backs up the first folder ("/home"), but none of the others. Any thoughts on what might be happening? Also, I'll take any suggestions on my overall approach as well.


outputFile="/mnt/synology/"$now"_backup.tar.gz"

inputFiles="/home"
inputFiles+=" /var/www"  #webpages for apache
inputFiles+=" /etc/fstab" #needed to mount synology share drive on /mnt
inputFiles+=" /etc/NetworkManager/NetworkManager.conf"  #to keep resolvd dead, so it doesn't interfere with ubound
inputFiles+=" /etc/ufw/*.*"  #to get all the firewall rules setup under ufw
inputFiles+=" /etc/apache2/sites-available/*.*" #apache websites available
inputFiles+=" /etc/mysql/mysql.conf.d/*.cnf"  #to allow nonlocal access into mysql server. It allows from everywhere. Y>inputFiles+=" /etc/samba/*.conf"  #samba config
inputFiles+=" /etc/unbound/*.conf"  #unbound configuration files
inputFiles+=" /var/spool/cron"  #crontab config files

sudo tar czf $outputFile --exclude=".*" $inputFiles

Solution

  • The problem turned out to be a missing shebang line in the script. The first line should be something like:

    #!/bin/bash
    

    ...to tell the system to run the script with bash, rather than whatever its default shell is. Which shell is used as a default can be hard to predict, so it's best to specify it explicitly, especially if you're using any bash extensions to the basic shell syntax (like +=). If you don't know what's a bash extension, use a bash shebang just in case.