zfs-backup/backup.sh
2022-11-19 03:51:26 -05:00

87 lines
2.1 KiB
Bash
Executable File

#!/usr/bin/env sh
### nodes.txt
# localhost 22 <this host's root zfs> <this host's user>
# <remote host 1 address> <host 1 port> <host 1 root zfs>
# <remote host 2 address> <host 2 port> <host 2 root zfs>
# ...
### datasets.txt (Only required for aggregator setup.)
# <dataset 1>
# <dataset 1>/<dataset 1a>
# <dataset 1>/<dataset 1b>
# <dataset 2>
# <dataset 2>/<dataset 2a>
# <dataset 3>
# ...
if [ ! -f nodes.txt ]; then echo "No nodes specified."; exit 2; fi
read BR_HOST BR_PORT BR_ROOT BR_USER < nodes.txt
list_filesets()
{
zfs list | tail -n +2 | cut -d' ' -f1|grep ${BR_ROOT}
}
get_latest_snapshot()
{
zfs list -t snapshot |
grep ${BR_ROOT} |
tail -n1 |
cut -d'@' -f2 |
cut -d' ' -f 1
}
setup_aggregator()
{
zfs allow -u ${BR_USER} send,snapshot,hold ${BR_ROOT}
for dataset in `cat datasets.txt`; do
zfs create $dataset
done
}
setup_mirror()
{
zfs allow -u ${BR_USER} send,snapshot,hold ${BR_ROOT}
zfs allow -du ${BR_USER} \
compression,mountpoint,receive,create,mount ${BR_ROOT}
}
create_snapshot()
{
id="`date -I`-`date +%s`"
zfs snapshot -r ${BR_ROOT}@${id}
}
send_single_snapshot()
{
BR_NEW_SNAPSHOT=`get_latest_snapshot`
while read BR_REMOTE_ADDR BR_REMOTE_PORT BR_REMOTE_ROOT BR_REMOTE_USER; do
zfs send -R ${BR_ROOT}@${BR_NEW_SNAPSHOT} |
ssh -i ~/.ssh/id_rsa -o port=${BR_REMOTE_PORT} \
${BR_REMOTE_USER}@${BR_REMOTE_ADDR} \
zfs receive -dvu ${BR_REMOTE_ROOT}@${BR_NEW_SNAPSHOT}
done
echo $BR_NEW_SNAPSHOT > "old-snapshot.txt"
}
send_incremental_snapshot()
{
BR_NEW_SNAPSHOT=`get_latest_snapshot`
while read BR_REMOTE_ADDR BR_REMOTE_PORT BR_REMOTE_ROOT BR_REMOTE_USER; do
zfs send -R -i `cat old-snapshot.txt` ${BR_ROOT}@${BR_NEW_SNAPSHOT} |
ssh -i ~/.ssh/id_rsa -o port=${BR_REMOTE_PORT} \
${BR_REMOTE_USER}@${BR_REMOTE_ADDR} \
zfs receive -dvu ${BR_REMOTE_ROOT}@${BR_NEW_SNAPSHOT}
done
echo $BR_NEW_SNAPSHOT > "old-snapshot.txt"
}
create_snapshot
if [ -f "old-snapshot.txt" ]; then
send_incremental_snapshot
else
send_single_snapshot
fi