commit 08e2b62a9a6dd8f8ac08f0e94b9aa632d5a87a1e Author: William Petit Date: Thu Oct 22 09:38:20 2020 +0200 chore: initial commit diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..f16c1f1 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/backup-* \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..31885e2 --- /dev/null +++ b/README.md @@ -0,0 +1,55 @@ +# sentry-util + +Script d'aide au suivi du cycle de vie de l'application Sentry. + +## Usage + +### Sauvegarde et restauration + +Deux commandes sont implémentées afin de permettre de sauvegarder et restaurer un environnement Sentry. + +#### Effectuer une sauvegarde + +Sur la machine hébergeant l'environnement Sentry: + +1. Couper l'application Sentry + + ``` + docker-compose down + ``` + +2. Lancer la sauvegarde + + ``` + ./sentry-util backup + ``` + +3. Redémarrer l'application Sentry + + ``` + docker-compose up + ``` + +Le script va créer un dossier `backup-` dans le répertoire d'exécution. Ce dossier contient les données des différents volumes externes de l'application Sentry. + +#### Restaurer une sauvegarde + +Sur la machine hébergeant l'environnement Sentry: + +1. Couper l'application Sentry + + ``` + docker-compose down + ``` + +2. Lancer la restauration + + ``` + ./sentry-util restore # Où YYYYMMDD correspond à la date de la sauvegarde + ``` + +3. Redémarrer l'application Sentry + + ``` + docker-compose up + ``` \ No newline at end of file diff --git a/sentry-util.sh b/sentry-util.sh new file mode 100755 index 0000000..25b6c55 --- /dev/null +++ b/sentry-util.sh @@ -0,0 +1,99 @@ +#!/usr/bin/env bash + +set -eo pipefail + +DOCKER_VOLUMES="sentry-postgres sentry-data sentry-redis sentry-zookeeper sentry-kafka sentry-clickhouse sentry-symbolicator" +BACKUP_BASE_DIR="." +BACKUP_DIR_PREFIX="backup-" +CONTAINER_VOLUME_BASE_DIR="/volumes" +CONTAINER_BACKUP_BASE_DIR="/backup" +SCRIPT_DIR="$( cd "$(dirname "$0")" >/dev/null 2>&1 ; pwd -P )" +SCRIPT_NAME="sentry-util.sh" + +function_exists() { + set +e + declare -f -F $1 > /dev/null + local ret=$? + set -e + echo $ret +} + +function backup { + [ ! -z "$BACKUP_BASE_DIR" ] && mkdir -p "$BACKUP_BASE_DIR" + + local today=$(date +%Y%m%d) + local backup_dir=$(readlink -f "$BACKUP_BASE_DIR/$BACKUP_DIR_PREFIX$today") + + mkdir -p "$backup_dir" + + echo "Creating backup directory '$backup_dir'..." + + run_container "backup-volumes" "-v $backup_dir:$CONTAINER_BACKUP_BASE_DIR" +} + +function run_container { + local docker_cmd="$1" + local additional_docker_args="$2" + + local volume_args="" + for volume in $DOCKER_VOLUMES; do + volume_args="$volume_args -v $volume:$CONTAINER_VOLUME_BASE_DIR/$volume" + done + + docker run --rm -it \ + $volume_args \ + $additional_docker_args \ + -v "$SCRIPT_DIR/$SCRIPT_NAME:/usr/local/bin/$SCRIPT_NAME" \ + bash:5 \ + /usr/local/bin/$SCRIPT_NAME $docker_cmd +} + +function backup-volumes { + for volume in $DOCKER_VOLUMES; do + local volume_dir="$CONTAINER_VOLUME_BASE_DIR/$volume" + echo "Copying volume '$volume' data..." + rm -rf "$CONTAINER_BACKUP_BASE_DIR/$volume" + cp -aR "$volume_dir" "$CONTAINER_BACKUP_BASE_DIR/" + done +} + +function restore-volumes { + for volume in $DOCKER_VOLUMES; do + local volume_dir="$CONTAINER_VOLUME_BASE_DIR/$volume" + echo "Restoring volume '$volume' data..." + rm -rf "$volume_dir/"* + cp -aR "$CONTAINER_BACKUP_BASE_DIR/$volume/"* "$volume_dir/" + done +} + +function restore { + local backup_date=$1 + + if [ -z "$backup_date" ]; then + echo 1>&2 "Vous devez spécifier une date au format YYYYMMDD en premier argument." + exit 1 + fi + + local backup_dir=$(readlink -f "$BACKUP_BASE_DIR/$BACKUP_DIR_PREFIX$backup_date") + + if [ ! -d "$backup_dir" ]; then + echo 1>&2 "Le dossier de sauvegarde '$backup_dir' n'existe pas." + exit 1 + fi + + run_container "restore-volumes" "-v $backup_dir:$CONTAINER_BACKUP_BASE_DIR" +} + +function main { + local command=$1 + local command_args=${@:2} + + if [ "$(function_exists $command)" == "1" ]; then + echo 1>&2 "La commande '$command' n'existe pas." + exit 1 + fi + + $command $command_args +} + +main $@