Skip to content

Instantly share code, notes, and snippets.

@Richard-Barrett
Created February 28, 2021 02:59
Show Gist options
  • Save Richard-Barrett/83a97fb7f9d215cbe9e92d111e861691 to your computer and use it in GitHub Desktop.
Save Richard-Barrett/83a97fb7f9d215cbe9e92d111e861691 to your computer and use it in GitHub Desktop.
auto-swarm-backup.sh
#!/bin/bash
# ===================================================
# Created by: Richard Barrett
# Date Created: 09/11/2020
# Purpose: Automatc Local or Remote Backup for Swarm
# Company: Mirantis
# ===================================================
# Official documentation
# ======================================================================================================================
# https://docs.docker.com/engine/swarm/admin_guide/#back-up-the-swarm
# https://docs.docker.com/engine/reference/commandline/node_ls/
# https://www.tutorialspoint.com/unix/if-else-statement.htm
# https://unix.stackexchange.com/questions/232946/how-to-copy-all-files-from-a-directory-to-a-remote-directory-using-scp
# https://docs.docker.com/engine/swarm/swarm_manager_locking/
# https://docs.docker.com/engine/swarm/swarm_manager_locking/#unlock-a-swarm
# https://linux.die.net/man/1/zip
# ...
# ======================================================================================================================
#set -e
# keep track of the last executed command
trap 'last_command=$current_command; current_command=$BASH_COMMAND' DEBUG
# echo an error message before exiting
trap 'echo "\"${last_command}\" command filed with exit code $?."' EXIT
# System Variables
# ================
DATE="$(date +'%Y%-m%d')"
REPOSITORY_TAG=$(docker image ls --format '{{.Repository}}'| awk -F "/" '{print $1}'| sort -u | grep -v "calico")
MKE_CLUSTER_DIR="/tmp/mke_cluster/"
MKE_SUPPORT_DUMP_DIR="/tmp/mke_cluster/support_dump"
MKE_SWARM_DIR="/tmp/mke_cluster/swarm_backup"
MKE_UCP_VERSION=$((docker container inspect ucp-proxy --format '{{index .Config.Labels "com.docker.ucp.version"}}' 2>/dev/null || echo -n 3.2.6)|tr -d [[:space:]])
REQUEST_URL="https://127.0.0.1"
USERNAME=$ADMIN
PASSWORD=$PASSWORD
AUTHTOKEN=$(curl -sk -d '{"username":"$USERNAME","password":"$PASSWORD"}' ${REQUEST_URL}/auth/login | jq -r .auth_token)
TODAY=$(date +"%Y-%m-%d")
ENGINE=$(docker version -f '{{.Server.Version}}')
MANAGERS=$(docker node ls -f "role=manager")
MANAGERS_STATUS=$(docker node ls -f "role=manager.Status")
SWARM_MANAGERS_REACHABLE=$(docker node inspect $MANAGERS_STATUS --format "{{ .ManagerStatus.Reachability }}"
SWARM_MANAGERS_READY=$(docker node inspect $MANAGERS --format "{{ .Status.State }}"
if [[ $SWARM_MANAGERS_REACHABLE == "reachable"; $SWARM_MANAGERS_READY == "ready" ]];
then
systemctl stop docker
tar -czf /tmp/dockerbackup/$today-$HOSTNAME-$ENGINE-swarm.tgz /var/lib/docker/swarm
systemctl start docker
elif [[ $SWARM_MANAGERS_REACHABLE != "reachable"; $SWARM_MANAGERS_READY != "ready" ]]
then
echo "SWARM is not in Quorum"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment