Supabase备份与恢复 #
一、备份概述 #
1.1 自动备份 #
text
Supabase自动备份
├── Pro计划: 每日自动备份
├── 保留7天
├── Dashboard可恢复
└── 包含完整数据库
1.2 手动备份 #
text
手动备份方式
├── pg_dump
├── Supabase CLI
├── Dashboard导出
└── 第三方工具
二、使用CLI备份 #
2.1 导出数据库 #
bash
# 导出整个数据库
supabase db dump -f backup.sql
# 只导出数据
supabase db dump --data-only -f data.sql
# 只导出结构
supabase db dump --schema-only -f schema.sql
# 导出特定表
supabase db dump --table users --table posts -f tables.sql
2.2 远程数据库备份 #
bash
# 导出远程数据库
supabase db dump --linked -f remote_backup.sql
三、使用pg_dump #
3.1 连接信息 #
text
Dashboard > Settings > Database
连接信息
├── Host: db.xxxxxxxx.supabase.co
├── Port: 5432
├── Database: postgres
├── User: postgres
└── Password: your-password
3.2 备份命令 #
bash
# 完整备份
pg_dump -h db.xxxxxxxx.supabase.co -U postgres -d postgres > backup.sql
# 自定义格式(推荐)
pg_dump -h db.xxxxxxxx.supabase.co -U postgres -d postgres -Fc > backup.dump
# 仅数据
pg_dump -h db.xxxxxxxx.supabase.co -U postgres -d postgres --data-only > data.sql
# 仅结构
pg_dump -h db.xxxxxxxx.supabase.co -U postgres -d postgres --schema-only > schema.sql
# 特定表
pg_dump -h db.xxxxxxxx.supabase.co -U postgres -d postgres -t users -t posts > tables.sql
3.3 备份选项 #
bash
# 只备份public schema
pg_dump -h host -U user -d db -n public > public.sql
# 排除某些表
pg_dump -h host -U user -d db --exclude-table=logs > backup.sql
# 包含创建数据库语句
pg_dump -h host -U user -d db --create > backup.sql
# 压缩备份
pg_dump -h host -U user -d db -Fc -Z9 > backup.dump.gz
四、恢复数据 #
4.1 使用psql恢复 #
bash
# 恢复SQL文件
psql -h db.xxxxxxxx.supabase.co -U postgres -d postgres < backup.sql
# 恢复到新数据库
psql -h db.xxxxxxxx.supabase.co -U postgres -c "CREATE DATABASE restore_test;"
psql -h db.xxxxxxxx.supabase.co -U postgres -d restore_test < backup.sql
4.2 使用pg_restore #
bash
# 恢复自定义格式备份
pg_restore -h db.xxxxxxxx.supabase.co -U postgres -d postgres backup.dump
# 恢复到新数据库
pg_restore -h db.xxxxxxxx.supabase.co -U postgres -d new_db backup.dump
# 只恢复数据
pg_restore -h host -U user -d db --data-only backup.dump
# 只恢复结构
pg_restore -h host -U user -d db --schema-only backup.dump
4.3 使用CLI恢复 #
bash
# 恢复到本地数据库
supabase db execute -f backup.sql
# 恢复到远程数据库
supabase db execute --linked -f backup.sql
五、Dashboard备份 #
5.1 自动备份恢复 #
text
Dashboard > Database > Backups
恢复步骤
├── 1. 选择备份点
├── 2. 点击Restore
├── 3. 确认恢复
└── 4. 等待完成
5.2 导出数据 #
text
Dashboard > Table Editor
导出选项
├── CSV格式
├── 导出特定表
└── 导出查询结果
六、备份策略 #
6.1 定期备份脚本 #
bash
#!/bin/bash
# backup.sh
DATE=$(date +%Y%m%d_%H%M%S)
BACKUP_DIR="/backups"
PROJECT_REF="your-project-ref"
# 创建备份目录
mkdir -p $BACKUP_DIR
# 执行备份
pg_dump -h db.$PROJECT_REF.supabase.co \
-U postgres \
-d postgres \
-Fc \
> $BACKUP_DIR/backup_$DATE.dump
# 压缩
gzip $BACKUP_DIR/backup_$DATE.dump
# 删除7天前的备份
find $BACKUP_DIR -name "*.dump.gz" -mtime +7 -delete
echo "Backup completed: backup_$DATE.dump.gz"
6.2 自动化备份 #
bash
# 添加到crontab
crontab -e
# 每天凌晨2点备份
0 2 * * * /path/to/backup.sh >> /var/log/backup.log 2>&1
七、数据迁移 #
7.1 从其他数据库迁移 #
bash
# 从MySQL迁移
# 1. 导出MySQL数据
mysqldump -u user -p database > mysql_dump.sql
# 2. 转换为PostgreSQL格式
# 使用工具如 pgloader 或手动转换
# 3. 导入到Supabase
psql -h db.xxxxxxxx.supabase.co -U postgres -d postgres < converted.sql
7.2 从本地迁移到远程 #
bash
# 导出本地数据
pg_dump -h localhost -U postgres -d local_db -Fc > local.dump
# 恢复到远程
pg_restore -h db.xxxxxxxx.supabase.co -U postgres -d postgres local.dump
八、灾难恢复 #
8.1 恢复计划 #
text
灾难恢复步骤
├── 1. 评估损坏程度
├── 2. 停止应用写入
├── 3. 选择恢复点
├── 4. 执行恢复
├── 5. 验证数据完整性
├── 6. 恢复应用服务
└── 7. 记录事故
8.2 表级恢复 #
sql
-- 恢复单个表
-- 1. 重命名损坏的表
ALTER TABLE users RENAME TO users_backup;
-- 2. 从备份恢复表结构
CREATE TABLE users (...);
-- 3. 导入数据
INSERT INTO users SELECT * FROM pg_temp.users_restore;
九、最佳实践 #
9.1 备份建议 #
text
备份最佳实践
├── 定期自动备份
├── 多地存储备份
├── 测试恢复流程
├── 保留多个版本
├── 加密敏感数据
└── 记录备份日志
9.2 恢复测试 #
bash
# 定期测试恢复
# 1. 创建测试数据库
psql -h db.xxxxxxxx.supabase.co -U postgres -c "CREATE DATABASE test_restore;"
# 2. 恢复备份
pg_restore -h db.xxxxxxxx.supabase.co -U postgres -d test_restore backup.dump
# 3. 验证数据
psql -h db.xxxxxxxx.supabase.co -U postgres -d test_restore -c "SELECT COUNT(*) FROM users;"
# 4. 清理
psql -h db.xxxxxxxx.supabase.co -U postgres -c "DROP DATABASE test_restore;"
十、总结 #
备份恢复要点:
| 操作 | 命令 |
|---|---|
| 备份 | pg_dump -Fc > backup.dump |
| 恢复 | pg_restore -d db backup.dump |
| CLI备份 | supabase db dump -f backup.sql |
| CLI恢复 | supabase db execute -f backup.sql |
下一步,让我们学习监控与日志!
最后更新:2026-03-28