Table of Contents

ABACKUP

Structure

root@server:/srv/abackup# tree
.
|-- conf
|   |-- abackup.conf
|   |-- files
|   |-- not
|-- err_msg.dat
|-- log
|-- scripts
|   |-- abackup_autologin_create.sh
|   |-- abackup_save.sh
|   |-- init_new_tape.sh
|   |-- libabackup.sh
|   |-- uploadFile.sh
`-- snapshot

Bash scripts

abackup.conf

# Configuration file
# Contains all the values needed by Advanced Backup
 
# Place where are different files and folder
#############################################
 
# ABACKUP_ROOT folder
BCK_ABACKUP_ROOT="/srv/abackup"
BCK_ABACKUP_DEST="/tmp/abackup"
 
# snapshot folder
BCK_SNAPSHOT_DIR="$BCK_ABACKUP_ROOT/snapshot/"
 
# Snapshot's filename
BCK_SNAPSHOT_FILENAME="snap_backup.dat"
 
# log folder
BCK_LOG_DIR="$BCK_ABACKUP_ROOT/log/"
 
# tmp folder
BCK_TMP_DIR="$BCK_ABACKUP_DEST/tmp"
 
#dailyPostingFolder
BCK_DAILY_DIR="$BCK_ABACKUP_DEST/dailyBackupFiles"
BCK_WEEKLY_DIR="$BCK_ABACKUP_DEST/weeklyBackupFiles"
 
# scripts folder
BCK_SCRIPT_DIR="$BCK_ABACKUP_ROOT/scripts/"
 
#conf folder
BCK_CONF_DIR="$BCK_ABACKUP_ROOT/conf/"
 
# Prefix of the tar file
BCK_TAR_PREFIX="backup_"
 
# Extension of the compressed tar file
BCK_COMPRESSED_EXTENSION="tgz"
BCK_PARTIAL_COMPRESSED_EXTENSION="tar.gz"
 
# File containing the name of directories/files to backup
BCK_BACKUP_LIST="files"
 
# File containing the name of directories/files to skip 
BCK_EXCLUDE_LIST="not"
 
# Information about when to do the full backup
#############################################
#
# Use : monday, tuesday, wednesday ...
BCK_FULL_BACKUP_DAY="monday"
#BCK_FULL_BACKUP_DAY="sunday"
 
# Information about today / DON'T CHANGE THAT
#############################################
 
# Day number of the week (sun = 0, mon = 1, ...)
BCK_DAY_NUM=`date +"%w"`
 
# Today's date. Example : "2005-04-05_Tue"
BCK_TODAY=`date +"%Y-%m-%d_%a"`
 
# Today's week number of the year (1..53)
# The new week starts on monday with %V and on sunday with %U
BCK_WEEK_NUM=`date +"%V"`
 
# Log file name
#############################################
 
# Name of the log file
BCK_LOG_FILE_NAME="$BCK_TODAY.log"
 
# Full path of the log file
BCK_LOG_FILE="$BCK_LOG_DIR/$BCK_LOG_FILE_NAME"
 
# Admin info
#############################################
 
# Email address
BCK_ADMIN_EMAIL="it@mydomain.com"
 
# Name
BCK_ADMIN_NAME="it"
 
# Devices info 
#############################################
 
# Device
BCK_TAPE_DEVICE=""
 
# Remote info
#############################################
 
# Remote computer
#BCK_REMOTE_COMPUTER="192.168.1.75"
BCK_REMOTE_COMPUTER="server2.com"
 
# Remote user
BCK_REMOTE_USER="root"
 
# Remote password
BCK_REMOTE_PASS="This is the best passphrase"
 
# Destination discs
BCK_DEST_DISC_1="/disc1/"
BCK_DEST_DISC_2="/disc2/"
BCK_DEST_DISC_3="/disc3/"
BCK_DEST_DISC_4="/disc4/"
 
# Default remote path
BCK_REMOTE_PATH=$BCK_DEST_DISC_1
 
# Local info (if the rest fails)
#############################################
 
BCK_LOCAL_PATH="$BCK_ABACKUP_DEST/failedBackup/"

files

/boot/
/etc/
/srv/
/var/lib/mysql/
/www/

not

*.mp3
*.mov
*.wav
*.mpg
*.mpeg
*.*~
*.wma
*.log

err_msg.dat

E0000;Configuration file doesn't exist, can't work without it.;Le fichier de configuration n'existe pas ou la variable d'environnement ne peut pas être trouvée.
E0001;Library is missing. Can't work without it.;La bibliothéque ne peut pas être trouvée, ceci peut-être dû à une erreur de configuration.
I0000;Starting Advanced Backup.;Indique que le logiciel Advanced Backup démarre.
I0100;Sunday. Creating a new snapshot.;Indique que la sauvegarde correspond au dimanche.
I0101;Deleting snapshot.;Indique que l'ancien fichier d'état va être détruit.
M/E0100;There's no file to backup. Please check the file <list_backup_file>.;Le fichier contenant la liste des fichier et/ou répertoire à sauvegarder n'existe pas, il faudrait le contrôler.
I0102;No file to exclude from the backup.;Indique qu'il n'y a pas de liste de fichier à exclure.
I0103;Creating archive.;Indique que l'archive est créée.
M/E0101;Tar has failed. You should be root to use Advanced Backup.;Le programme TAR a échoué, la raison est indéterminée. Le programme TAR doit être lancé en tant que 'root'.
M/E0102;Distant copy has failed. Copying to <local_path>.;La copie sur la machine distante a échoué. Une copie locale sera faite.
I0104;Snapshot seems to exist, deleting the copy.;Indique que la copie de l'ancien fichier d'état du système de fichier existe et qu'elle va être détruite.
M/W0100;Snapshot has not been created. Restoring the last one.;La création du nouveau fichier d'état du système de fichier a échoué, l'ancien va être restoré
M/W0101;Snapshot has not been created, but no old snapshot.;La création du nouveau fichier d'état du système de fichier a échoué, mais il n'y a pas de fichier précédent utilisable.
I0200;Day of the week. Doing a partial backup.;Indique que la sauvegarde est incrémentielle.
I0201;Copying snapshot file.;Indique que le fichier d'état du système de fichier est copiée.
W0200;No snapshot ! Create a new one.;Indique qu'il n'y a pas de fichier d'état du système de fichier. Un nouveau est créé.
M/E0200;There's no file to backup. Please check the file <list_backup_file>.;Le fichier contenant la liste des fichier et/ou répertoire à sauvegarder n'existe pas, il faudrait le contrôler.
I0202;No file to exclude from the backup.;Indique qu'il n'y a pas de liste de fichier à exclure.
I0203;Creating archive.;Indique que l'archive est créée.
M/E0201;Tar has failed. You should be root to use Advanced Backup.;Le programme TAR a échoué, la raison est indéterminée. Le programme TAR doit être lancé en tant que 'root'.
M/E0202;Something went wrong with the tape. Are you sure there's one in the device ? Copying on distant computer.;Le lecteur de bande est inaccessible, il est possible qu'aucune bande ne soit dedans. La sauvegarde va être transférée sur la machine distante.
M/E0203;Distant copy has failed. Copying to <local_path>.;La copie sur la machine distante a échoué. Une copie locale sera faite.
M/W0201;I guess that the tape is full. Please would you change the tape ?;La bande est pleine. Il faut la changer.
M/E0204;Undefined error ... Don't know what to do.;Une erreur indeterminée s'est produite. Cas pratiquement impossible.
W0300;Tape couldn't be rewinded or unloaded.;La bande n'a pas pu être rembobinnée ou éjectée.
M/E0300;Distant copy has failed. Copying to <local_path>.;La copie sur la machine distante a échoué. Une copie locale sera faite.

abackup_autologin_create.sh

#!/bin/sh
# You should run this with bash
# Uploads your id_rsa.pub to the specified host, wrapped for readability
if [ ! -r ${HOME}/.ssh/id_rsa.pub ]; then
 ssh-keygen -t rsa -b 2048
fi
# Copy the files to the remote servers
scp -qC ~/.ssh/id_rsa.pub $2@$1:/tmp/${USER}@${HOSTNAME}.id_rsa.pub
# Tell the remote host to copy the file over ~ of the current user
ssh $2@$1 "cat /tmp/${USER}@${HOSTNAME}.id_rsa.pub >> ~/.ssh/authorized_keys \
  && rm -f /tmp/${USER}@${HOSTNAME}.id_rsa.pub"
 
if [ $? -eq 0 ]; then
  echo "Success"
fi

abackup_save.sh

#!/bin/bash
#
# Author : Etienne Bagnoud
# Date : 2005-04-08
#
# Modified by : Sylvain Bolay
# Last Modification: 21.10.08
#
# Description :
#  - Script that do the backup. It's made of functions, 'cause it's easier
#    to call a function than a script if the main backup is missing.
#
 
if [[ -e $BCK_CONF_FILE ]]; then
	source $BCK_CONF_FILE
else
	echo "*** ERROR : Configuration file doesn't exist, can't work without it. (E0000)"
	exit 1
fi
 
if [[ -e $BCK_SCRIPT_DIR/libabackup.sh ]]; then
	source $BCK_SCRIPT_DIR/libabackup.sh 
else
	echo "*** ERROR : Library is missing. Can't work without it. (E0001)"
	exit 1
fi	
 
 
now_time=`date`
writeInfo "Start at : $now_time"
 
# Configure l'auto login sur une machine distante.
sshAutolog "$BCK_REMOTE_PASS"
 
# Function that choose the destination disc in function of the week number
function whichDisc()
{
	case $BCK_WEEK_NUM in
		1|5|9|13|17|21|25|29|33|37|41|45|49|53)	echo "$BCK_DEST_DISC_1"; return;;
		2|6|10|14|18|22|26|30|34|38|42|46|50)	echo "$BCK_DEST_DISC_2"; return;;
		3|7|11|15|19|23|27|31|35|39|43|47|51)	echo "$BCK_DEST_DISC_3"; return;;
#		4|8|12|16|20|24|28|32|36|40|44|48|52|0) return 4;;
		*)					echo "$BCK_DEST_DISC_4"; return;;
	esac
}
 
# Function that creat the main backup.
function createMainBackup() 	
{
 
	#create the file that contains all debian packages installed
	dpkg --get-selections > /srv/proxy_debian_list.txt
	dpkgReturn=$?
	if [[ $dpkgReturn -ne 0 ]]; then
	         message="A problem occurs while attempting to create a dpkg --get-selections file"
	         writeMail "$message"
	         writeWarning "$message"
	fi
 
	bck_tmp_log=$BCK_TMP_DIR/$BCK_TODAY.log
	writeInfo "Create Main Backup on $BCK_FULL_BACKUP_DAY ($BCK_TODAY). Creating a new snapshot. (I0100)"
	# If there's a snapshot of the file system, create a backup of the file then delete the file
	if [[ -e $BCK_SNAPSHOT_DIR/$BCK_SNAPSHOT_FILENAME ]]; then
		writeInfo "Deleting snapshot. (I0101)"
		cp $BCK_SNAPSHOT_DIR/$BCK_SNAPSHOT_FILENAME $BCK_TMP_DIR/$BCK_SNAPSHOT_FILENAME.bck
		rm $BCK_SNAPSHOT_DIR/$BCK_SNAPSHOT_FILENAME
	fi	
 
	# Check if there's files to backup
	if [[ ! -e $BCK_CONF_DIR/$BCK_BACKUP_LIST ]]; then
		message="There's no file to backup. Please check the file $BCK_CONF_DIR/$BCK_BACKUP_LIST. (M/E0100)"
		writeMail "$message"
		writeError "$message"
	fi	
 
	# Add option to run tar here.
	bck_extra_options="--ignore-failed-read --gzip"
 
	# Check if there's files to exclude from the backup
	if [[ ! -e $BCK_CONF_DIR/$BCK_EXCLUDE_LIST ]]; then
		writeInfo "No file to exclude from the backup. (I0102)"
	else 
		bck_extra_options="$bck_extra_options -X $BCK_CONF_DIR/$BCK_EXCLUDE_LIST"	
	fi
 
	#Block the directory for uploading files while tar is working
	#echo "Directory locked on $BCK_TODAY" > $BCK_WEEKLY_DIR/.lock
 
	# Create archive 
	writeInfo "Creating archive. (I0103)"
	#tar -T $BCK_CONF_DIR/$BCK_BACKUP_LIST $bck_extra_options --listed-incremental=$BCK_SNAPSHOT_DIR/$BCK_SNAPSHOT_FILENAME -cvf $BCK_TMP_DIR/$BCK_TAR_PREFIX$BCK_TODAY.$BCK_COMPRESSED_EXTENSION &> $bck_tmp_log
	tar -T $BCK_CONF_DIR/$BCK_BACKUP_LIST $bck_extra_options --listed-incremental=$BCK_SNAPSHOT_DIR/$BCK_SNAPSHOT_FILENAME -cvf $BCK_WEEKLY_DIR/$BCK_TAR_PREFIX$BCK_TODAY.$BCK_COMPRESSED_EXTENSION &> $bck_tmp_log
	tarReturn=$?
 
	cat $bck_tmp_log >> $BCK_LOG_FILE
	catReturn=$?
	# Warn if cating files failed
	if [[ $catReturn -ne 0 ]]; then
		message="Cating file failed..."
		writeMail "$message"
		writeWarning "$message"
	fi
 
	# Stop if tar faild. Send a mail.
	if [[ $tarReturn -ne 0 ]]; then
		message="Tar has failed. Check Disc space. (M/E0101)"
		#message="Tar has failed. You should be root to use Advanced Backup. (M/E0101)"
		message="$message + Snapshot has not been correctly created. Deleting what was already done. (M/E0101a)"
		rm $BCK_SNAPSHOT_DIR/$BCK_SNAPSHOT_FILENAME
		writeMail "$message"
		writeError "$message"
	fi 
 
	#chmod 777 $BCK_WEEKLY_DIR/$BCK_TAR_PREFIX$BCK_TODAY.$BCK_COMPRESSED_EXTENSION
	#Unlock the working directory
	#rm $BCK_WEEKLY_DIR/.lock
 
	discDest=`whichDisc`
	if [[ ! $discDest ]]; then 
		discDest=$BCK_DEST_DISC_4
		writeWarning "Error while selecting proper destination disc!"
		writeInfo "Will copy on disc $discDest!"
	fi
	writeInfo "Backup will be performed on disc $discDest!"
 
	# Distant copy of the file.
	#sshCopy $BCK_TMP_DIR/$BCK_TAR_PREFIX$BCK_TODAY.$BCK_COMPRESSED_EXTENSION $BCK_REMOTE_PATH/$BCK_TAR_PREFIX$BCK_TODAY.tar $BCK_REMOTE_COMPUTER $BCK_REMOTE_USER 
	sshCopy $BCK_WEEKLY_DIR/$BCK_TAR_PREFIX$BCK_TODAY.$BCK_COMPRESSED_EXTENSION $discDest/$BCK_TAR_PREFIX$BCK_TODAY.$BCK_COMPRESSED_EXTENSION $BCK_REMOTE_COMPUTER $BCK_REMOTE_USER 
	sshReturn=$?
 
	if [[ $sshReturn -ne 0 ]]; then
		#cp $BCK_TMP_DIR/$BCK_TAR_PREFIX$BCK_TODAY.$BCK_COMPRESSED_EXTENSION $BCK_LOCAL_PATH/
		#writeMail "Distant copy has failed. Copying to $BCK_LOCAL_PATH. (M/E0102)"
		#writeError  "Distant copy has failed. Copying to $BCK_LOCAL_PATH. (M/E0102)"
		message="Distant copy has failed. Please try manually. (M/E0102)"
		writeMail "$message"
		writeWarning "$message"
	else
		#message="Distant copy seems to be a success. Please check and if everything is ok please remove the local file\
		#manually. This is for testing purpose! And when no bugs can be automatically done!"
		message="Distant copy was a success. The locale file will be removed!"
		#writeMail "$message"
		writeInfo "$message"
		rm $BCK_WEEKLY_DIR/$BCK_TAR_PREFIX$BCK_TODAY.$BCK_COMPRESSED_EXTENSION
		rmReturn=$?
		if [[ $rmReturn -ne 0 ]]; then
			message="Removing local file failed. Please try manually!"
			writeMail "$message"
			writeInfo "$message"
		else
			message="Removing local file was a success!"
			writeInfo "$message"
		fi
	fi	
 
	# If the new snapshot was created, delete the old one	
	if [[ -e $BCK_SNAPSHOT_DIR/$BCK_SNAPSHOT_FILENAME ]]; then
		# Test if there's a old snapshot to delete
		if [[ -e $BCK_TMP_DIR/$BCK_SNAPSHOT_FILENAME.bck ]]; then
			writeInfo "Snapshot seems to exist, deleting the copy. (I0104)"
			rm $BCK_TMP_DIR/$BCK_SNAPSHOT_FILENAME.bck
		fi	
	# If the new shapshot wasn't created, restore the old one	
	else
		# Test if there's an old one. If not, send mail to administrator.
		if [[ -e $BCK_TMP_DIR/$BCK_SNAPSHOT_FILENAME.bck ]]; then
			message="Snapshot has not been created. Restoring the last one. (M/W0100)"
			writeMail "$message"
			writeWarning "$message"
			cp $BCK_TMP_DIR/$BCK_SNAPSHOT_FILENAME.bck $BCK_SNAPSHOT_DIR/$BCK_SNAPSHOT_FILENAME
		else
			message="Snapshot has not been created, but no old snapshot. (M/W0101)"
			writeMail "$message"
			writeWarning "$message"
		fi	
	fi	
 
	return 0
}
 
# Function that rewind the band and eject. Keeps the tar files.
# Do all the operation when the tape is full.
function doFullTapeOperation
{
	#bck_tar_file=$1
	#bck_file_path=$2
	compressedFileName="$BCK_TAR_PREFIX$BCK_TODAY.$BCK_COMPRESSED_EXTENSION" 
 
	ejectTape $BCK_TAPE_DEVICE
	if [[ $? -ne 0 ]]; then
		writeWarning "Tape couldn't be rewinded or unloaded. (W0300)"
	fi
 
	# Compress and make a distant copy of the file.
	gzip $BCK_DAILY_DIR/$BCK_TAR_PREFIX$BCK_TODAY.tar $BCK_DAILY_DIR/$compressedFileName
	chmod 777 $BCK_DAILY_DIR/$compressedFileName
	#sshCopy $bck_file_path/$bck_tar_file $BCK_REMOTE_PATH/$bck_tar_file $BCK_REMOTE_COMPUTER $BCK_REMOTE_USER 
 
 ====
#	if [[ $? -ne 0 ]]; then
#		cp $BCK_TMP_DIR/$BCK_TAR_PREFIX$BCK_TODAY.tar $BCK_LOCAL_PATH/
#		writeMail "Distant copy has failed. Copying to $BCK_LOCAL_PATH. (M/E0300)"
#		writeError  "Distant copy has failed. Copying to $BCK_LOCAL_PATH. (M/E0300)"
#	else
#		rm $bck_file_path/$bck_tar_file
#	fi	
 
	return 0
}
# Function that creates the incremental backup
function createIncrementalBackup()
{
	writeInfo "Day of the week. Doing a partial backup. (I0200)"
	bck_tmp_log=$BCK_TMP_DIR/$BCK_TODAY.log
 
	# Check if there's a snapshot, if not create one.
	if [[ -e $BCK_SNAPSHOT_DIR/$BCK_SNAPSHOT_FILENAME ]]; then
		writeInfo "Copying snapshot file. (I0201)"
		cp $BCK_SNAPSHOT_DIR/$BCK_SNAPSHOT_FILENAME $BCK_TMP_DIR/$BCK_SNAPSHOT_FILENAME
		bck_ss_file=$BCK_TMP_DIR/$BCK_SNAPSHOT_FILENAME
	else 
		writeWarning "No snapshot ! Create a new one (Main Backup will be done). (W0200)"
		createMainBackup
		createMainBackupReturn=$?
		if [[ $createMainBackupReturn -ne 0 ]]; then
			message="No snapshot was present and doing the MainBackup failed. (M/E02XX)"
			writeMail "$message"
			writeError "$message"
		else
			return 0
		fi
	fi
 
	# Check if there's files to backup
	if [[ ! -e $BCK_CONF_DIR/$BCK_BACKUP_LIST ]]; then
		message="There's no file to backup. Please check the file $BCK_CONF_DIR/$BCK_BACKUP_LIST. (M/E0200)"
		writeMail "$message"
		writeError "$message"
	fi	
 
	# Add option to run tar here.
	bck_extra_options="--ignore-failed-read --gzip"
 
	# Check if there's files to exclude from the backup
	if [[ ! -e $BCK_CONF_DIR/$BCK_EXCLUDE_LIST ]]; then
		writeInfo "No file to exclude from the backup. (I0202)"
	else 
		bck_extra_options="$bck_extra_options -X $BCK_CONF_DIR/$BCK_EXCLUDE_LIST"	
	fi
 
	#Block the directory for uploading files while tar is working
	#echo "Directory locked on $BCK_TODAY" > $BCK_DAILY_DIR/.lock
 
	# Create archive 
	writeInfo "Creating archive. (I0203)"
	#tar -T $BCK_CONF_DIR/$BCK_BACKUP_LIST $bck_extra_options --listed-incremental=$bck_ss_file -cvf $BCK_TMP_DIR/$BCK_TAR_PREFIX$BCK_TODAY.tar &> $bck_tmp_log
	tar -T $BCK_CONF_DIR/$BCK_BACKUP_LIST $bck_extra_options --listed-incremental=$bck_ss_file -cvf $BCK_DAILY_DIR/$BCK_TAR_PREFIX$BCK_TODAY.$BCK_PARTIAL_COMPRESSED_EXTENSION &> $bck_tmp_log
	tarReturn=$?
 
	cat $bck_tmp_log >> $BCK_LOG_FILE
	catReturn=$?
	# Warn if cating files failed
	if [[ $catReturn -ne 0 ]]; then
		message="Cating file failed..."
		writeMail "$message"
		writeWarning "$message"
	fi
 
	# Stop if tar fails. Send a mail.
	if [[ $tarReturn -ne 0 ]]; then
		#Unlock the working directory
		#rm $BCK_DAILY_DIR/.lock
		message="Tar has failed. Check Disc space. (M/E0201)"
		#message="Tar has failed. You should be root to use Advanced Backup. (M/E0201)"
		writeMail "$message"
		writeError "$message"
	fi
 
	discDest=`whichDisc`
	if [[ ! $discDest ]]; then 
		discDest=$BCK_DEST_DISC_4
		writeWarning "Error while selecting proper destination disc!"
		writeInfo "Will copy on disc $discDest!"
	fi
	writeInfo "Backup will be performed on disc $discDest!"
 
	# Distant copy of the file.
	#sshCopy $BCK_TMP_DIR/$BCK_TAR_PREFIX$BCK_TODAY.$BCK_COMPRESSED_EXTENSION $BCK_REMOTE_PATH/$BCK_TAR_PREFIX$BCK_TODAY.tar $BCK_REMOTE_COMPUTER $BCK_REMOTE_USER 
	sshCopy $BCK_DAILY_DIR/$BCK_TAR_PREFIX$BCK_TODAY.$BCK_PARTIAL_COMPRESSED_EXTENSION $discDest/ $BCK_REMOTE_COMPUTER $BCK_REMOTE_USER 
	sshReturn=$?
 
	if [[ $sshReturn -ne 0 ]]; then
		#cp $BCK_TMP_DIR/$BCK_TAR_PREFIX$BCK_TODAY.$BCK_COMPRESSED_EXTENSION $BCK_LOCAL_PATH/
		#writeMail "Distant copy has failed. Copying to $BCK_LOCAL_PATH. (M/E0102)"
		#writeError  "Distant copy has failed. Copying to $BCK_LOCAL_PATH. (M/E0102)"
		message="Distant copy has failed. Please try manually. (M/E0102)"
		writeMail "$message"
		writeWarning "$message"
	else
		#message="Distant copy seems to be a success. Please check and if everything is ok please remove the local file\
		#manually. This is for testing purpose! And when no bugs can be automatically done!"
		message="Distant copy was a success. The locale file will be removed!"
		#writeMail "$message"
		writeInfo "$message"
		rm $BCK_DAILY_DIR/$BCK_TAR_PREFIX$BCK_TODAY.$BCK_PARTIAL_COMPRESSED_EXTENSION
		rmReturn=$?
		if [[ $rmReturn -ne 0 ]]; then
			message="Removing local file failed. Please try manually!"
			writeMail "$message"
			writeInfo "$message"
		else
			message="Removing local file was a success!"
			writeInfo "$message"
		fi
	fi	
 
	return 0
}
 
######################## Start script ########################
# Clean the screen and show that the program has started
#clear
writeInfo "Starting Advanced Backup. (I0000)"
 
checkLocalFolders 
 
# Modified 2005-05-13 ...
bck_full_backup_day=`date -d "$BCK_FULL_BACKUP_DAY" "+%w"`
 
if [[ $BCK_DAY_NUM -eq $bck_full_backup_day ]]; then
	createMainBackup
else
	createIncrementalBackup	
fi	
 
rm $BCK_TMP_DIR/*
now_time=`date`
writeInfo "Stop at : $now_time"
exit 0
######################### End script #########################

init_new_tape.sh

#!/bin/bash
#
# Author : Sylvain Bolay
# Date : 2005-06-28
#
# Description:
#  - This script is in charge of initializing the new cartridge inserted into the tape drive.
 
drive="/dev/st0"
answer='n'
status=1
 
printf "The new cartridge will be erased and rewinded! Do you want to continue (y/other): "
read answer
if [[ $answer = 'y' ]] || [[ $answer = 'Y' ]]; then
        echo	
	status=`mt --file=$drive erase`
	if [[ $status -eq 0 ]]; then
	  echo -e "Errasing -> OK\n"
	else 
	  echo "Error while errasing the tape! Please check!"
	  exit;	
	fi
 
	status=`mt --file=$drive rewind`
	if [[ $status -eq 0 ]]; then
	  echo -e "Rewinding -> OK\n"
	else 
	  echo "Error while rewinning the tape! Please check!"
	  exit;
	fi
 
        echo "Tape Status:"	
	mt --file=$drive status
	echo -e "\n\ndone!"
else
	echo "skipped!"
	exit;
fi

libabackup.sh

#!/bin/bash
#
# Author : Etienne Bagnoud
# Date : 2005-04-05
#
# Modified by : Sylvain Bolay
# Last Modification: 21.10.08
#
# Description :
#  - Contain some functions to create a backup script, like i/o function
#    and tape manipulation function.
#
 
if [[ -e $BCK_CONF_FILE ]]; then
	source $BCK_CONF_FILE
else
	echo "*** ERROR : Configuration file doesn't exist, can't work without it."
	exit 1
fi
 
# This function return the actual time. Format : "hh:mm:ss"
function getTime()
{
	echo `date +"%H:%M:%S"`	
	return 0
}
 
# 4 output functions
 
# Write an info message (stdin), like "the weather is nice today!"
function writeInfo
{
	bck_now=`getTime`
	echo "*** INFO : $1"
	echo -e "*** INFO\t$bck_now :\t$1" >> $BCK_LOG_FILE
	return 0
}
 
# Write a warning message (stdin) but dosen't stop
function writeWarning
{
	bck_now=`getTime`
	echo "*** WARN : $1"
	echo -e "*** WARN\t$bck_now :\t$1" >> $BCK_LOG_FILE
	return 0
}
 
# Write an error message (sdterr) and stop the program
function writeError
{
	bck_now=`getTime`
	echo "*** ERROR : $1" >&2
	echo -e "*** ERROR\t$bck_now :\t$1" >> $BCK_LOG_FILE
	rm $BCK_TMP_DIR/*
	exit 1
}
 
# Write a mail to the man specified in the config file (BCK_CONF_FILE).
function writeMail
{
	echo -e "Dear $BCK_ADMIN_NAME,\n\nAdvanced Backup has something to say to you :\n$1\n\nSee you soon.\nA.B." | mail -s "Advanced Backup (PROXY)" $BCK_ADMIN_EMAIL
	return 0
}
 
# Test tape device
function checkTape
{
	bck_tape=$1
	mt -f $bck_tape status
	return $?
}
 
# Get the end of the data on the tape
function getTapeSpace
{
	bck_tape=$1
 
	checkTape $bck_tape
	if [[ $? -ne 0 ]]; then
		return 1
	fi
 
	mt -f $bck_tape eom
	return $?
}
 
# Write tar file to tape
function writeTape
{
	bck_tar_file_path=$1
	bck_tape=$2
 
	if [[ ! -e $bck_tar_file_path ]]; then
		return 1
	fi
 
	# Check if there's tape
	checkTape $bck_tape
	if [[ $? -ne 0 ]]; then
		return 1
	fi
 
	# Go at the end the tape
	getTapeSpace $bck_tape
	if [[ $? -ne 0 ]]; then
		return 1
	fi
 
	# Writes data on the tape
	cat $bck_tar_file_path > $bck_tape
	if [[ $? -ne 0 ]]; then
		return 2
	fi
 
	return 0
}
 
# Function unload tape (rewind before)
function ejectTape
{
	bck_tape=$1
 
	mt -f $bck_tape rewind
 
	mt -f $bck_tape offline
	if [[ $? -ne 0 ]]; then
		return 1
	fi
 
	return 0
}
 
# Function to log on the station, expect won't return other value than 0, so ...
function sshAutolog
{
	bck_pass=$1
 
	# This code is not a part of abackup
	SSHAGENT=/usr/bin/ssh-agent 
	SSHAGENTARGS="-s"
	if [ -z "$SSH_AUTH_SOCK" -a -x "$SSHAGENT" ]; then
		eval `$SSHAGENT $SSHAGENTARGS` > /dev/null
	     	trap "kill $SSH_AGENT_PID" 0
	fi
	# End
 
	expect -c "spawn ssh-add; expect \"id_rsa:\"; send \"$bck_pass\n\"; expect eof"
	return 0
}
 
# Function to copy a file trough ssh ...
function sshCopy
{
	bck_file=$1
	bck_remote_file=$2
	bck_remote_server=$3
	bck_remote_user=$4
 
	#gzip $bck_file 
	scp $bck_file $bck_remote_user@$bck_remote_server:$bck_remote_file
 
	if [[ $? -ne 0 ]]; then
		return 1
	fi
 
	return 0
}
 
function checkLocalFolders
{
	msg="Checking Local Folders...\n"
	error=false
	#echo $BCK_SNAPSHOT_DIR $BCK_LOG_DIR $BCK_ABACKUP_DEST $BCK_TMP_DIR $BCK_DAILY_DIR $BCK_WEEKLY_DIR $BCK_LOCAL_PATH
	for directory in $BCK_SNAPSHOT_DIR $BCK_LOG_DIR $BCK_ABACKUP_DEST $BCK_TMP_DIR $BCK_DAILY_DIR $BCK_WEEKLY_DIR $BCK_LOCAL_PATH
	do
		# bash check if directory exists
		if [[ -d $directory ]]; then
			msg=$msg+"Directory $directory exists\n"
		else 
			msg=$msg+"Directory $directory does not exists. Creating..."
			mkdir $directory
			if [ -d $directory ]; then
				msg=$msg+" OK\n"
			else
				msg=$msg+" FAILED\n"
				error=true
			fi
		fi
	done
 
	if $error; then
		writeMail $msg
		writeError $msg
		return 1
	else
		return 0
	fi
 
	#PARANOIA
	return 1
}

uploadFile.sh

#!/bin/bash
#
# Author : Etienne Bagnoud
# Date : 2005-04-08
#
# Modified by : Sylvain Bolay
# Last Modification: 09.12.05
#
# Description :
#  - Script that do the backup. It's made of functions, 'cause it's easier
#    to call a function than a script if the main backup is missing.
#
 
if [[ -e $BCK_CONF_FILE ]]; then
	source $BCK_CONF_FILE
else
	echo "*** ERROR : Configuration file doesn't exist, can't work without it. (E0000)"
	exit 1
fi
 
if [[ -e $BCK_SCRIPT_DIR/libabackup.sh ]]; then
	source $BCK_SCRIPT_DIR/libabackup.sh 
else
	echo "*** ERROR : Library is missing. Can't work without it. (E0001)"
	exit 1
fi	
 
now_time=`date`
writeInfo "Start at : $now_time"
 
# Configure l'auto login sur une machine distante.
sshAutolog "$BCK_REMOTE_PASS"
 
# Function that choose the destination disc in function of the week number
function whichDisc()
{
	case $BCK_WEEK_NUM in
		1|5|9|13|17|21|25|29|33|37|41|45|49|53)	echo "$BCK_DEST_DISC_1"; return;;
		2|6|10|14|18|22|26|30|34|38|42|46|50)	echo "$BCK_DEST_DISC_2"; return;;
		3|7|11|15|19|23|27|31|35|39|43|47|51)	echo "$BCK_DEST_DISC_3"; return;;
#		4|8|12|16|20|24|28|32|36|40|44|48|52|0) return 4;;
		*)					echo "$BCK_DEST_DISC_4"; return;;
	esac
}
 
 
######################## Start script ########################
 
# Clean the screen and show that the program has started
clear
writeInfo "Starting Advanced Backup. (I0000)"
 
# Modified 2005-05-13 ...
bck_full_backup_day=`date -d "$BCK_FULL_BACKUP_DAY" "+%w"`
 
 
	discDest=`whichDisc`
	if [[ ! $discDest ]]; then 
		discDest=$BCK_DEST_DISC_4
		writeWarning "Error while selecting proper destination disc!"
		writeInfo "Will copy on disc $discDest!"
	fi
	writeInfo "Backup will be performed on disc $discDest!"
 
	# Distant copy of the file.
	#sshCopy $BCK_TMP_DIR/$BCK_TAR_PREFIX$BCK_TODAY.$BCK_COMPRESSED_EXTENSION $BCK_REMOTE_PATH/$BCK_TAR_PREFIX$BCK_TODAY.tar $BCK_REMOTE_COMPUTER $BCK_REMOTE_USER 
	sshCopy $BCK_WEEKLY_DIR/$BCK_TAR_PREFIX$BCK_TODAY.$BCK_COMPRESSED_EXTENSION $discDest/$BCK_TAR_PREFIX$BCK_TODAY.$BCK_COMPRESSED_EXTENSION $BCK_REMOTE_COMPUTER $BCK_REMOTE_USER 
	sshReturn=$?
 
	if [[ $sshReturn -ne 0 ]]; then
		#cp $BCK_TMP_DIR/$BCK_TAR_PREFIX$BCK_TODAY.$BCK_COMPRESSED_EXTENSION $BCK_LOCAL_PATH/
		#writeMail "Distant copy has failed. Copying to $BCK_LOCAL_PATH. (M/E0102)"
		#writeError  "Distant copy has failed. Copying to $BCK_LOCAL_PATH. (M/E0102)"
		message="Distant copy has failed. Please try manually. (M/E0102)"
		writeMail "$message"
		writeWarning "$message"
	else
		#message="Distant copy seems to be a success. Please check and if everything is ok please remove the local file\
		#manually. This is for testing purpose! And when no bugs can be automatically done!"
		message="Distant copy was a success. The locale file will be removed!"
		#writeMail "$message"
		writeInfo "$message"
		rm $BCK_WEEKLY_DIR/$BCK_TAR_PREFIX$BCK_TODAY.$BCK_COMPRESSED_EXTENSION
		rmReturn=$?
		if [[ $rmReturn -ne 0 ]]; then
			message="Removing local file failed. Please try manually!"
			writeMail "$message"
			writeInfo "$message"
		else
			message="Removing local file was a success!"
			writeInfo "$message"
		fi
	fi	
 
 
 
	cat $bck_tmp_log >> $BCK_LOG_FILE
	catReturn=$?
	# Warn if cating files failed
	if [[ $catReturn -ne 0 ]]; then
		message="Cating file failed..."
		writeMail "$message"
		writeWarning "$message"
	fi
 
 
 
 
now_time=`date`
writeInfo "Stop at : $now_time"
exit 0
######################### End script #########################