#!/bin/bash
# Create BACKUP file in backup destination and add to cron
# Error codes
# 1 - Unknown error
# 1xx - fs problems
# 2xx - backup failed
ERR_100="Error $1: Backup not mounted"
ERR_110="Error $1: Target directories not available"
ERR_200="Error $1: Backup failed"
targets_to_backup="/home/work/scripts/
/home/work/something/
/home/work/notes/
/home/work/somethingelse/
"
backup_destination="/mnt/backup/mybox/"
function backup_not_mounted
{
# Is backup mounted?
if [[ ! -f "$backup_destination/BACKUP" ]]; then
backup_not_mounted_return=100
else
backup_not_mounted_return=0
fi
}
function target_not_available
{
SAVEIFS=$IFS
IFS=$(echo -en "\n\b")
for target in $targets_to_backup
do
if [[ ! -d "$target" ]]; then
target_not_available_return=110
else
target_not_available_return=0
fi
done
IFS=$SAVEIFS
}
function exit_error
{
case "$1" in
100) echo $ERR_100
exit $1
;;
110) echo $ERR_110
exit $1
;;
200) echo $ERR_200
exit $1
;;
0)
;;
*) echo "Unknown error $1"
exit 1
;;
esac
}
function do_backup
{
SAVEIFS=$IFS
IFS=$(echo -en "\n\b")
for target in $targets_to_backup
do
nice -n 19 ionice -c 3 rsync -avz -R "$target" "$backup_destination"
if [[ ! -d "$target" ]]; then
do_backup_return=200
break
else
do_backup_return=0
fi
done
IFS=$SAVEIFS
}
function main
{
backup_not_mounted
exit_error $backup_not_mounted_return
target_not_available
exit_error $target_not_available_return
do_backup
exit_error $do_backup_return
}
# RUN!
main