From 5cae723464196c94aa97f6690d0a1012e83254bb Mon Sep 17 00:00:00 2001 From: Angelo Fausti Date: Fri, 3 Jan 2025 12:44:44 -0700 Subject: [PATCH 1/3] Add kubectl and jq installation --- Dockerfile | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/Dockerfile b/Dockerfile index dd3c09c..3e7dc94 100644 --- a/Dockerfile +++ b/Dockerfile @@ -5,7 +5,7 @@ FROM influxdb:1.11.8-meta # Install pipx and use it to install gsutil which is required for the backup script # to upload the backup files to Google Cloud Storage RUN apt-get update && \ - apt-get install -y python3 python3-pip pipx && \ + apt-get install -y python3 python3-pip pipx jq && \ pipx install gsutil && \ apt-get clean && \ rm -rf /var/lib/apt/lists/* @@ -16,6 +16,11 @@ ENV PATH="/root/.local/bin:$PATH" # Verify gsutil installation RUN gsutil --version +# Install kubectl +RUN curl -LO "https://dl.k8s.io/release/$(curl -L -s https://dl.k8s.io/release/stable.txt)/bin/linux/amd64/kubectl" && \ + chmod +x kubectl && \ + mv kubectl /usr/local/bin/ + # Add the backup script COPY backup/backup.sh /usr/local/bin/backup.sh RUN chmod +x /usr/local/bin/backup.sh From 2cf6f0ee7e919f9116fbd9df157c85a21ba9501a Mon Sep 17 00:00:00 2001 From: Angelo Fausti Date: Fri, 3 Jan 2025 12:45:58 -0700 Subject: [PATCH 2/3] Restructure backup script - Backup Chronograf and Kapacitor databases - Use functions to execute backup tasks - Redirect standard output and error to keep the output clean --- backup/backup.sh | 77 ++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 65 insertions(+), 12 deletions(-) diff --git a/backup/backup.sh b/backup/backup.sh index 7cb7a80..713cbb8 100644 --- a/backup/backup.sh +++ b/backup/backup.sh @@ -2,22 +2,75 @@ set -e -# Configuration -BACKUP_DIR="/backup/sasquatch-influxdb-enterprise-backup" -GCS_BUCKET="gs://your-gcs-bucket" - -# Ensure the backup directory exists -mkdir -p "$BACKUP_DIR" - -echo "Starting InfluxDB Enterprise backup..." +backup_chronograf() { + echo "Backing up Chronograf..." + pod=$(kubectl get pods -n sasquatch -l app=sasquatch-chronograf -o jsonpath='{.items[0].metadata.name}') + backup_dir="/backup/chronograf-$(date +%Y-%m-%d)" + mkdir -p "$backup_dir" + kubectl cp -n sasquatch "$pod:/var/lib/chronograf/chronograf-v1.db" "$backup_dir/chronograf-v1.db" > /dev/null 2>&1 + if [ $? -eq 0 ] && [ -f "$backup_dir/chronograf-v1.db" ]; then + echo "Backup completed successfully at $backup_dir." + else + echo "Backup failed!" >&2 + exit 1 + fi +} -influxd-ctl -bind sasquatch-influxdb-enterprise-meta.sasquatch:8091 backup -strategy incremental "$BACKUP_DIR" +backup_kapacitor() { + echo "Backing up Kapacitor..." + pod=$(kubectl get pods -n sasquatch -l app=sasquatch-kapacitor -o jsonpath='{.items[0].metadata.name}') + backup_dir="/backup/kapacitor-$(date +%Y-%m-%d)" + mkdir -p "$backup_dir" + kubectl cp -n sasquatch "$pod:/var/lib/kapacitor/kapacitor.db" "$backup_dir/kapacitor.db" > /dev/null 2>&1 + if [ $? -eq 0 ] && [ -f "$backup_dir/kapacitor.db" ]; then + echo "Backup completed successfully at $backup_dir." + else + echo "Backup failed!" >&2 + exit 1 + fi +} -if [ $? -eq 0 ]; then - echo "Backup completed successfully at $BACKUP_DIR." -else +backup_influxdb_enterprise() { + echo "Backing up InfluxDB Enterprise (incremental backup)..." + backup_dir="/backup/sasquatch-influxdb-enterprise-backup" + backup_logs="/backup/sasquatch-influxdb-enterprise-backup/backup-$(date +%Y-%m-%d).logs" + mkdir -p "$backup_dir" + influxd-ctl -bind sasquatch-influxdb-enterprise-meta.sasquatch:8091 backup -strategy incremental "$backup_dir" > $backup_logs 2>&1 + if [ $? -eq 0 ]; then + echo "Backup completed successfully at $backup_dir." + else echo "Backup failed!" >&2 exit 1 + fi +} + +if [ -z "$BACKUP_ITEMS" ]; then + echo "No backup items specified. Exiting." + exit 0 fi +BACKUP_ITEMS=$(echo "$BACKUP_ITEMS" | jq -c '.[]') + +for item in $BACKUP_ITEMS; do + name=$(echo "$item" | jq -r '.name') + enabled=$(echo "$item" | jq -r '.enabled') + if [ "$enabled" == "true" ]; then + case "$name" in + "chronograf") + backup_chronograf + ;; + "kapacitor") + backup_kapacitor + ;; + "influxdb-enterprise") + backup_influxdb_enterprise + ;; + *) + echo "Unknown backup item: $name. Skipping..." + ;; + esac + else + echo "Skipping $name..." + fi +done From 5b569a223dc06c3334d0894b7f6c770727232664 Mon Sep 17 00:00:00 2001 From: Angelo Fausti Date: Mon, 6 Jan 2025 10:48:19 -0700 Subject: [PATCH 3/3] Implement backup retention - retention_days parameter is optional, it doesn't apply to incremental backups for example. --- backup/backup.sh | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/backup/backup.sh b/backup/backup.sh index 713cbb8..99863b6 100644 --- a/backup/backup.sh +++ b/backup/backup.sh @@ -10,6 +10,8 @@ backup_chronograf() { kubectl cp -n sasquatch "$pod:/var/lib/chronograf/chronograf-v1.db" "$backup_dir/chronograf-v1.db" > /dev/null 2>&1 if [ $? -eq 0 ] && [ -f "$backup_dir/chronograf-v1.db" ]; then echo "Backup completed successfully at $backup_dir." + echo "Cleaning up backups older than $retention_days day(s)..." + find /backup -type d -name "chronograf-*" -mtime +$retention_days -exec rm -rf {} \; else echo "Backup failed!" >&2 exit 1 @@ -24,13 +26,15 @@ backup_kapacitor() { kubectl cp -n sasquatch "$pod:/var/lib/kapacitor/kapacitor.db" "$backup_dir/kapacitor.db" > /dev/null 2>&1 if [ $? -eq 0 ] && [ -f "$backup_dir/kapacitor.db" ]; then echo "Backup completed successfully at $backup_dir." + echo "Cleaning up backups older than $retention_days day(s)..." + find /backup -type d -name "kapacitor-*" -mtime +$retention_days -exec rm -rf {} \; else echo "Backup failed!" >&2 exit 1 fi } -backup_influxdb_enterprise() { +backup_influxdb_enterprise_incremental() { echo "Backing up InfluxDB Enterprise (incremental backup)..." backup_dir="/backup/sasquatch-influxdb-enterprise-backup" backup_logs="/backup/sasquatch-influxdb-enterprise-backup/backup-$(date +%Y-%m-%d).logs" @@ -54,6 +58,7 @@ BACKUP_ITEMS=$(echo "$BACKUP_ITEMS" | jq -c '.[]') for item in $BACKUP_ITEMS; do name=$(echo "$item" | jq -r '.name') enabled=$(echo "$item" | jq -r '.enabled') + retention_days=$(echo "$item" | jq -r '.retention_days') if [ "$enabled" == "true" ]; then case "$name" in @@ -63,8 +68,8 @@ for item in $BACKUP_ITEMS; do "kapacitor") backup_kapacitor ;; - "influxdb-enterprise") - backup_influxdb_enterprise + "influxdb-enterprise-incremental") + backup_influxdb_enterprise_incremental ;; *) echo "Unknown backup item: $name. Skipping..." @@ -74,3 +79,5 @@ for item in $BACKUP_ITEMS; do echo "Skipping $name..." fi done +echo "Backup contents:" +ls -lhtR /backup