From fa7af91461d49a01a17b00510a4ca358cef5795e Mon Sep 17 00:00:00 2001 From: Wojciech Kozlowski Date: Mon, 29 Jul 2019 00:17:02 +0100 Subject: [PATCH] Add some helper scripts --- backup.sh | 76 +++++++++++++++++++++++++++++++++++++++++++++ registry-cleaner.sh | 30 ++++++++++++++++++ 2 files changed, 106 insertions(+) create mode 100755 backup.sh create mode 100755 registry-cleaner.sh diff --git a/backup.sh b/backup.sh new file mode 100755 index 0000000..b7d718f --- /dev/null +++ b/backup.sh @@ -0,0 +1,76 @@ +#!/bin/bash + +set -e + +CYAN='\033[01;36m' +YELLOW='\033[01;33m' +RED='\033[01;31m' +NC='\033[00m' + +SCRIPT=$(readlink -f $0) +DIRNAME=$(dirname $SCRIPT) + +# ----------------------------------------------------------------------------- +# Get the list of volumes. +# ----------------------------------------------------------------------------- + +echo -e "${CYAN}[${SCRIPT}] Extract list of volumes ${NC}" + +# Find the line where "services:" start +services_line=$(grep -n services docker-compose.yml | awk '{split($0, a, ":"); print a[1]}') + +# The volumes are listed between "volumes:" and "services:" +volume_list=$(head -n $services_line docker-compose.yml | awk '/volumes:/,/services:/') + +# Split into array +IFS=':'; volumes=($volume_list); unset IFS; + +# Trim whitespace +for ((i = 0; i < ${#volumes[@]}; i++)); do + volumes[$i]=$(echo -e "${volumes[$i]}" | tr -d '[:space:]') +done + +# Verify that the first entry is "volumes" and the last "services" +if [ ${volumes[0]} != "volumes" ] || [ "${volumes[-1]}" != "services" ]; then + echo -e "${RED}Unexpected input${NC}" >&2 + exit 1 +fi + +# Remove first and last entry - they will be "volumes" and " services" +let len=${#volumes[@]}-2 +volumes=("${volumes[@]:1:$len}") + +# Print final list +echo -e "${YELLOW}Volumes${NC}:" +for vol in "${volumes[@]}"; do + echo -e " - ${YELLOW}${vol} ${NC}" +done + +# ----------------------------------------------------------------------------- +# Stop the server. +# ----------------------------------------------------------------------------- + +echo -e "${CYAN}[${SCRIPT}] Stop loki-server ${NC}" + +service loki-server stop + +# ----------------------------------------------------------------------------- +# Back up volumes. +# ----------------------------------------------------------------------------- + +for vol in "${volumes[@]}"; do + echo -e "${CYAN}[${SCRIPT}] Back up ${YELLOW}${vol}${CYAN} volume ${NC}" + + set -o xtrace + docker run --rm -v loki_${vol}:/opt/${vol} -v /srv/backup:/opt/backup debian:stable-slim bash -c \ + "cd /opt/${vol} && tar cf /opt/backup/${vol}.tar ." + set +o xtrace +done + +# ----------------------------------------------------------------------------- +# Restart the server. +# ----------------------------------------------------------------------------- + +echo -e "${CYAN}[${SCRIPT}] Restart loki-server ${NC}" + +service loki-server start diff --git a/registry-cleaner.sh b/registry-cleaner.sh new file mode 100755 index 0000000..dc40dd2 --- /dev/null +++ b/registry-cleaner.sh @@ -0,0 +1,30 @@ +#!/bin/bash + +set -e + +CYAN='\033[01;36m' +YELLOW='\033[01;33m' +RED='\033[01;31m' +NC='\033[00m' + +SCRIPT=$(readlink -f $0) +DIRNAME=$(dirname $SCRIPT) + +# ----------------------------------------------------------------------------- +# Soft delete untagged images. +# ----------------------------------------------------------------------------- + +echo -e "${CYAN}[${SCRIPT}] Soft delete untagged images ${NC}" + +install="pip3 install gitlab-registry-cleanup" +cleanup="gitlab-registry-cleanup -g https://gitlab.wojciechkozlowski.eu -r https://registry.wojciechkozlowski.eu -u wojtek" + +docker run -it --rm --volumes-from gitlab python bash -c "${install} && ${cleanup}" + +# ----------------------------------------------------------------------------- +# Garbage collect and hard delete untagged images. +# ----------------------------------------------------------------------------- + +echo -e "${CYAN}[${SCRIPT}] Garbage collect untagged images ${NC}" + +docker exec gitlab bash -c "gitlab-ctl registry-garbage-collect"