hi guys
i am using a script to create daily backups of a folder + sql file on a server running centos using rclone that uploads to azure.
this script works fine when I run it via ssh console, but if I add it as cron job in /etc/crontab, the script runs, creates the dump.sql, puts the archive in tmp folder, but does not upload it to azure. i am guessing it fails at the rclone part.
here is the script,what is the problem here, can someone tell me?
#!/usr/bin/env bash
set -e
set -u
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
export PATH
today=$(date +%d-%m-%Y-%H:%M)
logs="/rclone.log"
# name of the directory where the archive_file will be created
backupdir="/tmp"
# Container name on AZURE
container="apache-backup"
# name and location of sql dump file
sql_dump_file="/dump.sql"
# AZURE location
backend="azure:${container}"
# name of the archive file, change backup_ to backup_servername if using script on multiple different servers to avoid confusion and possibly overwriting of backups.
archive_file="$backupdir/backup_$(date '+%Y-%m-%d_%H%M%S').tar.gz"
# Rclone exports for connecting to AZURE
export RCLONE_CONFIG_AZURE_TYPE=azureblob
export RCLONE_CONFIG_AZURE_ACCOUNT=xxxxx
export RCLONE_CONFIG_AZURE_KEY=xxxxxxxx
# dumps the sql database into sql_dump_file
echo $today "Creating sql dump" >> $logs
mysqldump --quick --single-transaction --all-databases > "${sql_dump_file}"
if [ -e "${sql_dump_file}" ] ; then
echo $today "sql dump file created" >> $logs
else
echo $today "creation of sql dump failed, exiting" >> $logs
exit 1
fi
# archives the dirs var/www, root and the sql_dump_file into a tar.gz file (excluding .gnupg dir,
echo $today "Creating archive file" >> $logs
/usr/bin/tar --exclude='.gnupg' -czf "${archive_file}" /var/www "${sql_dump_file}"
if [ -e "${archive_file}" ] ; then
echo $today "Archive file created" >> $logs
else
echo $today "Creation of Archive file failed, cleaning and exiting" >> $logs
rm -rf "${sql_dump_file}"
exit 1
fi
# copies the archive file to the AZURE backup location
echo $today "Copying archive to AZURE" >> $logs
/usr/bin/rclone copy -v "${archive_file}" "${backend}" >> $logs 2>&1
echo $today "Cleanup" >> $logs
echo "----------------------------------------------------" >> $logs
rm "${archive_file}" "${sql_dump_file}"
exit 0