From 1695a6bb8d8f5bc803bb9b1e9c52114c90a9d259 Mon Sep 17 00:00:00 2001 From: Alexis Date: Sat, 17 Aug 2024 16:44:08 +0200 Subject: [PATCH] Add backup file --- backup.sh | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 backup.sh diff --git a/backup.sh b/backup.sh new file mode 100644 index 0000000..02041de --- /dev/null +++ b/backup.sh @@ -0,0 +1,31 @@ +#!/bin/bash + +# Crontab example: 0 2 * * * /path/to/backup_script.sh >> /path/to/backup.log 2>&1 + +# Variables +BACKUP_SRC="/home/ubuntu/CTFd-Plugins/data" +BACKUP_DEST="/home/ubuntu/CTFd-Plugins/backups" # Feel free to move the backup folder to another location in order to have a real backup +DOCKER_COMPOSE_PATH="/home/ubuntu/CTFd-Plugins/" +BACKUP_COUNT=4 # Number of backups to keep +TIMESTAMP=$(date +"%Y%m%d%H%M%S") +BACKUP_NAME="backup_$TIMESTAMP.tar.gz" + +# Stop Docker stack +echo "Stopping Docker stack..." +cd $DOCKER_COMPOSE_PATH +docker compose stop + +# Create backup +echo "Creating backup of $BACKUP_SRC..." +mkdir -p $BACKUP_DEST +tar -czf $BACKUP_DEST/$BACKUP_NAME $BACKUP_SRC + +# Remove old backups +echo "Removing old backups, keeping the latest $BACKUP_COUNT..." +cd $BACKUP_DEST +ls -1tr | head -n -$BACKUP_COUNT | xargs -d '\n' rm -f -- + +cd $DOCKER_COMPOSE_PATH +docker compose start + +echo "Backup completed successfully." \ No newline at end of file