#!/bin/bash
#
# This script is used to backup and restore an LXC container, including the .conf file
# The resulting tar.gz file can then be transfered to another host
#
# 
#---------------------------------------------------------------------------------------
# 
LXCBASE=/var/lib/lxc

if [ "$(id -u)" != "0" ]; then
  echo "Please run this script as root"
  exit 1
fi
	
function Usage {
  echo "Usage: lxc-archive -n <name> -d <directory> [-b|-r|-l]"
  echo ""
  echo "-d : directory where the archive is located"
  echo "-b : backup"
  echo "-r : restore"
  echo "-l : list containers"
  echo ""
  echo "Archive file is of the form lxc-archive-<CONTAINER>.tar.gz"
}

function LXC-list {
  echo "Available containers for archive"
  lxc-ls --fancy
}

function LXC-exists {
#Verify container existence
  if ! [ -d "$LXCBASE/$CONTAINER_NAME" ] ; then
    return 404
  fi  
  if ! [ -e "$LXCBASE/$CONTAINER_NAME/config" ] ; then
    return 404
  fi
  return 0
}

#
# Backup LXC Container
#
function LXC-backup {
  echo "Backup LXC container $CONTAINER_NAME to $ARCHIVEFILE"
# Check container
  LXC-exists
  RESULT=$?
  if [ "$RESULT" != "0" ] ; then
    echo "Unable to access container $CONTAINER_NAME directory or configuration. Aborting..."
    exit 1
  fi
  if ! [ -w $DIRECTORY ] ; then
    echo "Unable to create archive file in directory $DIRECTORY"
  fi
# Check that container is stopped
  STATUS=$(lxc-info -s --name $CONTAINER_NAME | uniq | awk '{print $2}')
  if [ "$STATUS" != "STOPPED" ] ; then
    echo "Container $CONTAINER_NAME is not stopped. Aborting..."
    exit 1
  fi
  echo "Start archiving container $CONTAINER_NAME"
  cd $LXCBASE
  tar zcf $ARCHIVEFILE $CONTAINER_NAME
  RESULT=$?
  if [ "$RESULT" != "0" ] ; then
    echo "Unable to create archive file $ARCHIVEFILE. Aborting..."
    exit 1
  fi
  echo "Archive file $ARCHIVEFILE successfully created : "
  ls -l $ARCHIVEFILE
}


function LXC-restore {
  echo "Restore LXC container $CONTAINER_NAME from $ARCHIVEFILE"
# Check archive file

# Check container existence
  LXC-exists
  RESULT=$?
  if [ "$RESULT" = "0" ] ; then
    echo "Container $CONTAINER_NAME already exists. Aborting..."
    exit 1
  fi
# Everything seems OK, restore container
  echo "Start restoring container $CONTAINER_NAME"
  cd $LXCBASE
  tar zxf $ARCHIVEFILE
  RESULT=$?
  if [ "$RESULT" != "0" ] ; then
    echo "Unable to restore from archive file $ARCHIVEFILE. Aborting..."
    exit 1
  fi
  echo "Archive file $ARCHIVEFILE successfully restored : "
  lxc-info --name $CONTAINER_NAME
# 
  if [ -f $LXCBASE/$CONTAINER_NAME/rootfs.hold ] ; then
    rm  $LXCBASE/$CONTAINER_NAME/rootfs.hold 
  fi
}  
#
# --- Main --- 
#
ARCHIVEOPT="backup"

while getopts "n:d:brl" OPTION
do
    case "$OPTION" in
        n)
            CONTAINER_NAME="$OPTARG"
            ;;
        d)
            DIRECTORY="$OPTARG"
            ;;
        b)
            ARCHIVEOPT="backup"
            ;;
        r)
            ARCHIVEOPT="restore"
            ;;
        l)
            ARCHIVEOPT="list"
            ;;
    esac
done

if [ "$ARCHIVEOPT" = "list" ] ; then
  LXC-list
  exit
fi
  
#
# Check for mandatory arguments
#
if ! [ -n "$CONTAINER_NAME" ]
then
    echo "no container name specified"
    Usage
    exit 1
fi

if ! [ -n "$DIRECTORY" ]
then
    echo "no directory specified"
    Usage
    exit 1
fi

ARCHIVEFILE=${DIRECTORY}/lxc-archive.${CONTAINER_NAME}.tar.gz

case $ARCHIVEOPT in
  "list") LXC-list
  ;;
  "backup") LXC-backup
  ;;
  "restore") LXC-restore
  ;;
esac  

