backup2streamer/do_full_backup.sh

190 lines
5.1 KiB
Bash
Raw Normal View History

2017-04-20 12:20:52 +02:00
#!/bin/bash
2017-04-20 15:21:24 +02:00
# The folder which should be backuped (hint: use links to backup more than one folder at a time)
2017-04-20 12:20:52 +02:00
SOURCE_FOLDER="/opt/data/backup/backup_folders"
2017-04-20 15:21:24 +02:00
# The device to store the tar file to
2017-04-20 12:20:52 +02:00
LTO_DEV="/dev/st0"
2017-04-20 15:21:24 +02:00
# Name of the Backup-Set
2017-04-20 12:20:52 +02:00
BACKUP_NAME="full_backup"
2017-04-20 15:21:24 +02:00
# Where to store information about used tapes
2017-04-20 12:20:52 +02:00
TAPE_FOLDER="/opt/data/backup/tape_info"
2017-04-20 15:21:24 +02:00
# How long to wait between tape checks (if a tape has been inserted)
2017-04-20 12:20:52 +02:00
TAPE_DELAY="30s"
2017-04-20 15:21:24 +02:00
# How often to try to find a tape (1440 and 30s above will lead to 12h of trying)
TAPE_CHECK="1440"
# Receiver of result mail
MAIL="your@email.here"
# set maximum Tape Size
2017-04-20 12:20:52 +02:00
MAX_TAPE_SIZE="500G"
2017-04-20 15:21:24 +02:00
#
# You shouldn't need to change some below here
#
# Todays timestamp
2017-04-20 12:20:52 +02:00
DATE="$(date +%Y-%m-%d)"
TIME="$(date +%H-%M)"
2017-04-20 15:21:24 +02:00
# Create a temporary filename
LOGFILE_BASENAME="$(mktemp -t tar_output_XXX)"
# Search in Tape_Folder for the oldest file in the backup-set
2017-04-20 12:20:52 +02:00
TAPE_OLDFILE="$(ls -tC1 ${TAPE_FOLDER} | tail -n 1)"
2017-04-20 15:21:24 +02:00
MESSAGE=""
#
# First some general sanity checks
#
2017-04-20 12:20:52 +02:00
if [ ! -r $SOURCE_FOLDER ]; then
MESSAGE="ERROR: Source folder does not exist or is not readable."
fi
if [ ! -w $LTO_DEV ]; then
MESSAGE="ERROR: Output file/device does not exist or is not writeable."
fi
if [ ! -w $TAPE_FOLDER ]; then
MESSAGE="ERROR: Tape info-folder does not exist or is not writeable."
fi
if [ ! -z "$MESSAGE" ]; then
echo $MESSAGE; exit 1
fi
2017-04-20 15:21:24 +02:00
#
# Loop to wait for valid tape (see TAPE_CHECK and TYPE_DELAY parameters); give up after specified tries
# Hint: Format of serials is similar to C140604093
2017-04-20 12:20:52 +02:00
COUNTER=0
TAPE_SERIAL="$(sg_rmsn -r /dev/st0 2> /dev/null)"
EXIT_CODE=$?
while [ $COUNTER -lt $TAPE_CHECK -a $EXIT_CODE -ne 0 ]; do
2017-04-20 15:21:24 +02:00
# above check was negativ: send one mail, wait and recheck
2017-04-20 12:20:52 +02:00
echo $TAPE_SERIAL
if [ $EXIT_CODE -ne 0 -a $COUNTER -eq 0 ]; then
mail -s "Backup - ${BACKUP_NAME} - no tape found" $MAIL <<EOM
Hi Admin,
it seems there is currently no tape insert into the tape drive.
Please insert a tape into the drive to store the backup. It should be
a new one or the oldest of the backup-set.
Currently the oldest tape file seems to be:
2017-04-20 15:21:24 +02:00
${TAPE_OLDFILE%.log}
2017-04-20 12:20:52 +02:00
Best regards,
backup-script
EOM
fi
sleep $TAPE_DELAY
TAPE_SERIAL="$(sg_rmsn -r /dev/st0 2> /dev/null)"
EXIT_CODE=$?
let COUNTER=COUNTER+1
done
echo Tape suche abgeschlossen
# Check if we had a timeout. If so we send a mail an exit
if [ $COUNTER -eq $TAPE_CHECK ]; then
# Timeout!
mail -s "Backup - ${BACKUP_NAME} - tape-timeout" $MAIL <<EOM
Hi Admin,
2017-04-20 15:21:24 +02:00
even after waiting for the defined timout values: ${TAPE_CHECK} times ${TAPE_DELAY}
2017-04-20 12:20:52 +02:00
no tape was inserted. I gave up...
Best regards,
backup-script
EOM
echo timeout
exit 1;
fi
TAPE_FILE="${TAPE_FOLDER}/${TAPE_SERIAL}.log"
2017-04-20 15:21:24 +02:00
2017-04-20 12:20:52 +02:00
# So we should have a tape serial now, will check existing tape infos now
# First check if it is a new tape; this will be ok to use
# Else check if it is the oldest tape we used previously; this will be ok as well
# Else we will abort and send a mail...
if [ "$(ls -A ${TAPE_FILE} 2> /dev/null)" ]; then
# We found an existing file. Now we check if it is the oldest file in our log-folder.
if [ $TAPE_OLDFILE != $TAPE_SERIAL.log ]; then
mail -s "Backup - ${BACKUP_NAME} - invalid tape" $MAIL <<EOM
Hi Admin,
the inserted tape is not a valid one because it was used before and is not the oldest
tape in the current backup-set.
inserted serial : ${TAPE_SERIAL}
oldest used serial: ${TAPE_OLDFILE%.log}
2017-04-20 15:21:24 +02:00
Please remove the old serial logfile or use another or new tape.
2017-04-20 12:20:52 +02:00
Best regards,
backup-script
EOM
exit 1
fi
fi
touch $TAPE_FILE
2017-04-20 15:21:24 +02:00
#
# Now we have everything we need. So finnally do the backup.
#
2017-04-20 12:20:52 +02:00
# rewind this tape (just to be sure)
MESSAGE="$(mt -f /dev/nst0 rewind)"
2017-04-20 15:21:24 +02:00
# Start the backup
MESSAGE="$(tar -c -v --index-file=${LOGFILE_BASENAME}.index --totals -f - ${SOURCE_FOLDER} 2> ${LOGFILE_BASENAME}.err.txt | mbuffer -q -L -s 256k -m 1G -P 95 -o ${LTO_DEV})"
#echo "tar -c -v --index-file=${LOGFILE_BASENAME}.index --totals -f - ${SOURCE_FOLDER} 2> ${LOGFILE_BASENAME}.err | mbuffer -q -L -s 256k -m 1G -P 95 -o ${LTO_DEV}"
2017-04-20 12:20:52 +02:00
ERROR_CODE=$?
if [ $ERROR_CODE -ne 0 ]; then
2017-04-20 15:21:24 +02:00
mail -s "Backup -${BACKUP_NAME}- exited with error(s)" -a ${LOGFILE_BASENAME}.index -a ${LOGFILE_BASENAME}.err.txt $MAIL <<EOM
2017-04-20 12:20:52 +02:00
Hi Admin,
2017-04-20 15:21:24 +02:00
the backup for $SOURCE_FOLDER exited with error code $ERROR_CODE. Please check on addtional actions. Detailed output or the script below:
2017-04-20 12:20:52 +02:00
$MESSAGE
2017-04-20 15:21:24 +02:00
Please also check attached index and .err.txt file for further information.
2017-04-20 12:20:52 +02:00
Best regards,
your backup-script
EOM
else
2017-04-20 15:21:24 +02:00
mail -s "Backup -${BACKUP_NAME}- was successful" -a ${LOGFILE_BASENAME}.index -a ${LOGFILE_BASENAME}.err.txt $MAIL <<EOM
2017-04-20 12:20:52 +02:00
Hi Admin,
2017-04-20 15:21:24 +02:00
the backup for $SOURCE_FOLDER went fine. Please see the .index file for a list of stored files and the .err.txt file for error output of the tar command (there shouldn\'t be anything).
2017-04-20 12:20:52 +02:00
Please make sure to savely store the tape.
Best regards,
your backup-script
---------
$MESSAGE
EOM
fi