Files
openclaw-backups/automations/backup-to-gitea.sh
2026-02-21 07:01:51 +00:00

122 lines
3.9 KiB
Bash
Executable File

#!/bin/bash
# Daily backup script for OpenClaw workspace to GitTea
set -e
TIMESTAMP=$(date +"%Y-%m-%d %H:%M")
LOG_FILE="/tmp/openclaw-backup.log"
echo "🔄 Starting OpenClaw backup... [$TIMESTAMP]" | tee -a "$LOG_FILE"
# GitTea credentials (stored in environment or config)
# For automated backups, credentials should be configured in ~/.git-credentials
# or use SSH keys
# Check if git credentials are configured
if ! git config --global credential.helper &>/dev/null; then
echo "⚠️ Warning: Git credential helper not configured" | tee -a "$LOG_FILE"
echo " Run: git config --global credential.helper store" | tee -a "$LOG_FILE"
fi
# ntfy alert function
send_ntfy() {
local title="$1"
local message="$2"
local priority="${3:-4}"
local sound="${4:-default}"
[[ -z "${NTFY_URL:-}" ]] && return 0
[[ -z "${NTFY_TOPIC:-}" ]] && return 0
# Enforce minimum priority (default 4)
local minp="${NTFY_MIN_PRIORITY:-4}"
if [[ "$priority" =~ ^[0-9]+$ ]] && [[ "$minp" =~ ^[0-9]+$ ]]; then
if (( priority < minp )); then
priority="$minp"
fi
fi
curl -s -X POST "${NTFY_URL%/}/${NTFY_TOPIC}" \
-H "Title: $title" \
-H "Priority: $priority" \
-H "Sound: $sound" \
-d "$message" > /dev/null 2>&1 || true
}
# Snapshot critical OpenClaw state into workspace repo so updates/reinstalls are recoverable
snapshot_state_files() {
local src_root="/home/openclaw/.openclaw"
local dst_root="/home/openclaw/.openclaw/workspace/state-backup"
mkdir -p "$dst_root" "$dst_root/cron" "$dst_root/skills" "$dst_root/devices"
# Copy key state files if present
[[ -f "$src_root/openclaw.json" ]] && cp "$src_root/openclaw.json" "$dst_root/openclaw.json"
[[ -f "$src_root/cron/jobs.json" ]] && cp "$src_root/cron/jobs.json" "$dst_root/cron/jobs.json"
[[ -f "$src_root/devices/paired.json" ]] && cp "$src_root/devices/paired.json" "$dst_root/devices/paired.json"
# Skill metadata (if any)
if compgen -G "$src_root/skills/*.json" > /dev/null; then
cp "$src_root/skills"/*.json "$dst_root/skills/"
fi
# Helpful restore/readme metadata
cat > "$dst_root/README.md" <<EOF
# OpenClaw State Backup
This folder is auto-generated by automations/backup-to-gitea.sh before each git backup.
Included state:
- openclaw.json
- cron/jobs.json
- devices/paired.json
- skills/*.json
Generated at: $(date -u +"%Y-%m-%dT%H:%M:%SZ")
EOF
echo "🧠 State snapshot refreshed in state-backup/" | tee -a "$LOG_FILE"
}
# Function to backup a repo
backup_repo() {
local path=$1
local name=$2
cd "$path"
# Check if there are changes
if [[ -n $(git status --porcelain) ]]; then
echo "📦 $name: Changes detected, committing..." | tee -a "$LOG_FILE"
git add -A
git commit -m "Auto backup: $TIMESTAMP" || echo "⚠️ Commit failed or nothing to commit"
echo "☁️ $name: Pushing to GitTea..." | tee -a "$LOG_FILE"
if git push origin main 2>&1 | tee -a "$LOG_FILE"; then
echo "$name: Backup successful" | tee -a "$LOG_FILE"
else
echo "$name: Push failed - check credentials" | tee -a "$LOG_FILE"
send_ntfy "⚠️ Backup Failed" "$name push to Gitea failed. Check credentials." 4 default
fi
else
echo "⏭️ $name: No changes to backup" | tee -a "$LOG_FILE"
fi
}
# Backup workspace
echo "📂 Backing up workspace..." | tee -a "$LOG_FILE"
snapshot_state_files
backup_repo "/home/openclaw/.openclaw/workspace" "workspace"
echo "" | tee -a "$LOG_FILE"
echo "✅ Backup process complete!" | tee -a "$LOG_FILE"
echo "📄 Log saved to: $LOG_FILE" | tee -a "$LOG_FILE"
# Show recent backups
echo "" | tee -a "$LOG_FILE"
echo "📊 Recent backups:" | tee -a "$LOG_FILE"
cd /home/openclaw/.openclaw/workspace && git log --oneline -3 | tee -a "$LOG_FILE"
# Send success notification
send_ntfy "✅ Backup Complete" "OpenClaw backup to Gitea succeeded" 4 default