`lxd recover` as a non-interactive command for specific container recoveries

Hi @tomp,

yes, lxd import with the properties of my first post is required for our restore process.
To clarify the need I show the script lxc-restore.sh to you:

#!/bin/bash

# restore one or multiple containers from remote backup snapshot;
# local restore target names may differ from remote backup container names
# 
# Florian Sager, sager@agitos.de, 2020-11-19

set -e
set -x

LXDBINPATH=/snap/bin
LXCBIN=$LXDBINPATH/lxc
LXDBIN=$LXDBINPATH/lxd

ZFSBIN=/sbin/zfs
ZFSROOT=tank/containers/

FSROOT=/var/snap/lxd/common/lxd/storage-pools/

UMOUNTBIN=/bin/umount

NSENTERCMD="/usr/bin/nsenter --mount=/run/snapd/ns/lxd.mnt"

RESTOREPREFIXCMD="/usr/bin/rsync -azHAX -e'ssh -p 2222 -o StrictHostKeyChecking=no' --numeric-ids "

if [[ -z "$1" || "$1" == "-h" || "$1" == "--help" ]]; then
    echo 'usage: lxc-restore.sh userXXX@userXXX.trustedspace.de:snapshot/20191006-040921-170/ foo-backup:foo bar-backup:bar ...'
	exit
fi 

RESTOREPREFIXEXTCMD="$RESTOREPREFIXCMD $1"

i=0
for containersrcdst in "$@"
do
	if [ $i == 0 ]; then
		# skip first argument
	    let "i+=1"
	    continue
	fi

	let "i+=1"
	
	IFS=":" read -a containersrcdstarr <<< $containersrcdst

	CONTAINERBACKUPNAME=${containersrcdstarr[0]}
	CONTAINERNAME=${containersrcdstarr[1]}
		
	if [[ -z "$CONTAINERBACKUPNAME" || -z "$CONTAINERNAME" ]]; then
		echo '$containersrcdst is not of format containerbackupname:targetcontainername'
		exit 1
	fi

	# /sbin/zfs create tank/containers/foo
	$ZFSBIN create $ZFSROOT$CONTAINERNAME
	# /sbin/zfs set mountpoint=/var/snap/lxd/common/lxd/storage-pools/tank/containers/foo tank/containers/foo
	$ZFSBIN set mountpoint=$FSROOT$ZFSROOT$CONTAINERNAME $ZFSROOT$CONTAINERNAME

	# restore container from backup
	# alternative: cp -Rp /tmp/footest/foo-backup/* /var/snap/lxd/common/lxd/storage-pools/tank/containers/foo/	
	# /usr/bin/rsync -azHAX -e 'ssh -oStrictHostKeyChecking=no' --numeric-ids userXXX@userXXX.trustedspace.de:snapshot/20191006-040921-170/foo-backup/ /var/snap/lxd/common/lxd/storage-pools/tank/containers/foo/
	eval $RESTOREPREFIXEXTCMD$CONTAINERBACKUPNAME/ $FSROOT$ZFSROOT$CONTAINERNAME
	if [ $? != 0 ]; then
		echo "rsync failed with $?"
		exit 2
	fi

	# /bin/umount /var/snap/lxd/common/lxd/storage-pools/tank/containers/foo
	$UMOUNTBIN $FSROOT$ZFSROOT$CONTAINERNAME

	# /sbin/zfs set canmount=noauto tank/containers/foo
	$ZFSBIN set canmount=noauto $ZFSROOT$CONTAINERNAME
	
	# /usr/bin/nsenter --mount=/run/snapd/ns/lxd.mnt mount -t zfs tank/containers/foo /var/snap/lxd/common/lxd/storage-pools/tank/containers/foo
	$NSENTERCMD mount -t zfs $ZFSROOT$CONTAINERNAME $FSROOT$ZFSROOT$CONTAINERNAME

	# lxd import foo --force
	$LXDBIN import $CONTAINERNAME --force

	# lxc start foo
	$LXCBIN start $CONTAINERNAME
	
	# lxc ls agiprxtest
	$LXCBIN ls $CONTAINERNAME

done
2 Likes