0efd6a0b9e
added osxfer script for transfering a root filesystem based on subvolumes
108 lines
4.1 KiB
Bash
108 lines
4.1 KiB
Bash
#!/bin/bash
|
|
|
|
scripts_dir="$(dirname $(realpath "${BASH_SOURCE:-$0}"))"
|
|
|
|
clone_snaps () {
|
|
local snap; local bsnap
|
|
if ! sudo mkdir -p $TDIR/$TNAME &>/dev/null; then
|
|
echo ERROR unable to make directory $TDIR/$TNAME
|
|
return 1
|
|
fi
|
|
echo cloning transfered read only snapshots at $TSNAPS
|
|
ls -la $TSNAPS
|
|
echo to $TDIR/$TNAME
|
|
echo "################"
|
|
echo cdir: $CDIR, tname: $TNAME
|
|
latest=$(sudo btrbk -c "$CDIR/$TNAME.conf" list --format col:h:target_subvolume | xargs -I % sh -c 'basename %' | sed '$!N; /^\(.*\)\n\1$/!P; D')
|
|
echo $latest
|
|
if [[ $latest ]]; then
|
|
for snap in $latest; do
|
|
# dests+=" $(btrbk -c $file list target --format col:h:target_path)"
|
|
bsnap=$(basename "${snap%.*}")
|
|
echo cloning $TSNAPS/$snap to $TDIR/$TNAME/$bsnap
|
|
if [[ -d $TDIR/$TNAME/$bsnap ]]; then
|
|
echo snapshot $TDIR/$TNAME/$bsnap already exists you must manually remove it if you want to overwrite
|
|
echo "'sudo btrfs sub del $TDIR/$TNAME/$bsnap' or 'sudo btrfs sub del $TDIR/$TNAME/*'"
|
|
else
|
|
sudo btrfs sub snap $TSNAPS/$snap $TDIR/$TNAME/$bsnap
|
|
echo $TDIR/$TNAME/$bsnap
|
|
ls -la $TDIR/$TNAME/$bsnap
|
|
fi
|
|
done
|
|
echo $TDIR/$TNAME
|
|
ls -la $TDIR/$TNAME
|
|
else
|
|
echo no transfered snapshots, nothing to clone
|
|
fi
|
|
}
|
|
|
|
osxfer () {
|
|
|
|
local TDIR; local TSNAPS; local SDIR; local CDIR
|
|
|
|
efile=$1.env
|
|
if [[ -f $efile ]]; then
|
|
module_load confirm
|
|
echo "#### sourcing $efile ########"
|
|
source $efile
|
|
TDIR=${TDIR:-/snapshots/transfer/target}
|
|
TSNAPS=${TDIR:-/snapshots/transfer/target}/${TSNAPS:-tmp/tsnaps}
|
|
SDIR=${SDIR:-/snapshots/transfer/source}
|
|
CDIR=${CDIR:-/snapshots/conf}
|
|
echo tsnaps: $TSNAPS
|
|
# TODO make sure minimal environment is set
|
|
source <(
|
|
source $efile
|
|
bash $(module_find btpl) $scripts_dir/os.conf.tmpl
|
|
) >$CDIR/$TNAME.conf
|
|
# echo "######### btrbk file: $TNAME.conf ##############"
|
|
echo "$CDIR/$TNAME.conf"
|
|
# echo "#######################"
|
|
# if ! confirm "conf file is ok?"; then exit; fi
|
|
module_load btrbk btrfs
|
|
mkdir -p $TDIR &>/dev/null
|
|
mkdir -p $SDIR &>/dev/null
|
|
# echo $SDIR
|
|
# ls -la /snapshots/transfer
|
|
[[ $TLABEL ]] && sudo mount -L $TLABEL $TDIR
|
|
[[ $TUUID ]] && sudo mount --UUID=$TUUID $TDIR
|
|
[[ $TDEV ]] && sudo mount /dev/$TDEV $TDIR
|
|
if mountpoint $TDIR &>/dev/null; then
|
|
sudo mkdir -p $TSNAPS
|
|
echo able to mount $TLABEL $TUUID $TDEV at $TDIR... continuing transfer
|
|
if ls $TSNAPS/@root.* &>/dev/null; then
|
|
echo exists
|
|
if ! confirm a set of transfer snapshots exist at destination, resend?; then
|
|
clone_snaps
|
|
return
|
|
fi
|
|
fi
|
|
if btrbk_run $2 "$CDIR/$TNAME.conf"; then
|
|
echo successful transfer from $SMP/$SNAME to $TLABEL $TUUID $TDEV - $TSNAPS
|
|
sudo btrbk -c "$CDIR/$TNAME.conf" list latest
|
|
echo "########################"
|
|
clone_snaps
|
|
else
|
|
echo ERROR: snapshot transfer to $TLABEL $TUUID $TDEV - $TSNAPS failed
|
|
fi
|
|
echo deleting temporary transfer snapshots at $SDIR
|
|
sudo btrfs sub del $SDIR/*
|
|
sudo rm -rf $SDIR
|
|
# echo deleting temporary transfer snapshots at $TSNAPS
|
|
# sudo btrfs sub del $TSNAPS/*
|
|
# sudo rm -rf $TSNAPS
|
|
echo unmounting $TDIR
|
|
if sudo umount -R $TDIR; then
|
|
if ! confirm "remove local mount directory $TDIR?"; then exit; fi
|
|
sudo rm -f $TDIR
|
|
else
|
|
echo WARNING: not able to unmount $TDIR
|
|
fi
|
|
else
|
|
echo unable to mount $TLABEL $TUUID $TDEV at $TDIR
|
|
fi
|
|
else
|
|
echo environment file $efile does not exisit
|
|
fi
|
|
|
|
}
|