mirror of
https://asciireactor.com/o4data/zfs-backup.git
synced 2024-11-21 21:45:05 +00:00
288 lines
7.6 KiB
Bash
Executable File
288 lines
7.6 KiB
Bash
Executable File
#!/usr/bin/env sh
|
|
|
|
nodes_file="nodes.txt"
|
|
### nodes.txt
|
|
# <origin host> <user> <ssh port> <timezone> <compressed flag>
|
|
# <mirror 1 host> <user> <ssh port> <zfs root> <send speed>
|
|
# <mirror 2 host> <user> <ssh port> <zfs root> <send speed>
|
|
# ...
|
|
|
|
datasets_file="datasets.txt"
|
|
### datasets.txt
|
|
# <dataset 1> [remote dataset]
|
|
# <dataset 2> [remote dataset]
|
|
# <dataset 3> [remote dataset]
|
|
# ...
|
|
# recognized options: nosend nosnap(shot) remotezfs=<remote dataset name>
|
|
|
|
if [ ! -f $nodes_file ]; then echo "No nodes specified."; exit 2; fi
|
|
if [ ! -f $datasets_file ]; then echo "No datasets specified."; exit 3; fi
|
|
|
|
ZFS_SEND_OPTS="-pRLe"
|
|
ZFS_SNAPSHOT_OPTS="-r"
|
|
ZFS_RECV_OPTS="-vsu"
|
|
SSH_SEND_OPTS="-Ti ~/.ssh/id_rsa"
|
|
DEF_SPEED="20M"
|
|
|
|
read LOCAL_HOST LOCAL_USER LOCAL_PORT LOCAL_TZ SEND_COMPRESSED < $nodes_file
|
|
|
|
ORIGIN_PERMS="send,snapshot,hold"
|
|
MIRROR_PERMS="compression,receive,create,mount,mountpoint,readonly,jailed"
|
|
|
|
SEND_DATASETS=""
|
|
SNAPSHOT_DATASETS=""
|
|
|
|
debug()
|
|
{
|
|
if [ "$ZB_DEBUG_MODE" = "TRUE" ]; then
|
|
echo $@
|
|
fi
|
|
}
|
|
|
|
run_send_recv()
|
|
{
|
|
if [ $ZB_TEST_MODE = "TRUE" ]; then
|
|
echo "$send_cmd | $pv_cmd | $recv_cmd"
|
|
else
|
|
$send_cmd | $pv_cmd | $recv_cmd
|
|
fi
|
|
}
|
|
|
|
set_compression() {
|
|
case $SEND_COMPRESSED in
|
|
[tTyY]*) ZFS_SEND_OPTS="$ZFS_SEND_OPTS -c"
|
|
debug "Enabled send as compressed datasets." ;;
|
|
esac
|
|
}
|
|
|
|
populate_datasets()
|
|
{
|
|
while read dataset dataset_options; do
|
|
case $dataset in
|
|
\#*) continue;;
|
|
esac
|
|
|
|
snapshot_dataset="yes"
|
|
send_dataset="yes"
|
|
for option in $dataset_options; do
|
|
case $option in
|
|
"nosnap"* )
|
|
snapshot_dataset=""
|
|
;;
|
|
"nosend"* )
|
|
send_dataset=""
|
|
;;
|
|
"remotezfs="*)
|
|
remotezfs=`echo $option|cut -d'=' -f2`
|
|
dataset="$dataset:$remotezfs"
|
|
;;
|
|
esac
|
|
done
|
|
|
|
if [ $send_dataset ]; then
|
|
SEND_DATASETS="$SEND_DATASETS $dataset"
|
|
debug "Enabled send for $dataset."
|
|
fi
|
|
if [ $snapshot_dataset ]; then
|
|
SNAPSHOT_DATASETS="$SNAPSHOT_DATASETS $dataset"
|
|
debug "Enabled snapshot for $dataset."
|
|
fi
|
|
done < datasets.txt
|
|
}
|
|
|
|
list_datasets()
|
|
{
|
|
for dataset in $SEND_DATASETS; do
|
|
dataset=`echo $dataset|cut -d: -f1`
|
|
zfs list -H $dataset
|
|
done
|
|
}
|
|
|
|
list_remote_datasets() {
|
|
while read addr user ssh_port zroot speed; do
|
|
echo "=== $user@$addr $zroot ==="
|
|
if [ $zroot ]; then
|
|
case $zroot in
|
|
*"/") ;;
|
|
*) zroot="$zroot/" ;;
|
|
esac
|
|
fi
|
|
run_cmd="
|
|
sh -c 'for dataset in $SEND_DATASETS; do
|
|
dataset=`echo \\$dataset|cut -d: -f2`
|
|
zfs list -H $zroot\$dataset;
|
|
done'"
|
|
if [ $ZB_TEST_MODE = "TRUE" ]; then
|
|
echo ssh $SSH_SEND_OPTS -o port=$ssh_port $user@$addr $run_cmd
|
|
else
|
|
ssh $SSH_SEND_OPTS -o port=$ssh_port $user@$addr $run_cmd
|
|
fi
|
|
done < sendnodes.tmp
|
|
}
|
|
|
|
get_latest_snapshot()
|
|
{
|
|
zfs list -t snapshot $dataset | tail -n1 | cut -d'@' -f2 | cut -d' ' -f 1
|
|
}
|
|
|
|
get_latest_remote_snapshot()
|
|
{
|
|
if [ $zroot ]; then
|
|
case $zroot in
|
|
*"/") ;;
|
|
*) zroot="$zroot/" ;;
|
|
esac
|
|
fi
|
|
ssh $SSH_SEND_OPTS -o port=$ssh_port $user@$addr \
|
|
"zfs list -t snapshot $zroot$dataset |
|
|
tail -n1|cut -d'@' -f2|cut -d' ' -f 1"
|
|
}
|
|
|
|
zfs_allow_origin()
|
|
{
|
|
for dataset in $SEND_DATASETS; do
|
|
dataset=`echo $dataset|cut -d: -f1`
|
|
if [ $ZB_TEST_MODE = "TRUE" ]; then
|
|
echo zfs allow -u $LOCAL_USER $ORIGIN_PERMS $dataset
|
|
else
|
|
zfs allow -u $LOCAL_USER $ORIGIN_PERMS $dataset
|
|
fi
|
|
done
|
|
}
|
|
|
|
zfs_allow_mirrors()
|
|
{
|
|
while read addr user ssh_port zroot speed; do
|
|
echo "=== $user@$addr $zroot ==="
|
|
if [ $zroot ]; then
|
|
case $zroot in
|
|
*"/") ;;
|
|
*) zroot="$zroot/" ;;
|
|
esac
|
|
fi
|
|
remote_cmd="
|
|
for dataset in $SEND_DATASETS; do
|
|
dataset=`echo \\$dataset|cut -d: -f2`
|
|
zfs allow -u $user $MIRROR_PERMS $zroot\$dataset
|
|
done
|
|
"
|
|
if [ $ZB_TEST_MODE = "TRUE" ]; then
|
|
echo ssh $SSH_SEND_OPTS -o port=$ssh_port $user@$addr $remote_cmd
|
|
else
|
|
ssh $SSH_SEND_OPTS -o port=$ssh_port $user@$addr $remote_cmd
|
|
fi
|
|
done < sendnodes.tmp
|
|
}
|
|
|
|
create_snapshots()
|
|
{
|
|
snapshot_id="`date -I`-`date +%s`-$LOCAL_TZ"
|
|
for dataset in $SNAPSHOT_DATASETS; do
|
|
if [ $ZB_TEST_MODE = "TRUE" ]; then
|
|
echo zfs snapshot $ZFS_SNAPSHOT_OPTS $dataset@$snapshot_id
|
|
else
|
|
zfs snapshot $ZFS_SNAPSHOT_OPTS $dataset@$snapshot_id
|
|
fi
|
|
done
|
|
}
|
|
|
|
send_latest()
|
|
{
|
|
while read addr user ssh_port zroot speed; do
|
|
echo "=== $user@$addr $zroot ==="
|
|
if [ $zroot ]; then
|
|
case $zroot in
|
|
*"/") ;;
|
|
*) zroot="$zroot/" ;;
|
|
esac
|
|
fi
|
|
for dataset in $SEND_DATASETS; do
|
|
remote_dataset=`echo $dataset|cut -d: -f2`
|
|
dataset=`echo $dataset|cut -d: -f1`
|
|
debug "Processing $dataset (remote $remote_dataset)."
|
|
origin_snapshot=`get_latest_snapshot $dataset`
|
|
send_cmd="zfs send $ZFS_SEND_OPTS $dataset@$origin_snapshot"
|
|
pv_cmd="pv -qL ${speed:-$DEF_SPEED}"
|
|
recv_cmd="
|
|
ssh $SSH_SEND_OPTS -o port=$ssh_port $user@$addr \
|
|
zfs recv $ZFS_RECV_OPTS $zroot$remote_dataset
|
|
"
|
|
run_send_recv
|
|
done
|
|
done < sendnodes.tmp
|
|
}
|
|
|
|
send_increment()
|
|
{
|
|
while read addr user ssh_port zroot speed; do
|
|
echo "=== $user@$addr $zroot ==="
|
|
if [ $zroot ]; then
|
|
case $zroot in
|
|
*"/") ;;
|
|
*) zroot="$zroot/" ;;
|
|
esac
|
|
fi
|
|
for dataset in $SEND_DATASETS; do
|
|
remote_dataset=`echo $dataset|cut -d: -f2`
|
|
dataset=`echo $dataset|cut -d: -f1`
|
|
origin_snapshot=`get_latest_snapshot`
|
|
remote_snapshot=`get_latest_remote_snapshot`
|
|
send_cmd="
|
|
zfs send $ZFS_SEND_OPTS -i $dataset@$remote_snapshot \
|
|
$dataset@$origin_snapshot
|
|
"
|
|
pv_cmd="pv -qL ${speed:-$DEF_SPEED}"
|
|
recv_cmd="
|
|
ssh $SSH_SEND_OPTS -o port=$ssh_port $user@$addr \
|
|
zfs recv $ZFS_RECV_OPTS $zroot$remote_dataset
|
|
"
|
|
run_send_recv
|
|
done
|
|
done < sendnodes.tmp
|
|
}
|
|
|
|
|
|
################################### MAIN ####################################
|
|
|
|
### Options
|
|
set -- `getopt "tdl:" "$@"` || {
|
|
echo "Usage: `basename $0` [-tdl] [command]" 1>&2
|
|
exit 1
|
|
}
|
|
ZB_TEST_MODE=FALSE
|
|
while :; do
|
|
case "$1" in
|
|
-t)
|
|
ZB_TEST_MODE=TRUE
|
|
echo Test mode.
|
|
;;
|
|
-d)
|
|
ZB_DEBUG_MODE=TRUE
|
|
ZFS_SEND_OPTS="$ZFS_SEND_OPTS -v"
|
|
# Current on by default.
|
|
# ZFS_RECV_OPTS="$ZFS_RECV_OPTS -v"
|
|
echo Debug mode.
|
|
;;
|
|
--) shift; break ;;
|
|
esac
|
|
shift
|
|
done
|
|
|
|
tail -n +2 $nodes_file|grep -v '^#' > sendnodes.tmp
|
|
|
|
set_compression
|
|
populate_datasets
|
|
|
|
### Command
|
|
case "$1" in
|
|
list) list_datasets ;;
|
|
list-remote) list_remote_datasets ;;
|
|
snapshot) create_snapshots ;;
|
|
configure-mirrors) zfs_allow_mirrors ;;
|
|
configure-origin) zfs_allow_origin ;;
|
|
mirror-reset) send_latest ;;
|
|
mirror-increment) send_increment ;;
|
|
#mirror-prune) send_increment_with_F ;;
|
|
esac
|
|
|
|
rm sendnodes.tmp |