With the new scheduled snapshot feature I notice I still have to run a custom cron script to copy/replicate lxd containers to a backup server
I’m currently running a lxc copy --refresh container backup-server:container every so often.
I realized how handy it would be to either enable a native ‘replication’ feature that does this automatically or the ability to run custom scripts after snapshot actions.
I have 3 lxd nodes where two run containers and one functions as a backup.
What i do is create snapshots on running containers and copy the snapshot’s to the backup without renaming.
This way i can spinn up containers on the backup if there is a problem on the running host’s.
To streamline this prosess i made a scrip to run on the backup host that i can run manually or as a cron job that you can modify to your use i think.
#!/bin/sh
#variable
ct1=$(lxc list | grep STOPPED | awk '{print $2}')
ct2=$(lxc list lxchost2: | grep RUNNING | awk '{print $2}')
ct3=$(lxc list lxchost3: | grep RUNNING | awk '{print $2}')
# Delete local STOPPED containers
for lxc1 in $(echo "$ct1");
do lxc delete $lxc1;
done
# lxchost2 - Delete, create and copy snapshot
for lxc2 in $(echo "$ct2");
do lxc delete lxchost2:$lxc2/$lxc2 &&
lxc snapshot lxchost2:$lxc2 $lxc2 &&
lxc copy lxchost2:$lxc2/$lxc2 $lxc2;
done
#lxchost3 - Delete, create and copy snapshot
for lxc3 in $(echo "$ct3");
do lxc delete lxchost3:$lxc3/$lxc3 &&
lxc snapshot lxchost3:$lxc3 $lxc3 &&
lxc copy lxchost3:$lxc3/$lxc3 $lxc3;
done