zfs-backup/backup.sh

139 lines
3.4 KiB
Bash
Raw Normal View History

2022-11-19 08:34:22 +00:00
#!/usr/bin/env sh
2022-11-19 08:49:50 +00:00
### nodes.txt
2022-11-20 08:33:44 +00:00
# <local hostname> <local ssh port> <this host's root zfs> <this host's user>
# <remote host 1 address> <host 1 ssh port> <host 1 root zfs>
# <remote host 2 address> <host 2 ssh port> <host 2 root zfs>
2022-11-19 08:36:40 +00:00
# ...
2022-11-21 01:15:43 +00:00
### datasets.txt
2022-11-19 08:36:40 +00:00
# <dataset 1>
# <dataset 1>/<dataset 1a>
# <dataset 1>/<dataset 1b>
# <dataset 2>
# <dataset 2>/<dataset 2a>
# <dataset 3>
# ...
2022-11-20 08:33:44 +00:00
list_datasets()
2022-11-19 08:34:22 +00:00
{
2022-11-19 08:49:50 +00:00
zfs list | tail -n +2 | cut -d' ' -f1|grep ${BR_ROOT}
2022-11-19 08:34:22 +00:00
}
get_latest_snapshot()
{
zfs list -t snapshot |
2022-11-19 08:49:50 +00:00
grep ${BR_ROOT} |
2022-11-19 08:34:22 +00:00
tail -n1 |
cut -d'@' -f2 |
cut -d' ' -f 1
}
setup_aggregator()
{
2022-11-19 08:49:50 +00:00
zfs allow -u ${BR_USER} send,snapshot,hold ${BR_ROOT}
2022-11-19 08:34:22 +00:00
for dataset in `cat datasets.txt`; do
2022-11-21 01:15:43 +00:00
zfs create -p ${BR_ROOT}/$dataset
2022-11-19 08:34:22 +00:00
done
}
setup_mirror()
{
2022-11-21 01:15:43 +00:00
zfs create -p $BR_ROOT
for dataset in `cat datasets.txt`; do
zfs create -p ${BR_ROOT}/$dataset
done
2022-11-19 08:49:50 +00:00
zfs allow -u ${BR_USER} send,snapshot,hold ${BR_ROOT}
2022-11-20 08:33:44 +00:00
zfs allow -u ${BR_USER} \
2022-11-19 08:49:50 +00:00
compression,mountpoint,receive,create,mount ${BR_ROOT}
2022-11-19 08:34:22 +00:00
}
create_snapshot()
{
id="`date -I`-`date +%s`"
2022-11-21 01:06:47 +00:00
if [ $BR_TEST_MODE = "TRUE" ]; then
echo zfs snapshot -r ${BR_ROOT}@${id}
else
zfs snapshot -r ${BR_ROOT}@${id}
fi
2022-11-19 08:34:22 +00:00
}
send_single_snapshot()
{
2022-11-19 08:49:50 +00:00
BR_NEW_SNAPSHOT=`get_latest_snapshot`
2022-11-20 08:33:44 +00:00
tail -n +2 nodes.txt > sendnodes.tmp
2022-11-19 08:49:50 +00:00
while read BR_REMOTE_ADDR BR_REMOTE_PORT BR_REMOTE_ROOT BR_REMOTE_USER; do
2022-11-20 08:33:44 +00:00
case $BR_REMOTE_ADDR in
\#*) continue;;
esac
if [ $BR_TEST_MODE = "TRUE" ]; then
echo zfs send -R ${BR_ROOT}@${BR_NEW_SNAPSHOT}
echo ssh -i ~/.ssh/id_rsa -o port=${BR_REMOTE_PORT} \
${BR_REMOTE_USER}@${BR_REMOTE_ADDR} \
2022-11-21 01:52:51 +00:00
zfs receive -vu ${BR_REMOTE_ROOT}
2022-11-20 08:33:44 +00:00
else
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}
fi
done < sendnodes.tmp
rm sendnodes.tmp
if [ $BR_TEST_MODE = "TRUE" ]; then
:
else
echo $BR_NEW_SNAPSHOT > "old-snapshot.txt"
fi
2022-11-19 08:34:22 +00:00
}
2022-11-19 08:51:26 +00:00
send_incremental_snapshot()
2022-11-19 08:34:22 +00:00
{
2022-11-19 08:49:50 +00:00
BR_NEW_SNAPSHOT=`get_latest_snapshot`
2022-11-20 08:33:44 +00:00
echo Will send $BR_NEW_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
2022-11-19 08:49:50 +00:00
echo $BR_NEW_SNAPSHOT > "old-snapshot.txt"
2022-11-19 08:34:22 +00:00
}
2022-11-20 08:33:44 +00:00
################################### MAIN ####################################
### Options
set -- `getopt "tl:" "$@"` || {
echo "Usage: `basename $0` [-tl] [command]" 1>&2
exit 1
}
BR_TEST_MODE=FALSE
while :; do
case "$1" in
-t)
BR_TEST_MODE=TRUE
echo Test mode.
;;
--) shift; break ;;
esac
shift
done
read BR_HOST BR_PORT BR_ROOT BR_USER < nodes.txt
if [ ! -f nodes.txt ]; then echo "No nodes specified."; exit 2; fi
### Command
case "$1" in
list) list_datasets ;;
create) create_snapshot ;;
setup-mirror) setup_mirror ;;
send)
if [ -f "old-snapshot.txt" ]; then
send_incremental_snapshot
else
send_single_snapshot
fi
;;
esac