Mondo Unattended Backup Script

From Superk

Jump to: navigation, search
This page is unfinished. If you are logged in (Unattended Backup Script log in), you may edit this page. You can help make this site more informative!

This is a bash shell script that was designed to wrap around the Mondorescue project that would provide a way of running a backup unattended. The wrapper is needed as Mondo expects to be able to output to stdout which may not be available in an unattended setting.

#!/bin/bash
#
# Mondo/Mindi unattended backup script
#
# BK - 1-6-2003

# User defined variables 

DEST=/home			# Mount point of destination partition (must include preceding "/"
ADMIN=superbenk@superk.org	# Admin email to send reports to
LOG=/var/log			# Log file directory
BACKUP_DIR=/home/bkrein		# Location for backups
EXCLUDE_MNT="/dev/shm"		# List of *mount points* (not directories) to exclude from the backup process (seperated by white-space)
EXCLUDE_DIR="/windows"		# List of *directories* (not mount points) to exclude from the backup process (seperated by white-space)
ISO_SIZE=700m			# Set the max size of the ISO files to be created
COMP=9				# Set the compression level (0-9; 0 = lowest, 9 = highest)
SCRATCH_DIR=/tmp		# Directory where ISOs will be created before archive

# ==================================================================================================
# SCRIPT CONTENTS - DO NOT EDIT BELOW THIS POINT
# ==================================================================================================

LOG_DATE=`date "+%b  %e %R:%S"`	# Formatted date command for log files
DIR_DATE=`date "+backup_%F"`	# Formatted date command for creating backup directories

USER_DIR=$(cd $BACKUP_DIR; ls -ld | awk '{print $3}')
GROUP_DIR=$(cd $BACKUP_DIR; ls -ld | awk '{print $4}')

# Define locations of vital programs/utilities

MONDOARCHIVE=`which mondoarchive`
MINDI=`which mindi`
CHOWN=`which chown`
MKDIR=`which mkdir`


# Translate $DEST to something usable by awk

DEST_TRANS=$(echo $DEST | sed 's!/!\\/!g')

# This must be run as root since access to every file is required

echo -n "Checking that you are root: "

if [[ $UID != 0 ]]; then
	echo You must be root!
	exit 1
else
	echo OK, you are root!
fi

# Check that a log file is available, if not create one in the 
# directory specified in the user defined variables.

echo -n "Checking that log file exists: "

if [[ -e $LOG\/backup.log ]]; then
	echo Log file exists!
else	
	touch $LOG/backup.log
	if [[ $? != 0 ]]; then
		echo Failed! Something is wrong.
		exit 1
	else
		echo Log file did not exist - created it.
	fi
fi

# Log that backup script has started

echo "$LOG_DATE BACKUP[$$]: BEGIN - Mondo automation script started." >> $LOG/backup.log

# Check that $DEST is actually a mount point and not a directory

if [[ ! $(df | awk '/'$DEST_TRANS'$/') ]]; then
	echo "You must use a mount point, not a directory in your configuration for 'DEST'!  Quitting..."
	echo -e "Backup failed - incorrect configuration:\n\nYou must use a mount point, not a directory in your configuration for the DEST variable!" | mail -s "Backup failed - incorrect configuration" $ADMIN
	echo "$LOG_DATE BACKUP[$$]: ERROR - Incorrect configuration - DEST variable must be a mount point" >> $LOG/backup.log
	exit 1
fi

# Following calculate both the total drive space used (CHECK)
# and the actual amount of free space on the designated partition
# (ACTUAL).  

