Collection-Scripts/backup.sh

66 lines
2.2 KiB
Bash
Executable File

# backup.sh
#!/bin/bash
# Use your health check url
HEALTHCHECK_URL="https://healthchecks.io/ping/{uuid}"
curl --retry 3 "$HEALTHCHECK_URL/start" || exit 1
SRCDIRDOCKER="/root/docker"
DESTDIRDOCKER="/root/s3_backup/docker"
FILENAMEDOCKER="$(date +%Y-%m-%d)-BACKUP.tgz"
# Check if source directory exists
if [ ! -d "$SRCDIRDOCKER" ]; then
echo "$SRCDIRDOCKER does not exist"
exit 1
fi
tar -v --exclude-caches-all --exclude='.pyc' --exclude='.cache' --exclude='.DS_Store' --create --gzip --file="$DESTDIRDOCKER/DailyBackups/$FILENAMEDOCKER" "$SRCDIRDOCKER" || exit 1
SRCDIRSCRIPTS="/root/scripts"
DESTDIRSCRIPTS="/root/s3_backup/scripts"
FILENAMESCRIPTS="$(date +%Y-%m-%d)-BACKUP.tgz"
# Check if source directory exists
if [ ! -d "$SRCDIRSCRIPTS" ]; then
echo "$SRCDIRSCRIPTS does not exist"
exit 1
fi
tar -v --exclude-caches-all --exclude='.pyc' --exclude='.cache' --exclude='.DS_Store' --create --gzip --file="$DESTDIRSCRIPTS/DailyBackups/$FILENAMESCRIPTS" "$SRCDIRSCRIPTS" || exit 1
#BACKUP PROCESS HAPPENS HERE, PLACES BACKUP NAMED 10.29.15-BACKUP.zip in /home/User/DailyBackups FOLDER, WHICH WE WILL CALL $CurrentBackup
#Get Date from 3 months ago
ChkDate=`date --date="-3 months" +%Y-%m-%d`
#See if this file exists
ls $ChkDate-BACKUP.tgz $DESTDIRDOCKER/Archiv 2> /dev/null
EXITCODEDOCKERZIP=$?
ls $ChkDate-BACKUP.tgz $DESTDIRSCRIPTS/Archiv 2> /dev/null
EXITCODESCRIPTSZIP=$?
#If it does exist then copy current backup to BackupArchive Folder and Remove any backups older than 367 days from the BackupArchive Folder
if [[ $EXITCODEDOCKERZIP == 0 ]]; then
cp $DESTDIRDOCKER/DailyBackups/$FILENAMEDOCKER $DESTDIRDOCKER/Archiv/$FILENAMEDOCKER;
find $DESTDIRDOCKER/Archiv/*-BACKUP.tgz -mtime +367 -exec rm {} \;
fi
if [[ $EXITCODESCRIPTSZIP == 0 ]]; then
cp $DESTDIRSCRIPTS/DailyBackups/$FILENAMESCRIPTS $DESTDIRSCRIPTS/Archiv/$FILENAMESCRIPTS;
find $DESTDIRSCRIPTS/Archiv/*-BACKUP.tgz -mtime +367 -exec rm {} \;
fi
#Remove all but the most recent 12 Backups
for i in `ls -t $DESTDIRDOCKER/DailyBackups/*-BACKUP.tgz | tail -n +3`; do
rm "$i"
done
for d in `ls -t $DESTDIRSCRIPTS/DailyBackups/*-BACKUP.tgz | tail -n +3`; do
rm "$d"
done
curl --retry 3 "$HEALTHCHECK_URL" || exit 1