Created
February 17, 2020 00:02
-
-
Save pldmgg/78523929805a507aa47ac3d6c3c37afb to your computer and use it in GitHub Desktop.
restore_docker_containers.sh
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Reference: https://github.com/tlaanemaa/backup-docker | |
# Reference for starting addons via comd line: https://www.reddit.com/r/homeassistant/comments/an8lsl/how_do_you_issue_hassio_cli_commands_in_a/ | |
# Installation of Prerequisites - | |
# | |
# sudo apt-get install npm | |
# sudo npm install -g backup-docker | |
# sudo apt-get install powershell | |
# cd ~ | |
# wget https://dl.google.com/go/go1.13.8.linux-amd64.tar.gz | |
# sudo tar -C /usr/local -xzf go1.13.8.linux-amd64.tar.gz | |
# export PATH=$PATH:/usr/local/go/bin | |
# go get -d github.com/home-assistant/cli | |
# cd go/src/github.com/home-assistant/cli/ | |
# go install | |
# cd ~ | |
if (( $EUID != 0 )); then | |
echo "Please run as root! Halting!" | |
exit 1 | |
fi | |
if ! [ -x "$(command -v backup-docker)" ]; then | |
echo 'Error: backup-docker npm package is not installed! Halting!' >&2 | |
exit 1 | |
fi | |
if ! [ -x "$(command -v pwsh)" ]; then | |
echo 'Error: pwsh is not installed! Halting!' >&2 | |
exit 1 | |
fi | |
if ! [ -f "$HOME/go/bin/cli" ]; then | |
echo 'Error: The home assistant cli (https://github.com/home-assistant/cli) is not installed! Halting!' >&2 | |
exit 1 | |
fi | |
#originalbackupdir='/mnt/WDElements575/ldfam_share/docker_backups' | |
originalbackupdir=$1 | |
if [ ! -d $originalbackupdir ]; then | |
echo 'Error: The original backup directory does not exist! Halting!' >&2 | |
exit 1 | |
fi | |
# Check to make sure we have an appropriate backup directory under $originalbackupdir | |
backupsubdircheckcontainers=$(pwsh -Command "Get-ChildItem -Path '${originalbackupdir}/*/containers' -Directory") | |
backupsubdircheckvolumes=$(pwsh -Command "Get-ChildItem -Path '${originalbackupdir}/*/containers' -Directory") | |
if [ -z "$backupsubdircheckcontainers" ]; then | |
echo "Error: Unable to find an appropriate backup directory within '${originalbackupdir}'! Halting!" >&2 | |
exit 1 | |
fi | |
if [ -z "$backupsubdircheckvolumes" ]; then | |
echo "Error: Unable to find an appropriate backup directory within '${originalbackupdir}'! Halting!" >&2 | |
exit 1 | |
fi | |
backupdir=$(pwsh -Command "(Get-ChildItem -Path '${originalbackupdir}' -Directory | Sort-Object -Property Created)[-1].FullName") | |
if [ ! -d $backupdir ]; then | |
echo 'Error: The backup directory does not exist! Halting!' >&2 | |
exit 1 | |
fi | |
backupcontainersdir="${backupdir}/containers" | |
backupvolumesdir="${backupdir}/volumes" | |
backuptarfile="${backupvolumesdir}/usr_share_hassio.tar" | |
backupdockercmdstringpath="${backupvolumesdir}/ha_docker_cmd_string.txt" | |
backupdockerhaaddonspath="${backupvolumesdir}/ha_docker_addon_string_arrray.txt" | |
# Get the docker command used to start the homeassistant container | |
dockercmdstring=$(pwsh -Command "Get-Content ${backupdockercmdstringpath}") | |
if [ -z "$dockercmdstring" ]; then | |
echo 'Error: Unable to determine dockercmdstring used to create the homeassistant container! Halting!' >&2 | |
exit 1 | |
fi | |
addonslugsarray=($(pwsh -Command "Get-Content ${backupdockerhaaddonspath}")) | |
if [ -z "$addonslugsarray" ]; then | |
echo 'Error: Unable to determine the names of any home assistant addon containers! Halting!' >&2 | |
exit 1 | |
fi | |
HASSIO_TOKEN=$(echo $dockercmdstring | egrep -o "SUPERVISOR_TOKEN=\w+") | |
HASSIO_TOKEN="${HASSIO_TOKEN//SUPERVISOR_TOKEN=/}" | |
# First we need to wipe out all existing docker containers and volumes | |
# Stop the hassio systemctl services | |
sudo systemctl stop hassio-supervisor.service | |
sudo systemctl stop hassio-apparmor.service | |
# Remove all containers | |
docker stop $(docker ps -a -q) && docker rm $(docker ps -a -q) | |
# Remove all volumes | |
docker volume prune -f | |
sudo rm -rf /usr/share/hassio/* | |
# Next unzip /mnt/WDElements575/ldfam_share/docker_backups/volumes/usr_share_hassio.tar to /usr/share/hassio | |
sudo tar xvf $backuptarfile -C /usr/share/hassio | |
# Next, use backup-docker to restore all of the containers | |
backup-docker restore -d $backupdir | |
# Stop the hassio systemctl services | |
sudo systemctl start hassio-supervisor.service | |
sudo systemctl start hassio-apparmor.service | |
sleep 60 | |
# Make sure we can resolve hassio_supervisor | |
hassiosupervisorip=$(docker inspect hassio_supervisor | grep "IPv4Address" | awk '{print $2}' | sed 's/"//g') | |
# Add the hassio_supervisor container IP Address to /etc/hosts if it isn't in there already | |
linetoadd="${hassiosupervisorip} hassio_supervisor" | |
grep -qxF "${linetoadd}" /etc/hosts || sudo bash -c "echo '${linetoadd}' >> /etc/hosts" | |
# Redeploy the homeassistant container | |
$dockercmdstring | |
sleep 120 | |
## Startup all of the addon containers | |
for i in "${addonslugsarray[@]}"; do slug="${i//addon_/}"; ~/go/bin/cli addons start $slug --endpoint "http://hassio_supervisor/" --api-token $HASSIO_TOKEN; done | |
docker ps -a |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment