Skip to content

Instantly share code, notes, and snippets.

@kreativmonkey
Last active August 2, 2016 20:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kreativmonkey/f3abb8c372b27ed1cc5db4eb02450017 to your computer and use it in GitHub Desktop.
Save kreativmonkey/f3abb8c372b27ed1cc5db4eb02450017 to your computer and use it in GitHub Desktop.
Update LXC-Container with automatical Snapshot

Usage:

./lxcupdate.sh container1 container2 container3

#!/bin/bash
###
# Update LXC Container with automatic Snapshot
# Author: Sebastian Preisner
# Version: 0.1
# Date: 2016-07-24
###
DATE=$(date +%Y%m%dT%H%M)
function update() {
# Check if Container is Running or not
if [[ `lxc-info -n ${1} -s` == *"RUNNING"* ]]; then
RUNNING=true
echo "=== Stop running container ==="
lxc-stop -n ${1} || { echo "lxc-stop failed"; exit 1; }
else
RUNNING=false
fi
# Create the Snapshot
echo "=== Container Snapshot ==="
echo "Automatical Backup ${Date}" > /tmp/snap-comm
lxc-snapshot -n ${1} -c /tmp/snap-comm || { echo "lxc-snapshot failed"; rm /tmp/snap-comm; exit 1; }
rm /tmp/snap-comm
# Start Container for the Updateprocess
echo "=== Container Start/Update ==="
lxc-start -n $1 -d || { echo "lxc-start failed"; exit 1; }
lxc-wait -n $1 -s 'RUNNING'
sleep 5
## For ArchLinux
lxc-attach -n $1 -- pacman -Syy
lxc-attach -n $1 -- pacman -Suw --noconfirm
#lxc-attach -n $1 -- pacman -Su --noconfirm
## For Debian based systems
#lxc-attach -n $1 -- apt-get -qq update for Debian based systems
#lxc-attach -n $1 -- apt-get -qq -y upgrade
#lxc-attach -n $1 -- apt-get -qq -y clean
#lxc-attach -n $1 -- apt-get -qq -y autoclean
#lxc-attach -n $1 -- apt-get -qq autoremove
# Stop Container if not running on start
if [[ ${RUNNING} == false ]]; then
echo "=== Stop container ==="
lxc-stop -n $1
fi
echo "=== Update complete ==="
}
# Function to check if container exist
function containerExist() {
local e
for e in `lxc-ls`; do
if [[ "$e" == "$1" ]]; then
return 0
fi
done
return 1
}
# Loop throw the Commant
while [ $# -gt 0 ]; do
if containerExist ${1}; then
echo "=== Update ${1} ==="
update ${1}
else
echo "=== Container dosn't exist ==="
echo "Container: ${1}"
fi
shift
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment