modify and rewrite backup script

This commit is contained in:
Dejan R. 2025-11-27 16:23:27 +00:00
parent 172d2d88d3
commit 15b61c2194

130
scripts/backup-netbird.sh Normal file → Executable file
View file

@ -1,49 +1,105 @@
#!/bin/bash #!/bin/bash
set -e
BACKUP_DIR="/home/Dejan/Docker/Netbird-compose/backup" # Netbird Docker Compose Backup Script
TIMESTAMP=$(date +"%Y-%m-%d_%H-%M-%S") # This script stops the services, creates a timestamped backup, and restarts services
BACKUP_FILE="${BACKUP_DIR}/netbird_backup_${TIMESTAMP}.tar.gz"
echo "[*] Creating backup directory..." # Configuration
mkdir -p "$BACKUP_DIR" COMPOSE_DIR="/home/Dejan/Docker/Netbird-compose"
BACKUP_DIR="${COMPOSE_DIR}/backup"
TIMESTAMP=$(date +"%Y%m%d_%H%M%S")
BACKUP_NAME="netbird-backup-${TIMESTAMP}.tar.gz"
BACKUP_PATH="${BACKUP_DIR}/${BACKUP_NAME}"
echo "[*] Stopping NetBird stack..." # Colors for output
docker compose down RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m' # No Color
echo "[*] Backing up Docker volumes..." # Function to print colored messages
VOLUMES=( print_msg() {
"netbird_zdb_data" echo -e "${GREEN}[INFO]${NC} $1"
"netbird_management" }
"netbird_zitadel_certs"
)
TEMP_DIR="/tmp/netbird_backup_${TIMESTAMP}" print_error() {
mkdir -p "$TEMP_DIR" echo -e "${RED}[ERROR]${NC} $1"
}
for VOL in "${VOLUMES[@]}"; do print_warning() {
echo "[*] Exporting volume: $VOL" echo -e "${YELLOW}[WARNING]${NC} $1"
docker run --rm -v ${VOL}:/volume -v $TEMP_DIR:/backup \ }
alpine tar -czf /backup/${VOL}.tar.gz -C /volume .
done
echo "[*] Backing up configuration files..." # Check if running as root
tar -czf "$BACKUP_FILE" \ if [ "$EUID" -ne 0 ]; then
docker-compose.yml \ print_error "Please run as root"
*.env \ exit 1
management.json \ fi
turnserver.conf \
machinekey \
traefik-stack \
backup-netbird.sh \
$TEMP_DIR/*.tar.gz
echo "[*] Cleaning temporary files..." # Create backup directory if it doesn't exist
rm -rf "$TEMP_DIR" if [ ! -d "$BACKUP_DIR" ]; then
print_msg "Creating backup directory: $BACKUP_DIR"
mkdir -p "$BACKUP_DIR"
fi
echo "[*] Starting NetBird stack again..." # Change to compose directory
cd "$COMPOSE_DIR" || {
print_error "Failed to change to directory: $COMPOSE_DIR"
exit 1
}
print_msg "Starting backup process..."
print_msg "Timestamp: $TIMESTAMP"
# Stop docker compose services
print_msg "Stopping Docker Compose services..."
docker compose stop
if [ $? -eq 0 ]; then
print_msg "Services stopped successfully"
else
print_error "Failed to stop services"
exit 1
fi
# Create backup (excluding the backup directory itself and git directory)
print_msg "Creating backup archive: $BACKUP_NAME"
tar -czf "$BACKUP_PATH" \
--exclude='./backup' \
--exclude='./.git' \
--exclude='*.tar.gz' \
-C "$COMPOSE_DIR" .
if [ $? -eq 0 ]; then
print_msg "Backup created successfully: $BACKUP_PATH"
BACKUP_SIZE=$(du -h "$BACKUP_PATH" | cut -f1)
print_msg "Backup size: $BACKUP_SIZE"
else
print_error "Failed to create backup"
docker-compose up -d
exit 1
fi
# Restart docker compose services
print_msg "Starting Docker Compose services..."
docker compose up -d docker compose up -d
if [ $? -eq 0 ]; then
print_msg "Services started successfully"
else
print_error "Failed to start services"
exit 1
fi
echo "======================================================" # Optional: Keep only last N backups (uncomment to enable)
echo "Backup created: $BACKUP_FILE" # KEEP_BACKUPS=5
echo "======================================================" # print_msg "Cleaning old backups, keeping last $KEEP_BACKUPS..."
# cd "$BACKUP_DIR"
# ls -t netbird-backup-*.tar.gz | tail -n +$((KEEP_BACKUPS + 1)) | xargs -r rm
# print_msg "Old backups cleaned"
print_msg "Backup process completed successfully!"
print_msg "Backup location: $BACKUP_PATH"
# List recent backups
print_msg "Recent backups:"
ls -lht "$BACKUP_DIR"/netbird-backup-*.tar.gz | head -n 5
exit 0