From 7ae60f9722fff7bcf8cf4c419b1a0a70cb0d643e Mon Sep 17 00:00:00 2001
From: Catherine Pitt <cen1001@cam.ac.uk>
Date: Tue, 29 Jun 2021 08:37:49 +0100
Subject: [PATCH] Add an outline script for moving a whole zpool

This came about because a disk has failed on nest-backup, which only has
subdirectory backups of nest-filestore-0 and so move-machine.sh was not
going to be helpful - it assumes all tasks for a machine are on the same
zpool which isn't true there. In this case I did the move by hand, but
have sketched out the steps in the script in the hope that next time we
have to do this we'll do it by looking at the script and running bits by
hand, then improve the script a bit, and continue until it's usable.
---
 .../lib/chem-zfs-backup-server/move-zpool.sh  | 66 +++++++++++++++++++
 1 file changed, 66 insertions(+)
 create mode 100755 ROOT/usr/lib/chem-zfs-backup-server/move-zpool.sh

diff --git a/ROOT/usr/lib/chem-zfs-backup-server/move-zpool.sh b/ROOT/usr/lib/chem-zfs-backup-server/move-zpool.sh
new file mode 100755
index 0000000..d761300
--- /dev/null
+++ b/ROOT/usr/lib/chem-zfs-backup-server/move-zpool.sh
@@ -0,0 +1,66 @@
+#!/bin/bash
+# To move the contents of a zpool, perhaps because the underlying disk is failing
+# This is incomplete and untested
+echo "This script is incomplete and should not be used yet"
+exit 1
+
+set -e
+set -x
+
+# Load some settings
+. /etc/default/zfs-backup
+
+function dosql() {
+ SQL="$1"
+ psql -t -c "$SQL"
+}
+
+# 
+if [ -z "$2" ] ; then
+ echo Usage: $0 zpool new-zpool
+ echo Moves the content of old-zpool to new-zpool
+ exit 1
+fi
+
+OZ=$1
+NZ=$2
+
+# need to try to unexport nfs here, but failure isn't a worry
+set +e
+EXPORTS=`dosql "select zfs_target from backup_task where backup_task_id in (select backup_task_id from backup_task where zfs_target ~'^$OZ')"`
+for EXPORT in $EXPORTS ; do
+  exportfs -u $FQDN:/$EXPORT
+done
+set -e
+
+echo moving $FQDN from $SOURCE to $TGT
+RUNNING=`dosql "select count(*) from backup_log natural join backup_task where backup_task.zfs_target ~ '^$OZ' and ended_processing is null and started_processing is not null"`
+RUNNING=`echo $RUNNING` # chomp
+#echo RUNNING $RUNNING
+if ! [ "$RUNNING" = 0 ] ;  then
+ echo $OZ has a job running
+ exit 3
+fi
+
+# De-queue jobs for this zpool
+dosql "delete from backup_log where backup_task_id in (select backup_task_id from backup_task where backup_task.zfs_target ~ '^$OZ') and ended_processing is null and started_processing is null;"
+
+# Update tasks
+dosql "update backup_task set zfs_target=regexp_replace(zfs_target,'^$OZ','$NZ') where backup_task.zfs_target ~ '^$OZ')"
+
+
+# Move ZFSes. Have to do this one at a time as some intermediates may already exist
+TIME=`date +%s`
+zfs snapshot -r $OZ@$TIME
+zfs hold zfs-moving $OZ@$TIME
+
+for ZC in $(zfs list -H -r -oname $OZ|grep -v reserved); do
+# check if this ZFS exists on NZ
+# if it doesn't, move it. Need to figure out the form of the target - sub NZ for OZ and strip off last component?
+  SIZE=`zfs list -H -o used $ZC`
+  echo Sending ZFS $ZC [ $SIZE]
+  zfs send -$ $ZC@${TIME} | mbuffer | zfs receive -e $NZ
+done
+
+# Tidy up
+#zpool destroy $OZ
-- 
GitLab