ACTUAL=$(df | echo $(awk '/'$DEST_TRANS'$/ {print $4}')+5120 | bc -l)
CHECK=$(df | tail +2 | grep -v ${EXCLUDE_MNT// /\\|} | echo $(awk '{sum+=$3}; END {print sum}') | bc -l)

# Following are modified versions of ACTUAL and CHECK that will
# display the results in a more user friendly manner using 'GB'

MOD_ACT=`echo scale\=2\; \($ACTUAL/1024\)/1024 |bc -l`
MOD_CHK=`echo scale\=2\; \($CHECK/1024\)/1024 |bc -l`

echo -n "Checking drive space: "

if [[ "$CHECK" -ge  "$ACTUAL" ]]; then
	echo Not enough room!
	echo -e "Backup failed - insufficient space available:\n\nSpace needed: $MOD_CHK GB\nSpace available: $MOD_ACT GB" | mail -s "Backup failure - insufficient drive space" $ADMIN 
	echo "$LOG_DATE BACKUP[$$]: ERROR - Insufficient space available, $MOD_ACT GB available but $MOD_CHK GB needed." >> $LOG/backup.log

	echo -e "Needed: $MOD_CHK""GB\tAvailable: $MOD_ACT""GB"
	exit 1
else
	echo You have enough space!
	echo -e "Needed: $MOD_CHK""GB\tAvailable: $MOD_ACT""GB"
fi

# Check to see if directory exists for storing backups

echo -n "Checking for backup root directory: "
if [[ -d $BACKUP_DIR/mondo ]]; then
	echo Directory exists!
else
	echo -n "Directory doesn't exist, creating... "
	$MKDIR $BACKUP_DIR/mondo
	$CHOWN $USER_DIR.$GROUP_DIR $BACKUP_DIR/mondo
	if [[ $? != 0 ]]; then
		echo Failed!
		echo -e "Backup failed - couldn't create backup root directory:\n\n$BACKUP_DIR/mondo." | mail -s "Backup failure - couldn't create directory" $ADMIN
		echo "$LOG_DATE BACKUP[$$]: ERROR - Couldn't create backup root directory: $BACKUP_DIR/mondo." >> $LOG/backup.log 
		exit 1
	else
		echo Done!
		echo Backup root directory: $BACKUP_DIR/mondo
		BACKUP_DIR=$BACKUP_DIR/mondo
	fi
fi

# Create new backup directory for current ISO images

echo -n "Creating directory to store today's backup: "
if [[ -d $BACKUP_DIR/$DIR_DATE ]]; then
	echo Directory exists!  Continuing...
else
	$MKDIR $BACKUP_DIR/$DIR_DATE
	$CHOWN $USER_DIR.$GROUP_DIR $BACKUP_DIR/$DIR_DATE
	if [[ $? != 0 ]]; then
		echo Failed!
		echo -e "Backup failed - couldn't create directory:\n\n$BACKUP_DIR/$DIR_DATE." | mail -s "Backup failure - couldn't create directory" $ADMIN
		echo "$LOG_DATE BACKUP[$$]: ERROR - Couldn't create backup directory: $BACKUP_DIR/$DIR_DATE." >> $LOG/backup.log
		exit 1
	else
		echo Done!
		echo Backup directory: $BACKUP_DIR/$DIR_DATE
	fi
fi

# Actually start the Mondo backup

echo "$LOG_DATE BACKUP[$$]: BEGIN - Mondo backup process started." >> $LOG/backup.log

# Start mondoarchive job
#
# 	-O = Archive (not verify)
#	-i = Backup to ISO images only (no removable medium)
# 	-E = Exclude directories from backup
#	-N = Exclude all NFS mounts (only backup local drives)
#	-d = Backup to this directory
#	-s = Set maximum ISO size
#	-(0-9) = Set compression level
#	-F = Do not offer to write boot+data floppy disk images - images still created & stored
#	-S = Scratch directory for building ISO images
#
# May be worth adding something like this (after testing) to modify ISO filenames to something
# a bit more friendly:
#
#	-A 'mv _ISO_ backup-_ISO_'

$MONDOARCHIVE -OVi -E$EXCLUDE_DIR -N -d$BACKUP_DIR/$DIR_DATE -s$ISO_SIZE -$COMP -F -S$SCRATCH_DIR

if [[ $? != 0 ]]; then
	echo There was a problem... failed!
	echo "$LOG_DATE BACKUP[$$]: ERROR - Mondo backup process failed!" >> $LOG/backup.log
	echo -e "Backup failed - Mondo backup process failed:\n\nCheck the log file:\n\n /var/log/mondo-archive.log\n\nBackup incomplete." |mail -s "Backup failure - Mondo backup process failed!" $ADMIN
	exit 1
else
	echo Mondo backup process complete!
	echo ISOs located at: $BACKUP_DIR/$DIR_DATE
	echo -e "Backup complete - Mondo backup process successful!\n\nBackup ISOs located at: $BACKUP_DIR/$DIR_DATE\n\nPlease burn the ISOs to CD-R media and remove immediately!\n\nLog file: /var/log/mondo-archive.log" | mail -s "Backup success - Mondo backup process complete!" $ADMIN
	echo "$LOG_DATE BACKUP[$$]: SUCCESS - Mondo backup process completed!" >> $LOG/backup.log
fi
Personal tools