scripts/backup.sh

78 lines
1.7 KiB
Bash
Raw Normal View History

2021-01-21 16:58:32 -05:00
#!/usr/bin/env bash
#
# Tar directory files
#
CONF=$1
CURR_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
BOOTSTRAP=${CURR_DIR}/bootstrap.sh
## Check for perl regex support in grep
echo 'test' | grep -P 'test' > /dev/null 2>&1
if [ $? != 0 ]; then
echo "EXITING: your distribution of grep binary doesn't support -P"
exit 1
fi
if [ ! -e ${BOOTSTRAP} ];then
echo 'EXITING: boostrap.sh must be in the same directory as this script'
exit 1
fi
source ${BOOTSTRAP}
if [ -z "${CONF}" ] || [ ! -e ${CONF} ];then
echo 'EXITING: missing configuration file'
exit 1
fi
source ${CONF}
## Check if we exceeded number of back up files
FILES=$(ls -r ${BACKUP_DEST})
i=0
RM=''
for f in $FILES
do
FILE=$(echo $f | grep -P "(${B_PREFIX}\..*\.tgz)$")
if [ -n "${FILE}" ]; then
FILE=${BACKUP_DEST}/${FILE}
((i++))
if [ $i -gt ${NUM_BACKUPS} ]; then
if [ -z "${RM}" ]; then
RM="rm -f ${FILE}"
else
RM="${RM} ${FILE}"
fi
fi
fi
done
if [ -n "${RM}" ]; then
echo 'CRON: Removing old backups..'
debug "Executing: ${RM}"
${RM}
else
echo 'CRON: Did not remove any old backups this run...'
fi
## Run backup
echo "CRON: Backing up files now..."
BASENAME=$(basename ${BACKUP_DIR})
cd ${BACKUP_DIR}
cd ..
### Debug
debug "Current directory: "`pwd`
tar -czf ${BACKUP_DEST}/${B_PREFIX}.`date +%m-%d-%Y`.tgz ${BASENAME} >> ${LOG} 2>&1
if [ $? != 0 ]; then
echo "CRON: Failed to backup files"
send_notification "CRON: Unable to backup directory on ${HOSTNAME}" "DATE: {$TODAY}\n\nUnable to backup directory: ${BACKUP_DIR}"
exit 1
else
echo "CRON: Successfully backed up files"
send_notification "CRON: Successfully backed up directory on ${HOSTNAME}" "DATE: $TODAY\n\n Successfully backed up directory: ${BACKUP_DIR}"
exit 0
fi