MacOS의 cron에서 bash 스크립트를 실행할 때 변수(로컬로 정의됨)가 정의되지 않습니다.

MacOS의 cron에서 bash 스크립트를 실행할 때 변수(로컬로 정의됨)가 정의되지 않습니다.

백업 생성을 위해 다음 스크립트가 있습니다.

#!/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

이 스크립트는 쉘, bash 또는 ZSH에서 스크립트를 실행하든 관계없이 수동으로 실행할 때 완벽하게 작동합니다.

이 경우 로그 파일은 다음과 같습니다.

[ 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

그러나 cronjob, Automator 또는 Apple Calendar를 통해 실행하면 로그 파일은 다음과 같습니다.

[ 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

해당 스크립트에서 정의한 변수는 아무런 영향을 미치지 않는다고 가정합니다.

여기서 문제가 무엇인지 아는 사람이 있습니까?

답변1

정말 감사합니다역청성그리고마크 와그너그들의 도움을 위해. 오류 출력을 추가한 후에는 누락된 경로와 디렉터리에 액세스하고 MySQL에서 데이터베이스 목록을 읽을 수 있는 권한이 부족하다는 사실이 혼합된 것으로 나타났습니다.

정말 감사합니다

관련 정보