I don't mind posting it, but note that I known nothing about scripting, I'm just good a googling and finding examples of what I want to do, so the script is very crude and while it works great for me and my use case it likely won't for other use cases, also:
-send/receive has currently no way of showing progress/transfer size, so I do it by using pv after comparing the used sized on both servers, obviously this will only be accurate if both servers contain the same data, including the same snapshots, i.e., when I delete old snapshots on source I also delete them on destination.
-you'll need to pre-create the ssh keys.
-if any of the CPUs doesn't have hardware AES support remove "-c
[email protected]" from the SSH options.
-for the script to work correctly the most recent snapshot (the one used as parent for the incremental btrfs send) must exist on source and destination, so the initial snapshot for all disks needs to be sent manually, using the same name format.
#!/bin/bash
#Snapshot date format
nd=$(date +%Y-%m-%d-%H%M)
#Dest IP Address
ip="192.168.1.24"
#Share to snapshot and send/receive
sh=TV
#disks that have share to snapshot and send/receive
for i in {1..28} ; do
#calculate and display send size
s=$(BLOCKSIZE=1M df | grep -w disk$i | awk '/[0-9]%/{print $(3)}')
su=$(BLOCKSIZE=1M df | grep -w user | awk '/[0-9]%/{print $(3)}')
d=$(ssh root@$ip BLOCKSIZE=1M df | grep -w disk$i | awk '/[0-9]%/{print $(3)}')
du=$(ssh root@$ip BLOCKSIZE=1M df | grep -w user | awk '/[0-9]%/{print $(3)}')
t=$((s-d))
if [ "$t" -lt 0 ] ; then ((t = 0)) ; fi
g=$((t/1024))
tu=$((su-du))
if [ "$tu" -lt 0 ] ; then ((tu = 0)) ; fi
gu=$((tu/1024))
echo -e "\e[32mTotal transfer size for disk$i is ~"$g"GiB, total remaining for this backup is ~"$gu"GiB\e[0m"
#source snaphots folder
cd /mnt/disk$i/snaps
#get most recent snapshot
sd=$(echo $sh_* | awk '{print $NF}')
#make a new snapshot and send differences from previous one
btrfs sub snap -r /mnt/disk$i/$sh /mnt/disk$i/snaps/"$sh"_$nd
sync
btrfs send -p /mnt/disk$i/snaps/$sd /mnt/disk$i/snaps/"$sh"_$nd | pv -prtabe -s "$t"M | ssh -c
[email protected] root@$ip "btrfs receive /mnt/disk$i"
if [[ $? -eq 0 ]]; then
ssh root@$ip sync
echo -e "\e[32mdisk$i send/receive complete\e[0m"
printf "\n"
else
echo -e "\e[31mdisk$i send/receive failed\e[0m"
/usr/local/emhttp/webGui/scripts/notify -i warning -s "disk$i send/receive failed"
fi
done
/usr/local/emhttp/webGui/scripts/notify -i normal -s "T5>T6 Sync complete"