mirror of
https://asciireactor.com/o4data/zfs-backup.git
synced 2024-11-22 03:25:05 +00:00
162 lines
4.2 KiB
Bash
Executable File
162 lines
4.2 KiB
Bash
Executable File
#!/usr/bin/env sh
|
|
|
|
nodes_file="nodes.txt"
|
|
### nodes.txt
|
|
# <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>
|
|
# ...
|
|
|
|
datasets_file="datasets.txt"
|
|
### datasets.txt
|
|
# <dataset 1>
|
|
# <dataset 1>/<dataset 1a>
|
|
# <dataset 1>/<dataset 1b>
|
|
# <dataset 2>/<dataset 2a>
|
|
# <dataset 3>
|
|
# ...
|
|
|
|
list_active_datasets()
|
|
{
|
|
zfs list | tail -n +2 | cut -d' ' -f1|grep ${BG_ROOT}
|
|
}
|
|
|
|
read_datasets() {
|
|
while read BG_REMOTE_ADDR BG_REMOTE_PORT BG_REMOTE_ROOT BG_REMOTE_USER; do
|
|
case $BG_REMOTE_ADDR in
|
|
\#*) continue;;
|
|
esac
|
|
if [ $BG_TEST_MODE = "TRUE" ]; then
|
|
echo zfs send -R ${BG_ROOT}@${BG_NEW_SNAPSHOT}
|
|
echo ssh -i ~/.ssh/id_rsa -o port=${BG_REMOTE_PORT} \
|
|
${BG_REMOTE_USER}@${BG_REMOTE_ADDR} \
|
|
zfs receive -vu ${BG_REMOTE_ROOT}
|
|
else
|
|
zfs send -R ${BG_ROOT}@${BG_NEW_SNAPSHOT} |
|
|
ssh -i ~/.ssh/id_rsa -o port=${BG_REMOTE_PORT} \
|
|
${BG_REMOTE_USER}@${BG_REMOTE_ADDR} \
|
|
zfs receive -dvu ${BG_REMOTE_ROOT}
|
|
fi
|
|
done < ${datasets_file}
|
|
}
|
|
|
|
# compare_datasets()
|
|
|
|
create_datasets() {
|
|
# should compare_datasets and only create missing or possibly error.
|
|
for dataset in `cat ${datasets_file}`; do
|
|
zfs create -p ${BG_ROOT}/$dataset
|
|
done
|
|
}
|
|
|
|
get_latest_snapshot()
|
|
{
|
|
zfs list -t snapshot |
|
|
grep ${BG_ROOT} |
|
|
tail -n1 |
|
|
cut -d'@' -f2 |
|
|
cut -d' ' -f 1
|
|
}
|
|
|
|
setup_aggregator()
|
|
{
|
|
create_datasets
|
|
zfs allow -u ${BG_USER} send,snapshot,hold ${BG_ROOT}
|
|
}
|
|
|
|
setup_mirror()
|
|
{
|
|
zfs allow -u ${BG_USER} send,snapshot,hold ${BG_ROOT}
|
|
zfs allow -u ${BG_USER} \
|
|
compression,mountpoint,receive,create,mount ${BG_ROOT}
|
|
}
|
|
|
|
create_snapshot()
|
|
{
|
|
id="`date -I`-`date +%s`"
|
|
if [ $BG_TEST_MODE = "TRUE" ]; then
|
|
echo zfs snapshot -r ${BG_ROOT}@${id}
|
|
else
|
|
zfs snapshot -r ${BG_ROOT}@${id}
|
|
fi
|
|
}
|
|
|
|
send_single_snapshot()
|
|
{
|
|
BG_NEW_SNAPSHOT=`get_latest_snapshot`
|
|
tail -n +2 ${nodes_file} > sendnodes.tmp
|
|
while read BG_REMOTE_ADDR BG_REMOTE_PORT BG_REMOTE_ROOT BG_REMOTE_USER; do
|
|
case $BG_REMOTE_ADDR in
|
|
\#*) continue;;
|
|
esac
|
|
if [ $BG_TEST_MODE = "TRUE" ]; then
|
|
echo zfs send -R ${BG_ROOT}@${BG_NEW_SNAPSHOT}
|
|
echo ssh -i ~/.ssh/id_rsa -o port=${BG_REMOTE_PORT} \
|
|
${BG_REMOTE_USER}@${BG_REMOTE_ADDR} \
|
|
zfs receive -dvu ${BG_REMOTE_ROOT}
|
|
else
|
|
zfs send -R ${BG_ROOT}@${BG_NEW_SNAPSHOT} |
|
|
ssh -i ~/.ssh/id_rsa -o port=${BG_REMOTE_PORT} \
|
|
${BG_REMOTE_USER}@${BG_REMOTE_ADDR} \
|
|
zfs receive -dvu ${BG_REMOTE_ROOT}
|
|
fi
|
|
done < sendnodes.tmp
|
|
rm sendnodes.tmp
|
|
if [ $BG_TEST_MODE = "TRUE" ]; then
|
|
:
|
|
else
|
|
echo $BG_NEW_SNAPSHOT > "old-snapshot.txt"
|
|
fi
|
|
}
|
|
|
|
send_incremental_snapshot()
|
|
{
|
|
BG_NEW_SNAPSHOT=`get_latest_snapshot`
|
|
echo Will send $BG_NEW_SNAPSHOT
|
|
#while read BG_REMOTE_ADDR BG_REMOTE_PORT BG_REMOTE_ROOT BG_REMOTE_USER; do
|
|
# zfs send -R -i `cat old-snapshot.txt` ${BG_ROOT}@${BG_NEW_SNAPSHOT} |
|
|
# ssh -i ~/.ssh/id_rsa -o port=${BG_REMOTE_PORT} \
|
|
# ${BG_REMOTE_USER}@${BG_REMOTE_ADDR} \
|
|
# zfs receive -dvu ${BG_REMOTE_ROOT}@${BG_NEW_SNAPSHOT}
|
|
#done
|
|
echo $BG_NEW_SNAPSHOT > "old-snapshot.txt"
|
|
}
|
|
|
|
|
|
################################### MAIN ####################################
|
|
|
|
### Options
|
|
set -- `getopt "tl:" "$@"` || {
|
|
echo "Usage: `basename $0` [-tl] [command]" 1>&2
|
|
exit 1
|
|
}
|
|
BG_TEST_MODE=FALSE
|
|
while :; do
|
|
case "$1" in
|
|
-t)
|
|
BG_TEST_MODE=TRUE
|
|
echo Test mode.
|
|
;;
|
|
--) shift; break ;;
|
|
esac
|
|
shift
|
|
done
|
|
|
|
read BG_HOST BG_PORT BG_ROOT BG_USER < ${nodes_file}
|
|
|
|
if [ ! -f ${nodes_file} ]; then echo "No nodes specified."; exit 2; fi
|
|
|
|
### Command
|
|
case "$1" in
|
|
list) list_active_datasets ;;
|
|
create) create_snapshot ;;
|
|
setup-mirror) setup_mirror ;;
|
|
send)
|
|
if [ -f "old-snapshot.txt" ]; then
|
|
send_incremental_snapshot
|
|
else
|
|
send_single_snapshot
|
|
fi
|
|
;;
|
|
esac
|