87 lines
3.6 KiB
Bash
Executable File
87 lines
3.6 KiB
Bash
Executable File
#!/bin/bash
|
|
scripts_dir="$(dirname $(realpath "${BASH_SOURCE:-$0}"))"
|
|
|
|
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}
|
|
# TODO make sure minimal environment is set
|
|
source <(
|
|
source $efile
|
|
bash $(module_find btpl) os.conf.tmpl
|
|
) >$CDIR/$TNAME.conf
|
|
echo "######### btrbk file: $TNAME.conf ##############"
|
|
cat "$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 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 "########################"
|
|
if sudo mkdir -p $TDIR/$TNAME &>/dev/null; then
|
|
echo cloning transfered read only snapshots at $TSNAPS
|
|
ls -la $TSNAPS
|
|
echo to $TDIR/$TNAME
|
|
sudo btrbk -c "$CDIR/$TNAME.conf" list latest
|
|
latest=$(sudo btrbk -c "$CDIR/$TNAME.conf" list latest --format col:h:snapshot_subvolume | xargs -I % sh -c 'basename %' | sed '$!N; /^\(.*\)\n\1$/!P; D')
|
|
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 to clone
|
|
fi
|
|
else
|
|
echo ERROR unable to make directory $TDIR/$TNAME
|
|
fi
|
|
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
|