Repository navigation
Expand file tree
/
Copy pathbackup.sh
More file actions
executable file
·178 lines (148 loc) · 6.27 KB
/
Copy pathbackup.sh
File metadata and controls
executable file
·178 lines (148 loc) · 6.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
#!/usr/bin/env bash
set -e
# =============================================================================
# SiliconBeest — Backup Script
# Exports D1 database and lists R2 objects.
# =============================================================================
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
source "$SCRIPT_DIR/config.sh"
[[ -f "$SCRIPT_DIR/config.env" ]] && source "$SCRIPT_DIR/config.env"
BACKUP_DIR="$PROJECT_ROOT/backups"
# ---------------------------------------------------------------------------
# Parse arguments
# ---------------------------------------------------------------------------
SKIP_R2=false
OUTPUT_DIR=""
while [[ $# -gt 0 ]]; do
case "$1" in
--output-dir|-o) OUTPUT_DIR="$2"; shift 2 ;;
--skip-r2) SKIP_R2=true; shift ;;
-h|--help)
echo "Usage: backup.sh [OPTIONS]"
echo
echo "Options:"
echo " -o, --output-dir DIR Directory for backup files (default: ./backups)"
echo " --skip-r2 Skip R2 bucket listing"
echo " -h, --help Show this help"
exit 0
;;
*) error "Unknown option: $1"; exit 1 ;;
esac
done
BACKUP_DIR="${OUTPUT_DIR:-$BACKUP_DIR}"
# ---------------------------------------------------------------------------
# Prerequisites
# ---------------------------------------------------------------------------
header "Checking Prerequisites"
if ! command -v wrangler &>/dev/null; then
error "wrangler CLI is not installed."
exit 1
fi
success "wrangler found"
if ! wrangler whoami &>/dev/null; then
error "Not logged in to Cloudflare."
exit 1
fi
success "Authenticated"
# ---------------------------------------------------------------------------
# Create backup directory
# ---------------------------------------------------------------------------
TIMESTAMP=$(date +%Y%m%d_%H%M%S)
BACKUP_SUBDIR="$BACKUP_DIR/$TIMESTAMP"
mkdir -p "$BACKUP_SUBDIR"
info "Backup directory: $BACKUP_SUBDIR"
# ---------------------------------------------------------------------------
# Export D1 Database
# ---------------------------------------------------------------------------
header "Backing Up D1 Database"
DB_NAME="$D1_DATABASE_NAME"
DB_BACKUP_FILE="$BACKUP_SUBDIR/d1_${DB_NAME}_${TIMESTAMP}.sql"
info "Exporting D1 database: $DB_NAME"
# Export each table's data
TABLES=(
accounts users actor_keys statuses media_attachments polls poll_votes
follows follow_requests favourites blocks mutes bookmarks
notifications mentions tags status_tags tag_follows
oauth_applications oauth_access_tokens oauth_authorization_codes
lists list_accounts instances domain_blocks domain_allows
web_push_subscriptions reports account_warnings ip_blocks email_domain_blocks
markers user_preferences filters filter_keywords filter_statuses
settings custom_emojis announcements rules
conversations conversation_accounts
)
echo "-- SiliconBeest D1 Database Backup" > "$DB_BACKUP_FILE"
echo "-- Generated: $(date -u +%Y-%m-%dT%H:%M:%SZ)" >> "$DB_BACKUP_FILE"
echo "-- Database: $DB_NAME" >> "$DB_BACKUP_FILE"
echo "" >> "$DB_BACKUP_FILE"
for TABLE in "${TABLES[@]}"; do
info " Exporting table: $TABLE"
RESULT=$(wrangler d1 execute "$DB_NAME" --remote --command "SELECT * FROM $TABLE;" --json 2>/dev/null || echo "[]")
# Write table data as JSON (for later import)
echo "-- Table: $TABLE" >> "$DB_BACKUP_FILE"
echo "-- Data (JSON): $RESULT" >> "$DB_BACKUP_FILE"
echo "" >> "$DB_BACKUP_FILE"
done
success "D1 database exported to: $DB_BACKUP_FILE"
# Also get row counts for summary
info "Gathering table statistics..."
STATS_FILE="$BACKUP_SUBDIR/d1_stats_${TIMESTAMP}.txt"
echo "SiliconBeest D1 Database Statistics" > "$STATS_FILE"
echo "Generated: $(date -u +%Y-%m-%dT%H:%M:%SZ)" >> "$STATS_FILE"
echo "---" >> "$STATS_FILE"
for TABLE in "${TABLES[@]}"; do
COUNT=$(wrangler d1 execute "$DB_NAME" --remote --command "SELECT COUNT(*) as count FROM $TABLE;" --json 2>/dev/null | node -e "
try {
const data = JSON.parse(require('fs').readFileSync('/dev/stdin','utf8'));
const results = Array.isArray(data) ? data[0]?.results : data?.results;
process.stdout.write(String(results?.[0]?.count ?? '?'));
} catch(e) { process.stdout.write('?'); }
" 2>/dev/null || echo "?")
echo " $TABLE: $COUNT rows" >> "$STATS_FILE"
done
success "Statistics saved to: $STATS_FILE"
# ---------------------------------------------------------------------------
# List R2 Objects
# ---------------------------------------------------------------------------
if [[ "$SKIP_R2" == false ]]; then
header "Listing R2 Objects"
BUCKET_NAME="$R2_BUCKET_NAME"
R2_LIST_FILE="$BACKUP_SUBDIR/r2_objects_${TIMESTAMP}.txt"
info "Listing objects in R2 bucket: $BUCKET_NAME"
wrangler r2 object list "$BUCKET_NAME" 2>/dev/null > "$R2_LIST_FILE" || \
warn "Could not list R2 objects. The bucket may be empty or inaccessible."
if [[ -s "$R2_LIST_FILE" ]]; then
OBJECT_COUNT=$(wc -l < "$R2_LIST_FILE" | tr -d ' ')
success "R2 object listing saved ($OBJECT_COUNT entries): $R2_LIST_FILE"
else
info "R2 bucket appears empty or listing returned no results."
fi
else
info "Skipping R2 listing (--skip-r2)"
fi
# ---------------------------------------------------------------------------
# Note on KV Namespaces
# ---------------------------------------------------------------------------
header "KV Namespace Notes"
info "FEDIFY_KV stores Fedify federation state (signature caches, etc.)."
info "CACHE and SESSIONS KV data is ephemeral and does not need backup."
info "FEDIFY_KV data is also ephemeral (signature caches) and will be"
info " repopulated automatically. No KV backup is typically needed."
# ---------------------------------------------------------------------------
# Summary
# ---------------------------------------------------------------------------
header "Backup Complete"
echo -e "${GREEN}${BOLD}Backup completed successfully!${NC}"
echo
echo -e " ${BOLD}Backup directory:${NC} $BACKUP_SUBDIR"
echo -e " ${BOLD}Files:${NC}"
for FILE in "$BACKUP_SUBDIR"/*; do
if [[ -f "$FILE" ]]; then
SIZE=$(du -h "$FILE" | cut -f1)
echo -e " $(basename "$FILE") ($SIZE)"
fi
done
echo
echo -e "${YELLOW}Note:${NC} R2 objects are listed but not downloaded."
echo " To download specific R2 objects, use:"
echo " wrangler r2 object get $R2_BUCKET_NAME <key> --file <local-path>"
echo