LXC backup

Current This solution still works remarkably good.

Additionally to a duply-driven approach, we wanted to backup our LXC containers remotely in an archive.

The following approach was taken from stackoverflow and was tailored to our needs.

lxc-backup.sh (Source)

#!/bin/bash
TODAY=$(date --iso-8601)
BACKUP=backup-`date +%s`.tar.gz
LXC_DIR=/var/lib/lxc
REMOTE_HOST=HOSTNAME
REMOTE_DIR=/FOLDER/FOLDER/
for container_path in "$LXC_DIR"/*
do
  container_name="$(basename $container_path)"
  echo "clearing apt caches for $container_name"
  lxc-attach --name ${container_name} apt clean
  echo "stopping $container_name"
  lxc-stop -n ${container_name}
  cd $container_path
  echo "compressing $container_name"
  echo "${LXC_DIR}/${TODAY}-${container_name}.tar"
  tar --numeric-owner -czvf ${LXC_DIR}/${TODAY}-${container_name}.tar ./*
  echo "starting $container_name"
  lxc-start -n ${container_name} --daemon
done
echo "moving existing remote files to temporary remote folder"
ssh $REMOTE_HOST mkdir ${REMOTE_DIR}/old-${TODAY}
ssh $REMOTE_HOST mv ${REMOTE_DIR}/*.tar ${REMOTE_DIR}/old-${TODAY}
echo "copying backups to ${REMOTE_HOST}:${REMOTE_DIR}"
rsync -avzhe ssh --progress ${LXC_DIR}/*.tar ${REMOTE_HOST}:${REMOTE_DIR}
echo "removing temporary remote folder"
ssh $REMOTE_HOST rm -rf ${REMOTE_DIR}/old-${TODAY}
echo "removing local tars"
rm ${LXC_DIR}/*.tar