utils/bash/cronbackup/backup.sh

90 lines
1.5 KiB
Bash
Raw Normal View History

2014-01-23 12:48:52 +00:00
#!/bin/bash
# IO Redirection
exec > backup.log
# exec 2> backup_err.log
BMOUNT=/mnt/backup
BAKDIR=$BMOUNT/backup/cron
2014-01-23 12:48:52 +00:00
if ! grep -qs $BMOUNT /proc/mounts; then
echo Mounting $BMOUNT ...
mount $BMOUNT
2014-01-23 12:48:52 +00:00
fi
2014-01-23 12:48:52 +00:00
if ! [ -d $BAKDIR ]; then
mkdir $BAKDIR
fi
echo Begin backup schedule:
echo Date: $(date)
echo $'\n'
2014-01-23 12:48:52 +00:00
while read line; do
# substring first char
firstChr=${line::1}
# if line is not empty and does not start with "#"
if [ $firstChr ] && [ $firstChr != "#" ]; then
# file and arguments are seperated by ":"
IFS=':' read -ra ARGS <<< "$line"
# Get file
FILE=${ARGS[0]}
# if file exists
if [ -a $FILE ]; then
# filename
FILENAME=$(basename $FILE)
# set the backup name YYYYMMDDhhmm
BACKUPFILE=$(date +$FILENAME.%Y%m%d%H%M.tar.gz)
# exclude file
EXCLUDE="exclude/${ARGS[1]}"
if [ -f $EXCLUDE ]; then
tar zcf "$BAKDIR/$BACKUPFILE" -C "$FILE/../" $FILENAME -X $EXCLUDE
else
tar zcf "$BAKDIR/$BACKUPFILE" -C "$FILE/../" $FILENAME
fi
# count backup file
fCount=$(ls $BAKDIR/*$FILENAME*.gz|wc -l)
echo Checking for expired backups $FILENAME \($fCount\)
2014-01-23 12:48:52 +00:00
# keep up to 7 backup
if [ $fCount -ne 7 ]; then
# delete count
dCount=`expr $fCount - 7`
# loop over files
for files in $BAKDIR/*$FILENAME*.gz; do
if [ 0 -lt $dCount ]; then
# remove expired file
rm "$files";
# dCount --
dCount=$((dCount-1));
fi
done
fi
fi
fi
# echo --\> $line
done < backup_list
umount $BMOUNT