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
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}"
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m' # No Color
# Function to print colored messages
print_msg() {
echo -e "${GREEN}[INFO]${NC} $1"
}
print_error() {
echo -e "${RED}[ERROR]${NC} $1"
}
print_warning() {
echo -e "${YELLOW}[WARNING]${NC} $1"
}
# Check if running as root
if [ "$EUID" -ne 0 ]; then
print_error "Please run as root"
exit 1
fi
# Create backup directory if it doesn't exist
if [ ! -d "$BACKUP_DIR" ]; then
print_msg "Creating backup directory: $BACKUP_DIR"
mkdir -p "$BACKUP_DIR" mkdir -p "$BACKUP_DIR"
fi
echo "[*] Stopping NetBird stack..." # Change to compose directory
docker compose down cd "$COMPOSE_DIR" || {
print_error "Failed to change to directory: $COMPOSE_DIR"
exit 1
}
echo "[*] Backing up Docker volumes..." print_msg "Starting backup process..."
VOLUMES=( print_msg "Timestamp: $TIMESTAMP"
"netbird_zdb_data"
"netbird_management"
"netbird_zitadel_certs"
)
TEMP_DIR="/tmp/netbird_backup_${TIMESTAMP}" # Stop docker compose services
mkdir -p "$TEMP_DIR" 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
for VOL in "${VOLUMES[@]}"; do # Create backup (excluding the backup directory itself and git directory)
echo "[*] Exporting volume: $VOL" print_msg "Creating backup archive: $BACKUP_NAME"
docker run --rm -v ${VOL}:/volume -v $TEMP_DIR:/backup \ tar -czf "$BACKUP_PATH" \
alpine tar -czf /backup/${VOL}.tar.gz -C /volume . --exclude='./backup' \
done --exclude='./.git' \
--exclude='*.tar.gz' \
-C "$COMPOSE_DIR" .
echo "[*] Backing up configuration files..." if [ $? -eq 0 ]; then
tar -czf "$BACKUP_FILE" \ print_msg "Backup created successfully: $BACKUP_PATH"
docker-compose.yml \ BACKUP_SIZE=$(du -h "$BACKUP_PATH" | cut -f1)
*.env \ print_msg "Backup size: $BACKUP_SIZE"
management.json \ else
turnserver.conf \ print_error "Failed to create backup"
machinekey \ docker-compose up -d
traefik-stack \ exit 1
backup-netbird.sh \ fi
$TEMP_DIR/*.tar.gz
echo "[*] Cleaning temporary files..." # Restart docker compose services
rm -rf "$TEMP_DIR" print_msg "Starting Docker Compose services..."
echo "[*] Starting NetBird stack again..."
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