backup-bot-two/backup.sh

60 lines
1.6 KiB
Bash
Executable File

#!/bin/bash
### FIXME: just for testing
echo $RESTIC_PASSWORD_FILE
echo $RESTIC_REPOSITORY
echo $AWS_ACCESS_KEY_ID
echo $AWS_SECRET_ACCESS_KEY
export DOCKER_CONTEXT=default
mkdir /tmp/backups
backup_path=/tmp/backups
mapfile -t services < <(docker service ls --format '{{ .Name }}')
# FIXME: just for testing
services=("cloud_local_db")
for service in "${services[@]}"; do
echo "service: $service"
details=$(docker service inspect "$service" --format "{{ json .Spec.Labels }}")
if echo "$details" | jq -r '.["backupbot.backup"]' | grep -q 'true'; then
pre=$(echo "$details" | jq -r '.["backupbot.backup.pre-hook"]')
post=$(echo "$details" | jq -r '.["backupbot.backup.post-hook"]')
path=$(echo "$details" | jq -r '.["backupbot.backup.path"]')
if [ "$path" = "null" ]; then
echo "ERROR: missing 'path' for $service"
continue # or maybe exit?
fi
container=$(docker container ls -f "name=$service" --format '{{ .ID }}')
echo "backing up $service"
test -d "$backup_path/$service" || mkdir "$backup_path/$service"
if [ "$pre" != "null" ]; then
# run the precommand
# shellcheck disable=SC2086
docker exec "$container" $pre
fi
# run the backup
docker cp -a "$container:$path" "$backup_path/$service"
if [ "$post" != "null" ]; then
# run the postcommand
# shellcheck disable=SC2086
docker exec "$container" $post
fi
fi
# Check if restic repo exists
if [ -z "$(restic cat config)" ] 2>/dev/null; then
echo "initializing restic repo"
restic init "$backup_path"
fi
restic backup --tag coop-cloud "$backup_path" # --quiet
done