66 lines
2.1 KiB
Bash
Executable file
66 lines
2.1 KiB
Bash
Executable file
#!/bin/bash
|
|
|
|
source config.sh
|
|
startDate=$(date +%s)
|
|
|
|
function main {
|
|
date=$(date +%Y-%m-%d)
|
|
month=$(date +%Y-%B)
|
|
for i in ${!SUBVOLS[@]}; do
|
|
snapName=${VOLNAME[$i]}.$date
|
|
snapLast=$SNAPDIR${VOLNAME[$i]}.$(date -d "$date -1 day" +%Y-%m-%d)
|
|
if ! [[ -d "$snapLast" ]]; then
|
|
snapLast=$SNAPDIR$(ls -a $SNAPDIR | grep ${VOLNAME[$i]}.$(date -d "$date" +%Y-%m)- | tail -n 1)
|
|
fi
|
|
if [[ -d "$SNAPDIR$snapName" ]]; then
|
|
echo "backup already run today"
|
|
continue
|
|
else
|
|
btrfs subvolume snapshot -r ${SUBVOLS[$i]} $SNAPDIR$snapName
|
|
fi
|
|
if [[ -d "$snapLast" && $bkType = "daily" ]]; then
|
|
btrfs send -p $snapLast $SNAPDIR$snapName | gpg --batch -er $PUBKEY -o - | rclone rcat $RCLONEDIR$month"-incr/"$snapName.pgp
|
|
else
|
|
btrfs send $SNAPDIR$snapName | gpg --batch -er $PUBKEY -o - | rclone rcat $RCLONEDIR$month"-full/"$snapName.pgp
|
|
if [[ $bktype = monthly ]] && rclone ls $RCLONEDIR$(date -d "$date -1 month" +%Y-%B)-incr; then
|
|
rclone delete $RCLONEDIR$(date -d "$date -1 month" +%Y-%B)-incr/
|
|
if rclone ls $RCLONEDIR$(date -d "$date -6 month" +%Y-%B)-full/; then
|
|
rclone delete $RCLONEDIR$(date -d "$date -6 month" +%Y-%B)-full/
|
|
fi
|
|
fi
|
|
fi
|
|
mapfile -t oldSnaps < <(ls -a $SNAPDIR | grep ${VOLNAME[$i]}.\....-..- | head -n -3)
|
|
for snap in ${!oldSnaps[@]}; do
|
|
btrfs subvolume delete $SNAPDIR${oldSnaps[$snap]}
|
|
done
|
|
done
|
|
end
|
|
}
|
|
|
|
function testDate {
|
|
if [ `date +%d` != "01" ]; then
|
|
bkType=daily
|
|
main
|
|
else
|
|
bkType=monthly
|
|
main
|
|
fi
|
|
}
|
|
|
|
function end {
|
|
echo "backup finished in $(( ($(date +%s) - startDate) / 60 )) min"
|
|
}
|
|
|
|
|
|
if rclone ls $RCLONEDIR > /dev/null 2>&1; then
|
|
if gpg --list-key $PUBKEY > /dev/null 2>&1; then
|
|
testDate
|
|
else
|
|
echo "pubkey wrong or not found exit code: $?"
|
|
exit
|
|
fi
|
|
else
|
|
echo "rclone dir wrong or not found exit code: $?"
|
|
exit
|
|
fi
|