Add some helper scripts

This commit is contained in:
Wojciech Kozlowski 2019-07-29 00:17:02 +01:00
parent c141ec1eb7
commit fa7af91461
2 changed files with 106 additions and 0 deletions

76
backup.sh Executable file
View File

@ -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

30
registry-cleaner.sh Executable file
View File

@ -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"