#!/bin/bash
#
#-------------- Config -------------------#
#
# backup destination dir
destdir="/data/backup"
# split backup to multiple parts
splitsize="3900m" #~4GB
# the disk that holds the MBR
disk="/dev/sda"
# check free space on backup medium
checkspace="true"
destpercentminfree="25"
# which dirs to exclude from backup
excludes=(/mnt /media /data /data2 /home/xbmc/.ccache /var/cache /var/log /opt/xbmc.src)
# extra options for tar
tarverbose="v" #"v" = verbose. leave blank for no verbosity
extrataropts=""
#---------------- END CONFIG ---------------------#
###
# Functions
###
function usage {
echo "usage: sudo $0"
#exit 0
}
function checkRoot {
if [[ $(id -u) != 0 ]]
then
echo "you must run this as root"
usage
exit 10
fi
}
function checkBackupDir {
cd $destdir || exit 11
if [ -f $filename* ]
then
echo "$filename exists"
exit 1
fi
if [ "$checkspace" != "false" ]
then
rootsize=$(df -k |grep "/$" | awk '{ print $3 }')
destfree=$(df -k . |tail -1 | awk '{ print $3 }')
tmp=$(echo "scale=2; ($destfree / $rootsize) * 100" | bc)
destpercentfree=${tmp%\.00}
echo "% free space on $destdir : $destpercentfree "
if [ $destpercentfree -lt $destpercentminfree ]
then
echo "Warning: free space on $destdir is lower then 25% of used space on /"
echo "free up space or set checkspace=\"false\" to ignore"
exit 12
fi
fi
}
function backupMBR {
cd ${destdir} || exit 11
echo "#-------- backing up MBR ----------#"
dd if=${disk} of=backup-${disk#/dev/}_${date}.mbr count=1 bs=512
ERR=$?
[ $ERR -eq 0 ] && echo "#----- backup successful ---------#"
}
function backupParttable {
cd ${destdir} || exit 11
echo "#--- backing up Partition Table ---#"
sfdisk -d ${disk} > backup-${disk#/dev/}_${date}.sf
ERR=$?
[ $ERR -eq 0 ] && echo "#----- backup successful ---------#"
}
function backupData {
excludestring=$(echo "" ${excludes[*]} | sed -r 's/\s+/ --exclude=/g')
cd ${destdir} || exit 11
echo "#-------- backing up data----------#"
#$X tar -c${tarverbose}pzf ${filename} --exclude=/${filename} \
tar -c${tarverbose}pz --exclude=${filename} --exclude=$PWD \
--exclude=/proc --exclude=/lost+found --exclude=/sys $excludestring \
$extrataropts / | split -d -b ${splitsize} - ${filename}. | grep -v "exit delayed"
ERR=$?
}
###
# Main
###
date=$(date '+%Y_%m_%d')
filename="backup_$(hostname)_${date}.tar.gz"
checkRoot
checkBackupDir
backupMBR
backupParttable
backupData
exit 0
#+ DO NOT UNCOMMENT ANYTHING BELOW, THIS COULD BREAK YOUR SYSTEM
#################
# How to restore
#################
#+ DO NOT UNCOMMENT ANYTHING BELOW, THIS COULD BREAK YOUR SYSTEM
#
#+ Restore will eventually get its own script, till then here is the manual procedure - not for newbies. if unsure ask on the forum
#
#+ Be careful, restoring is a dangerous action - it can destroy data!
#
#+ restore the Master Boot Record:
#dd if=backup-sda.mbr of=/dev/sda
#
#+ here is how to restore extended partitions entries:
#sfdisk /dev/sda < backup-sda.sf
#
#+ restore data:
# cd /
# sudo tar xzvf backup_$(hostname).tar.gz
# or if split:
# cat *tar.gz* | tar -xvpzf - -C /


LinkBack URL
About LinkBacks
