This repository has been archived on 2023-02-05. You can view files and clone it, but cannot push or open issues or pull requests.
loki/backup.sh

189 lines
4.6 KiB
Bash
Raw Normal View History

2019-07-29 01:17:02 +02:00
#!/bin/bash
set -e
RED='\033[01;31m'
GREEN='\033[01;32m'
YELLOW='\033[01;33m'
CYAN='\033[01;36m'
2019-07-29 01:17:02 +02:00
NC='\033[00m'
2019-12-16 08:30:36 +01:00
if [ ! -t 1 ]; then
2019-12-14 23:15:20 +01:00
RED=''
GREEN=''
YELLOW=''
CYAN=''
NC=''
fi
2019-07-29 01:17:02 +02:00
SCRIPT=$(readlink -f $0)
DIRNAME=$(dirname $SCRIPT)
2019-12-14 16:51:01 +01:00
BACKUP_DIR="/media/usb0/backup"
2019-07-29 01:17:02 +02:00
# -----------------------------------------------------------------------------
# Get the list of volumes.
# -----------------------------------------------------------------------------
function get_volumes {
2019-07-29 01:17:02 +02:00
# Find the line where "services:" start
services_line=$(grep -n services docker-compose.yml | \
awk '{split($0, a, ":"); print a[1]}')
2019-07-29 01:17:02 +02:00
# The volumes are listed between "volumes:" and "services:"
volume_list=$(head -n $services_line docker-compose.yml | \
awk '/volumes:/,/services:/')
2019-07-29 01:17:02 +02:00
# Split into array
IFS=':'; volumes=($volume_list); unset IFS;
2019-07-29 01:17:02 +02:00
# Trim whitespace
for ((i = 0; i < ${#volumes[@]}; i++)); do
volumes[$i]=$(echo -e "${volumes[$i]}" | tr -d '[:space:]')
done
2019-07-29 01:17:02 +02:00
# 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
2019-07-29 01:17:02 +02:00
# Remove first and last entry - they will be "volumes" and " services"
let len=${#volumes[@]}-2
volumes=("${volumes[@]:1:$len}")
2019-07-29 01:17:02 +02:00
echo ${volumes[*]}
}
# -----------------------------------------------------------------------------
# Start the server.
# -----------------------------------------------------------------------------
function server_start {
echo -e "${CYAN}[${SCRIPT}] Restart loki-server ${NC}"
2019-11-03 00:26:36 +01:00
systemctl start loki-server
}
2019-07-29 01:17:02 +02:00
# -----------------------------------------------------------------------------
# Stop the server.
# -----------------------------------------------------------------------------
function server_stop {
2019-07-29 01:17:02 +02:00
echo -e "${CYAN}[${SCRIPT}] Stop loki-server ${NC}"
2019-11-03 00:26:36 +01:00
systemctl stop loki-server
}
2019-07-29 01:17:02 +02:00
# -----------------------------------------------------------------------------
# Back up volumes.
# -----------------------------------------------------------------------------
function backup {
2019-07-29 01:17:02 +02:00
volumes=$1
2019-12-14 16:51:01 +01:00
# Remove old backup directory
if [ -d ${BACKUP_DIR} ]; then
2019-12-14 16:51:01 +01:00
rm -f ${BACKUP_DIR}/*.tar
rmdir ${BACKUP_DIR}
fi
# Make sure directory exists
mkdir ${BACKUP_DIR}
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} \
2019-12-14 16:51:01 +01:00
-v ${BACKUP_DIR}:/opt/backup \
debian:stable-slim \
bash -c "cd /opt/${vol} && tar cf /opt/backup/${vol}.tar ."
set +o xtrace
done
}
2019-07-29 01:17:02 +02:00
# -----------------------------------------------------------------------------
# Restore volumes.
2019-07-29 01:17:02 +02:00
# -----------------------------------------------------------------------------
function restore {
volumes=$1
for vol in "${volumes[@]}"
do
echo -e "${CYAN}[${SCRIPT}] Restore ${YELLOW}${vol}${CYAN} volume${NC}"
set -o xtrace
docker run --rm \
-v loki_${vol}:/opt/${vol} \
2019-12-14 16:51:01 +01:00
-v ${BACKUP_DIR}:/opt/backup \
debian:stable-slim \
bash -c "cd /opt/${vol} && tar xf /opt/backup/${vol}.tar"
set +o xtrace
done
}
# -----------------------------------------------------------------------------
# Main.
# -----------------------------------------------------------------------------
while getopts "br" option
do
case ${option} in
b )
echo -e "${CYAN}[${SCRIPT}] Extract list of volumes ${NC}"
volumes=($(get_volumes))
echo -e "${YELLOW}Volumes${NC}:"
for vol in "${volumes[@]}"
do
echo -e " - ${YELLOW}${vol} ${NC}"
done
server_stop
backup ${volumes}
server_start
exit 0
;;
r )
echo -e "${CYAN}[${SCRIPT}] Extract list of volumes ${NC}"
volumes=($(get_volumes))
echo -e "${YELLOW}Volumes${NC}:"
for vol in "${volumes[@]}"
do
echo -e " - ${YELLOW}${vol} ${NC}"
done
server_stop
restore ${volumes}
server_start
exit 0
;;
\? )
echo -e "${GREEN} Usage: backup.sh [-b|-r]${NC}"
exit 1
;;
esac
done
2019-07-29 01:17:02 +02:00
# If we get here then no options were triggered
echo -e "${GREEN} Usage: backup.sh [-b|-r]${NC}"