Skip to content

Commit

Permalink
Merge pull request #56 from lsst-sqre/tickets/DM-48369
Browse files Browse the repository at this point in the history
[DM-48369] Add restore script
  • Loading branch information
afausti authored Jan 14, 2025
2 parents 91ebc0e + 27e5abb commit 6409e7d
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 44 deletions.
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# Change log

<!-- Format for headings: 1.2.3 (YYYY-MM-DD) -->
## 1.3.0 (2025-01-14)

- Add ability to restore InfluxDB OSS backups

## 1.2.0 (2025-01-08)

Expand Down
4 changes: 4 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ RUN influxd version
COPY backup/backup.sh /usr/local/bin/backup.sh
RUN chmod +x /usr/local/bin/backup.sh

# Add the restore script
COPY backup/restore.sh /usr/local/bin/restore.sh
RUN chmod +x /usr/local/bin/restore.sh

# Create a new user to run the backup script
RUN useradd --create-home sasquatch

Expand Down
81 changes: 38 additions & 43 deletions backup/backup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,79 +2,73 @@

set -e

# Function to generate a timestamp in ISO 8601 format without separators
get_timestamp() {
date -u +"%Y%m%dT%H%M%SZ"
}

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)"
backup_dir="/backup/chronograf-$(get_timestamp)"
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."
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
fi
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 {} \;
}

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)"
backup_dir="/backup/kapacitor-$(get_timestamp)"
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."
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
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 {} \;
}

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"
backup_logs="$backup_dir/backup-$(get_timestamp).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

influxd-ctl -bind sasquatch-influxdb-enterprise-meta.sasquatch:8091 backup -strategy incremental "$backup_dir" > "$backup_logs" 2>&1
echo "Backup completed successfully at $backup_dir. Logs stored at $backup_logs."
}

backup_influxdb_oss() {
backup_influxdb_oss_full() {
echo "Backing up InfluxDB OSS (full backup)..."
backup_dir="/backup/sasquatch-influxdb-oss-$(date +%Y-%m-%d)"
backup_logs="/backup/sasquatch-influxdb-oss-$(date +%Y-%m-%d)/backup.logs"
backup_dir="/backup/sasquatch-influxdb-oss-full-$(get_timestamp)"
backup_logs="$backup_dir/backup.logs"
mkdir -p "$backup_dir"
influxd backup -portable -host sasquatch-influxdb.sasquatch:8088 "$backup_dir" > $backup_logs 2>&1
if [ $? -eq 0 ]; then
echo "Backup completed successfully at $backup_dir."
echo "Cleaning up backups older than $retention_days day(s)..."
find /backup -type d -name "sasquatch-influxdb-oss-*" -mtime +$retention_days -exec rm -rf {} \;
else
echo "Backup failed!" >&2
exit 1
fi
}

influxd backup -portable -host sasquatch-influxdb.sasquatch:8088 "$backup_dir" > "$backup_logs" 2>&1
echo "Backup completed successfully at $backup_dir. Logs stored at $backup_logs."

echo "Cleaning up backups older than $retention_days day(s)..."
find /backup -type d -name "sasquatch-influxdb-oss-*" -mtime +$retention_days -exec rm -rf {} \;
}

# Check if BACKUP_ITEMS is set
if [ -z "$BACKUP_ITEMS" ]; then
echo "No backup items specified. Exiting."
exit 0
fi

# Process backup items
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')
retention_days=$(echo "$item" | jq -r '.retentionDays')

if [ "$enabled" == "true" ]; then
case "$name" in
Expand All @@ -87,8 +81,8 @@ for item in $BACKUP_ITEMS; do
"influxdb-enterprise-incremental")
backup_influxdb_enterprise_incremental
;;
"influxdb-oss")
backup_influxdb_oss
"influxdb-oss-full")
backup_influxdb_oss_full
;;
*)
echo "Unknown backup item: $name. Skipping..."
Expand All @@ -98,5 +92,6 @@ for item in $BACKUP_ITEMS; do
echo "Skipping $name..."
fi
done

echo "Backup contents:"
ls -lhtR /backup
du -sh /backup/*
49 changes: 49 additions & 0 deletions backup/restore.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#!/bin/bash

set -e

# Function to check if the backup directory exists
check_backup_dir() {
if [ ! -d "$1" ]; then
echo "Backup directory $1 does not exist. Exiting."
exit 1
fi
}

restore_influxdb_oss_full() {
echo "Restoring InfluxDB OSS from backup..."
backup_dir="/backup/sasquatch-influxdb-oss-full-$backup_timestamp"

check_backup_dir "$backup_dir"

influxd restore -portable -host sasquatch-influxdb.sasquatch:8088 "$backup_dir" 2>&1
echo "Restore completed successfully."
}

# Check if RESTORE_ITEMS is set
if [ -z "$RESTORE_ITEMS" ]; then
echo "No restore items specified. Exiting."
exit 0
fi

RESTORE_ITEMS=$(echo "$RESTORE_ITEMS" | jq -c '.[]')

# Process each restore item
for item in $RESTORE_ITEMS; do
name=$(echo "$item" | jq -r '.name')
enabled=$(echo "$item" | jq -r '.enabled')
backup_timestamp=$(echo "$item" | jq -r '.backupTimestamp')

if [ "$enabled" == "true" ]; then
case "$name" in
"influxdb-oss-full")
restore_influxdb_oss_full
;;
*)
echo "Unknown restore item: $name. Skipping..."
;;
esac
else
echo "Skipping $name..."
fi
done

0 comments on commit 6409e7d

Please sign in to comment.