Variablen (lokal definiert) sind beim Ausführen eines Bash-Skripts von Cron unter MacOS nicht definiert

Variablen (lokal definiert) sind beim Ausführen eines Bash-Skripts von Cron unter MacOS nicht definiert

Ich habe folgendes Skript zum Erstellen von Backups:

#!/usr/bin/env bash
{
  declare -a directories=(
    "/Applications/Apache"
    "/Applications/Scripts"
  )

  function duplicateBackups()
  {
    from=`realpath $1`
    to=`realpath $2`
    echo [ `date +"%T"` ] Copying backups from $from to $to
    cp -r $1 $2/
    sleep 1
  }

  date=`date +"%Y-%m-%d"`

  destination1='/opt/Volumes/USB1'
  destination2='/opt/Volumes/USB2'
  destination3='/opt/Volumes/FireWire'
  destination4='/opt/Volumes/USB3'
  destinationFolder='MacMini-Backups'
  targetFolder=$destination1/$destinationFolder/$date

  mkdir $targetFolder
  targetPath=`realpath $targetFolder`

  echo [ `date +"%T"` ] Starting files backups

  for i in "${directories[@]}"
  do
    fileName="$(basename $i)"
    mkdir -p $targetFolder/Files/
    echo [ `date +"%T"` ] Backing up "$i" as "$targetPath/Files/$fileName.tgz"
    tar -Pczf $targetFolder/Files/$fileName.tgz -C $i .
    sleep 1
  done
  echo [ `date +"%T"` ] Files backups finished
  sleep 1
  echo [ `date +"%T"` ] Starting databases backup
  sleep 1

  mkdir -p $targetFolder/Databases/

  echo [ `date +"%T"` ] Backing up all databases into $targetPath/Databases/full-dump.sql.gz
  mysqldump --opt --all-databases --skip-lock-tables | gzip -c > $targetFolder/Databases/full-dump.sql.gz

  for db in `mysql -se "show databases;"`;
  do
    if [[ ${db} != *"schema"* ]]; then
      echo [ `date +"%T"` ] Backing up database $db into $targetPath/Databases/$db-dump.sql.gz
      mysqldump --skip-lock-tables --opt $db | gzip -c > $targetFolder/Databases/$db-dump.sql.gz
      sleep 1
    fi
  done;
  echo [ `date +"%T"` ] Databases backup finished
  sleep 1

  duplicateBackups $destination1/$destinationFolder/$date $destination2/$destinationFolder
  duplicateBackups $destination1/$destinationFolder/$date $destination3/$destinationFolder
  duplicateBackups $destination1/$destinationFolder/$date $destination4/$destinationFolder

} >> /Applications/Scripts/Logs/backups.log

Dieses Skript funktioniert absolut einwandfrei, wenn ich es manuell ausführe, unabhängig davon, ob es von der Shell, Bash oder ZSH aus erfolgt.

So sieht die Protokolldatei in diesem Fall aus:

[ 09:26:05 ] Starting files backups
[ 09:26:05 ] Backing up /Applications/Apache as /Volumes/USB1/MacMini-Backups/2020-12-31/Files/Apache.tgz
[ 09:27:14 ] Backing up /Applications/Scripts as /Volumes/USB1/MacMini-Backups/2020-12-31/Files/Scripts.tgz
[ 09:27:15 ] Files backups finished
[ 09:27:16 ] Starting databases backup
[ 09:27:17 ] Backing up all databases into /Volumes/USB1/MacMini-Backups/2020-12-31/Databases/full-dump.sql.gz
[ 09:27:35 ] Backing up database Cloud into /Volumes/USB1/MacMini-Backups/2020-12-31/Databases/Cloud-dump.sql.gz
[ 09:27:36 ] Backing up database mysql into /Volumes/USB1/MacMini-Backups/2020-12-31/Databases/mysql-dump.sql.gz
[ 09:27:38 ] Backing up database radek into /Volumes/USB1/MacMini-Backups/2020-12-31/Databases/site-dump.sql.gz
[ 09:27:41 ] Backing up database radek_j3 into /Volumes/USB1/MacMini-Backups/2020-12-31/Databases/site_j3-dump.sql.gz
[ 09:27:45 ] Backing up database test into /Volumes/USB1/MacMini-Backups/2020-12-31/Databases/test-dump.sql.gz
[ 09:27:46 ] Backing up database site_j3 into /Volumes/USB1/MacMini-Backups/2020-12-31/Databases/joomla_3-dump.sql.gz
[ 09:27:48 ] Backing up database joomla_dev into /Volumes/USB1/MacMini-Backups/2020-12-31/Databases/joomla_dev-dump.sql.gz
[ 09:27:49 ] Backing up database joomla_dev2 into /Volumes/USB1/MacMini-Backups/2020-12-31/Databases/joomla_dev2-dump.sql.gz
[ 09:27:51 ] Backing up database joomla_j4 into /Volumes/USB1/MacMini-Backups/2020-12-31/Databases/joomla_j4-dump.sql.gz
[ 09:27:53 ] Backing up database joomla_site into /Volumes/USB1/MacMini-Backups/2020-12-31/Databases/joomla_site-dump.sql.gz
[ 09:28:06 ] Databases backup finished
[ 09:28:07 ] Copying backups from /Volumes/USB1/MacMini-Backups/2020-12-31 to /Volumes/USB2/MacMini-Backups
[ 09:28:30 ] Copying backups from /Volumes/USB1/MacMini-Backups/2020-12-31 to /Volumes/FireWire/MacMini-Backups
[ 09:28:43 ] Copying backups from /Volumes/USB1/MacMini-Backups/2020-12-31 to /Volumes/USB3/MacMini-Backups

Wenn ich es jedoch über Cronjob, Automator oder sogar Apple Calendar ausführe, sieht die Protokolldatei folgendermaßen aus:

[ 06:00:00 ] Starting files backups
[ 06:00:00 ] Backing up /Applications/Apache as /Files/Apache.tgz
[ 06:01:22 ] Backing up /Applications/Scripts as /Files/Scripts.tgz
[ 06:01:23 ] Files backups finished
[ 06:01:25 ] Starting databases backup
[ 06:01:26 ] Backing up all databases into /Databases/full-dump.sql.gz
[ 06:01:26 ] Databases backup finished
[ 06:01:27 ] Copying backups from to
[ 06:01:46 ] Copying backups from to
[ 06:01:58 ] Copying backups from to

Ich gehe davon aus, dass die Variablen, die ich in diesem Skript definiert habe, keinerlei Wirkung haben.

Weiß jemand, was hier das Problem sein könnte?

Antwort1

Vielen Dank anbitinerantUndMark Wagnerfür ihre Hilfe. Nachdem ich die Fehlerausgabe hinzugefügt hatte, stellte sich heraus, dass es sich um eine Mischung aus fehlenden Pfaden und fehlenden Berechtigungen zum Zugriff auf Verzeichnisse und zum Lesen von Datenbanklisten aus MySQL handelte.

Vielen Dank

verwandte Informationen