added restore
This commit is contained in:
parent
6512f81d54
commit
805449870e
109
btrbk
109
btrbk
|
@ -5,7 +5,7 @@ startDate=$(date +%s)
|
||||||
|
|
||||||
function bkRun {
|
function bkRun {
|
||||||
local date=$(date +%Y-%m-%d)
|
local date=$(date +%Y-%m-%d)
|
||||||
local month=$(date +%Y-%B)
|
local month=$(date +%Y-%m)
|
||||||
local bkSuffix=("${INCR[0]}" "${FULL[0]}")
|
local bkSuffix=("${INCR[0]}" "${FULL[0]}")
|
||||||
local keepTime=("${INCR[1]}" "${FULL[1]}")
|
local keepTime=("${INCR[1]}" "${FULL[1]}")
|
||||||
for i in ${!SUBVOLS[@]}; do
|
for i in ${!SUBVOLS[@]}; do
|
||||||
|
@ -19,11 +19,11 @@ function bkRun {
|
||||||
sync
|
sync
|
||||||
fi
|
fi
|
||||||
if [[ -d "$snapLast" && $1 = "incremental" ]]; then
|
if [[ -d "$snapLast" && $1 = "incremental" ]]; then
|
||||||
btrfs send -p $snapLast $SNAPDIR$snapName | gpg --batch -er $PUBKEY -o - | rclone rcat $RCLONEDIR$month"-incr/"$snapName.pgp
|
btrfs send -p $snapLast $SNAPDIR$snapName | gpg --batch -er $PUBKEY -o - | rclone rcat $RCLONEDIR$month"-${INCR[0]}/"$snapName.pgp
|
||||||
else
|
else
|
||||||
btrfs send $SNAPDIR$snapName | gpg --batch -er $PUBKEY -o - | rclone rcat $RCLONEDIR$month"-full/"$snapName.pgp
|
btrfs send $SNAPDIR$snapName | gpg --batch -er $PUBKEY -o - | rclone rcat $RCLONEDIR$month"-${FULL[0]}/"$snapName.pgp
|
||||||
if [[ $1 = "full" ]]; then
|
if [[ $1 = "full" ]]; then
|
||||||
for o in ${!bkName[@]}; do
|
for o in ${!bkSuffix[@]}; do
|
||||||
mapfile -t oldBk < <(rclone lsf $RCLONEDIR | grep ....-..-${bkSuffix[$o]} | head -n -${keepTime[$o]})
|
mapfile -t oldBk < <(rclone lsf $RCLONEDIR | grep ....-..-${bkSuffix[$o]} | head -n -${keepTime[$o]})
|
||||||
for n in ${!oldBk[@]}; do
|
for n in ${!oldBk[@]}; do
|
||||||
rclone delete $RCLONEDIR${oldBk[$n]}
|
rclone delete $RCLONEDIR${oldBk[$n]}
|
||||||
|
@ -31,29 +31,65 @@ function bkRun {
|
||||||
done
|
done
|
||||||
fi
|
fi
|
||||||
fi
|
fi
|
||||||
mapfile -t oldSnaps < <(ls -a $SNAPDIR | grep ${VOLNAME[$i]}.\....-..- | head -n -3)
|
mapfile -t oldSnaps < <(ls -a $SNAPDIR | grep ${VOLNAME[$i]}.\....-..- | head -n -$KEEPLOCAL)
|
||||||
for snap in ${!oldSnaps[@]}; do
|
for snap in ${!oldSnaps[@]}; do
|
||||||
btrfs subvolume delete $SNAPDIR${oldSnaps[$snap]}
|
btrfs subvolume delete $SNAPDIR${oldSnaps[$snap]}
|
||||||
done
|
done
|
||||||
done
|
done
|
||||||
|
rclone copy /etc/btrbk.conf $RCLONEDIR"config/"
|
||||||
end
|
end
|
||||||
}
|
}
|
||||||
|
|
||||||
function runRestore {
|
function runRestore {
|
||||||
if [ -z ${rDate+x} ]; then
|
if [ -z ${rDate+x} ]; then
|
||||||
for o in ${!VOLNAME[@]}; do
|
for o in ${!VOLNAME[@]}; do
|
||||||
if [[ "${VOLNAME[$o]}" = "${rest}" ]]; then
|
if [[ "${VOLNAME[$o]}" = "${restoreVol}" ]]; then
|
||||||
getSnap "$o"
|
getSnap "$o"
|
||||||
break
|
break
|
||||||
elif [[ ! "${VOLNAME[@]}" =~ "${rest}" ]]; then
|
elif [[ ! "${VOLNAME[@]}" =~ "${restoreVol}" ]]; then
|
||||||
echo "Subvolume doesnt exist"
|
echo "Subvolume doesnt exist"
|
||||||
exit
|
exit 1
|
||||||
fi
|
fi
|
||||||
done
|
done
|
||||||
mount /dev/mappaer/hideri /mnt/btrfsroot
|
# mkdir /mnt/$BTRFSUUID
|
||||||
btrfs
|
# mount -o subvolid=1 -U $BTRFSUUID /mnt/$BTRFSUUID
|
||||||
|
# btrfs subvolume delete /mnt/$BTRFSUUID/
|
||||||
fi
|
fi
|
||||||
|
echo "not implemented yet™"
|
||||||
|
exit 1
|
||||||
|
}
|
||||||
|
|
||||||
|
function runFullRestore {
|
||||||
|
read -r -p "Do You want to run rclone config?[y/N]: " rcloneConf
|
||||||
|
case "$rcloneConf" in
|
||||||
|
[yY]) rclone config;;
|
||||||
|
*) ;;
|
||||||
|
esac
|
||||||
|
echo "specify the remote path with trailing slash [remote:/path/to/backupdir/]"
|
||||||
|
read -r -p "Dir: " rcloneDir
|
||||||
|
rclone copy $rcloneDir"config/btrbk.conf" /etc/btrbk.conf
|
||||||
|
echo "Supply Path of your gpg public and secret key"
|
||||||
|
read -r -p "Publickey: " gpgPub
|
||||||
|
read -r -p "Secretkey: " gpgPriv
|
||||||
|
gpg --import $gpgPub
|
||||||
|
gpg --import $gpgPriv
|
||||||
|
|
||||||
|
source /etc/btrbk.conf
|
||||||
|
local bkSuffix=("${INCR[0]}" "${FULL[0]}")
|
||||||
|
btrfs subvolume create $restoreDir"snapshots"
|
||||||
|
lastBk=$(rclone lsf $RCLONEDIR | grep ....-..-${FULL[0]} | tail -n -1 | sed "s/\/$//")
|
||||||
|
if rclone lsf $RCLONEDIR$($lastBk | sed "s/${FULL[0]}/${INCR[0]}") > /dev/null 2>&1; then
|
||||||
|
for i in ${!bkSuffix[@]}; do
|
||||||
|
mapfile -t bkFile < <(rclone lsf "$RCLONEDIR$lastBk${bkSuffix[$i]}/")
|
||||||
|
for o in ${!bkFile[@]}; do
|
||||||
|
rclone rcat "$RCLONEDIR$lastBk${bkSuffix[$i]}/${bkFile[$o]}" | gpg --batch -d $PUBKEY -o - | btrfs recive $restoreDir"snapshots"
|
||||||
|
done
|
||||||
|
done
|
||||||
|
for i in ${!SUBVOLS[@]}; do
|
||||||
|
|
||||||
|
rclone rcat /mnt/bk/root-bk.gpg | gpg --batch -d --passphrase-file /mnt/bk/gpgpass -o - | btrfs receive /mnt/restore
|
||||||
|
done
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function getSnap {
|
function getSnap {
|
||||||
|
@ -65,24 +101,31 @@ function getSnap {
|
||||||
done
|
done
|
||||||
}
|
}
|
||||||
|
|
||||||
function testconf {
|
|
||||||
if rclone ls $RCLONEDIR > /dev/null 2>&1; then
|
function testConf {
|
||||||
if gpg --list-key $PUBKEY > /dev/null 2>&1; then
|
if [ -z ${rDate+x} ]; then
|
||||||
if [ $fullRestore=1 ]; then
|
if ! date -d "$rDate" > /dev/null 2>&1; then
|
||||||
runFull
|
echo "date is in wrong format: YYYY-MM-DD"
|
||||||
elif [ $restore=1 ]; then
|
exit 1
|
||||||
runRestore
|
|
||||||
else
|
|
||||||
testDate
|
|
||||||
fi
|
fi
|
||||||
else
|
|
||||||
echo "pubkey wrong or not found exit code: $?"
|
|
||||||
exit
|
|
||||||
fi
|
fi
|
||||||
else
|
if rclone ls $RCLONEDIR > /dev/null 2>&1; then
|
||||||
echo "rclone dir wrong or not found exit code: $?"
|
if gpg --list-key $PUBKEY > /dev/null 2>&1; then
|
||||||
exit
|
if [ $fullRestore=1 ]; then
|
||||||
fi
|
runFullRestore
|
||||||
|
elif [ $restore=1 ]; then
|
||||||
|
runRestore
|
||||||
|
elif [ $backup=1 ]; then
|
||||||
|
testDate
|
||||||
|
fi
|
||||||
|
else
|
||||||
|
echo "pubkey wrong or not found exit code: $?"
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
else
|
||||||
|
echo "rclone dir wrong or not found exit code: $?"
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
function testDate {
|
function testDate {
|
||||||
|
@ -97,7 +140,7 @@ function end {
|
||||||
echo "backup finished in $(( ($(date +%s) - startDate) / 60 )) min"
|
echo "backup finished in $(( ($(date +%s) - startDate) / 60 )) min"
|
||||||
}
|
}
|
||||||
|
|
||||||
function Help {
|
function echoHelp {
|
||||||
echo "Usage: btrbk [options]"
|
echo "Usage: btrbk [options]"
|
||||||
echo "A script to automatically or manually backup your btrfs subvolumes."
|
echo "A script to automatically or manually backup your btrfs subvolumes."
|
||||||
echo "options:"
|
echo "options:"
|
||||||
|
@ -105,9 +148,10 @@ function Help {
|
||||||
echo " -f, --force force backup to override today's backup"
|
echo " -f, --force force backup to override today's backup"
|
||||||
echo " -d, --date=YYYY-MM-DD define a date from which to restore when using -r"
|
echo " -d, --date=YYYY-MM-DD define a date from which to restore when using -r"
|
||||||
echo " -r, --restore=VOLNAME restores the latest snapshot of a given subvolume"
|
echo " -r, --restore=VOLNAME restores the latest snapshot of a given subvolume"
|
||||||
echo " -x, --full-restore=PATH this walks you through an complete config restore
|
echo " -x, --full-restore=PATH this walks you through an complete config restore"
|
||||||
and will restore the whole system to a given directory"
|
echo " and will restore the whole system to a given directory"
|
||||||
echo " -h, --help display this help and exit"
|
echo " -h, --help display this help and exit"
|
||||||
|
exit 1
|
||||||
}
|
}
|
||||||
|
|
||||||
options=$(getopt -l "backup,force,date:,restore:,full-restore:,help" -o "bfd:r:x:h" -- "$@")
|
options=$(getopt -l "backup,force,date:,restore:,full-restore:,help" -o "bfd:r:x:h" -- "$@")
|
||||||
|
@ -120,9 +164,10 @@ while true; do
|
||||||
-r|--restore) restore=1; restoreVol=$2; shift;;
|
-r|--restore) restore=1; restoreVol=$2; shift;;
|
||||||
-x|--full-restore) fullRestore=1; restoreDir=$2; shift;;
|
-x|--full-restore) fullRestore=1; restoreDir=$2; shift;;
|
||||||
-h|--help) Help; shift; break;;
|
-h|--help) Help; shift; break;;
|
||||||
--) shift; break;;
|
--) shift;;
|
||||||
\?) echo "error"; exit 1;;
|
\?) echo "error"; exit 1;;
|
||||||
|
*) echoHelp; shift; break;;
|
||||||
esac
|
esac
|
||||||
shift
|
shift
|
||||||
done
|
done
|
||||||
|
testConf
|
||||||
|
|
|
@ -10,12 +10,18 @@ SUBVOLS=("/" "/home")
|
||||||
#VOLNAME has to be in the same order as SUBVOLS to be interpreted correctly
|
#VOLNAME has to be in the same order as SUBVOLS to be interpreted correctly
|
||||||
VOLNAME=("root" "home")
|
VOLNAME=("root" "home")
|
||||||
|
|
||||||
|
#UUID of the Disk with the btrfs root
|
||||||
|
BTRFSUUID="d1b20b8a-3cf4-41f7-bdc8-d50d53dc77c2"
|
||||||
|
|
||||||
#btrfs volume too keep snapshots in
|
#btrfs volume too keep snapshots in
|
||||||
SNAPDIR="/.snapshots/"
|
SNAPDIR="/.snapshots/"
|
||||||
|
|
||||||
#Rclone Directory on remote storage
|
#Rclone Directory on remote storage
|
||||||
RCLONEDIR="test:/mnt/bk/new/"
|
RCLONEDIR="test:/mnt/bk/new/"
|
||||||
|
|
||||||
|
#How many local snapshots should be kept
|
||||||
|
KEEPLOCAL="10"
|
||||||
|
|
||||||
#Suffix name for incremental/full backups and retention
|
#Suffix name for incremental/full backups and retention
|
||||||
#example: YYYY-mm-full and will keep the last two backups
|
#example: YYYY-mm-full and will keep the last two backups
|
||||||
#For full Backups dont set retention below 1 or you will always delete your current backup which renders all following incremental backups useless
|
#For full Backups dont set retention below 1 or you will always delete your current backup which renders all following incremental backups useless
|
||||||
|
|
Loading…
Reference in a new issue