Some legacy scripts that I still use but will be decommissioned long term
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

203 lines
3.8 KiB

#!/bin/bash
# Load config
if [ ! -f /etc/default/backupfs ]; then
echo "/etc/default/backupfs missing"
exit 1
fi
. /etc/default/backupfs
declare -A dumptool
dumptool[ext2]=dump
dumptool[ext3]=dump
dumptool[ext4]=dump
dumptool[btrfs]=btrfs
adjustpath ()
{
havesbin=1
haveusrsbin=1
for i in `echo $PATH|tr : " "`;do
[ "$i" == "/sbin" ] && havesbin=0
[ "$i" == "/usr/sbin" ] && haveusrsbin=0
done
[ $havesbin -eq 0 ] || export PATH="/sbin:$PATH"
[ $haveusrsbin -eq 0 ] || export PATH="/usr/sbin:$PATH"
}
checkdeps ()
{
which curl > /dev/null
if [ $? -ne 0 ]; then
echo "curl missing, existing"
exit 1
fi
which gpg > /dev/null
if [ $? -ne 0 ]; then
echo "gpg missing, existing"
exit 1
fi
for fs in "${!dumptool[@]}"; do
if [ -n "`mount -t $fs`" ]; then
which ${dumptool[$fs]} > /dev/null
if [ $? -ne 0 ]; then
echo "You have a $fs filesystem but ${dumptool[$fs]} is missing"
exit 1
fi
fi
done
}
checkval ()
{
if [ -z "$url" ]; then
echo "No target url specified, exiting"
exit 1
fi
if [ -z "$encryptto" ]; then
echo "No encrypt to specified, exiting"
exit 1
fi
gpg --list-key "$encryptto" > /dev/null
if [ $? -ne 0 ]; then
gpg --recv-keys "$encryptto"
gpg --list-key "$encryptto" > /dev/null
if [ $? -ne 0 ]; then
echo "PGP key missing"
exit 1
fi
fi
}
lock ()
{
PID=`cat ~/.backuplock 2> /dev/null`
if [ -n "$PID" ]; then
if [ -d "/proc/$PID" ]; then
if [ "`readlink /proc/$PID/exe`" == "/bin/bash" ]; then
echo "Locked, exiting"
exit 1
fi
fi
fi
echo -n $$ > ~/.backuplock
}
fsdetect ()
{
local mountpoint=${1}
local device="`mount |grep " on $mountpoint "|cut -d\ -f1`"
fstype=`/sbin/blkid -o value -s TYPE ${device}`
}
mptobname ()
{
# mountpoint
bname="`echo ${1}|tr / _`"
}
mptofname ()
{
# mountpoint, level, extension
hn=`hostname`
fname="`echo ${hn}-${date}-${1}-${2}.${3}|tr / _`"
}
checkbtrfs ()
{
true
}
docurl ()
{
local source="${1}"
local destination="${2}"
curl --no-buffer --data-binary @"${source}" -XPUT $url/"${destination}"
}
dumpbtrfs ()
{
mountpoint=${1}
checkbtrfs
mptobname ${mountpoint} ${level}
# TODO del if exists
btrfs subvolume snapshot -r ${mountpoint} /snapshot/current
btrfs subvolume snapshot -r /snapshot/current /snapshot/$bname-$level
btrfs subvolume delete /snapshot/current
fname="${hostname}-${date}-${bname}-${level}.snap.gpg"
if [ $level -eq 0 ]; then
#btrfs send /snapshots/$bname-$level | xz -6e -T0|gpg --encrypt -r dev@mailchuck.com " | docurl "${fname}"
true
else
plevel=$(($level - 1))
#btrfs send -p /snapshots/$bname-$plevel /snapshots/$bname-$level | xz -6e -T0|gpg --encrypt -r $encryptto | docurl "${fname}"
true
fi
}
dumpextfs ()
{
mountpoint=${1}
mptobname ${mountpoint} ${level}
fname="${hostname}-${date}-${bname}-${level}.dump.gpg"
tmp_file="$(mktemp)"
echo "dumping $mountpoint into $fname (temp $tmp_file)"
dump -${level}uj -h 0 ${mountpoint} -f -|gpg --encrypt -r $encryptto > "${tmp_file}"
docurl "${tmp_file}" "${fname}"
rm -f "${tmp_file}"
}
dodump ()
{
for fs in "${!dumptool[@]}"; do
for mpt in `mount -t $fs|cut -d\ -f3`; do
if [ -z "$mpt" ]; then
continue
fi
echo "Dumping $mpt as $fs"
if [ "$fs" == "btrfs" ]; then
dumpbtrfs $mpt $level
elif [ "$fs" == "ext2" -o "$fs" == "ext3" -o "$fs" == "ext4" ]; then
dumpextfs $mpt $level
fi
done
done
}
adjustpath
checkdeps
checkval
umask 077
renice 19 $$ &> /dev/null
ionice -c 3 -p $$
lock
#date=`date +%y-%m-%d`
hostname=`hostname|cut -d. -f1`
dom=`date +%d`
dow=`date +%w`
date=`date +%y%m%d`
level=$(($dow+1))
if [ $dow -eq 0 ];then # sunday
if [ $dom -le 7 ];then # first sunday in month
level=0
else
level=1
fi
fi
if [ "$1" == "0" -o "$1" == "1" -o "$1" == "2" ]
then
level="$1"
fi
dodump
rm ~/.backuplock
echo "All OK"
exit 0
# vim: tabstop=2