首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 數據庫知識 >  >> MySQL 資料庫 >> MySQL 綜合教學

主流數據庫備份恢復命令速查手冊:MySQL/SQL Server/Oracle/Redis/Mongo/PostgreSQL

     編輯:MySQL 綜合教學

主流數據庫備份恢復命令速查手冊

整理六大主流數據庫最常用的備份/恢復命令,生產環境請參考官方文檔調整參數。

1. MySQL / MariaDB

# 參數說明
$user $password $host $port $charset $dbname $table $file

# 備份(單庫/全庫/單表)
mysqldump --single-transaction -l -u$user -p$password -h$host -P$port \
  --default-character-set=$charset $dbname [$table] > $file

# 恢復
mysql -u$user -p$password -h$host -P$port \
  --default-character-set=$charset $dbname < $file

2. SQL Server (sqlcmd/osql)

# 參數
$user $password $instance $dbname $full_file $diff_file

# 全量備份
sqlcmd -U $user -P "$password" -S $instance -Q \
"BACKUP DATABASE $dbname TO DISK='$full_file' WITH INIT, FORMAT"

# 全量恢復
sqlcmd -U $user -P "$password" -S $instance -Q \
"RESTORE DATABASE $dbname FROM DISK='$full_file' WITH REPLACE, RECOVERY"

# 差異備份/恢復同理(WITH DIFFERENTIAL / NORECOVERY + RECOVERY)

3. Oracle

生產環境強烈建議使用 RMAN (Recovery Manager),功能最全、性能最優,支持增量、壓縮、加密、目錄管理等高級特性。此處不展開命令行示例。

4. Redis

# 備份:觸發生成 RDB 快照文件
redis-cli -h $host -p $port -a $password SAVE
# 或 BGSAVE 異步備份

# 恢復:將 dump.rdb 放回配置的 dir 目錄,重啟 Redis 即可

5. MongoDB

# 參數
$host $port $dbname $backup_dir $user $password $table $restore_dir

# 備份(全庫/單表)
mongodump --host $host:$port -d $dbname -o $backup_dir -u $user -p $password [-c $table]

# 恢復
mongorestore --host $host:$port -d $dbname -u $user -p $password \
  [-c $table] $restore_dir

6. PostgreSQL

# 參數
$host $port $user $dbname $backup_file $restore_file

# 備份
pg_dump -c -b -h $host -p $port -U "$user" $dbname > $backup_file

# 恢復
psql -h $host -p $port -U "$user" -d $dbname -f $restore_file

7. 通用最佳實踐提醒

  • 備份後務必 驗證可恢復性(定期演練)
  • 備份文件 異地存儲,防單點災難
  • 生產庫建議 主從架構 + 從庫備份,避免主庫鎖表影響業務
  • 關鍵數據庫啟用 binlog/oplog/WAL 支持時間點恢復 (PITR)
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved