用于迁移的一致性导出
Oracle
# Consistent logical export with Data Pump (at a single SCN)
expdp system/password schemas=shop directory=DIR_DMP \
dumpfile=shop_%U.dmp logfile=shop_exp.log \
flashback_scn=<scn> parallel=4
# Current SCN for point-in-time consistency:
# SELECT current_scn FROM v$database;
# Alternative: FLASHBACK_TIME=SYSTIMESTAMPData Pump(expdp)是用于迁移的标准逻辑导出工具。为保证读一致性,传入 FLASHBACK_SCN=<scn>(或 FLASHBACK_TIME),使所有表在同一个 SCN 上一致导出;这在在线系统上至关重要。PARALLEL 可加快速度,%U 生成多个文件。
PostgreSQL
# Single compressed file in custom format (restorable in parallel)
pg_dump -Fc -Z6 -f shop.dump shop
# Directory format (parallel dump + parallel restore):
pg_dump -Fd -j4 -f shop_dir shop
# For the whole cluster + roles/tablespaces:
# pg_dumpall --globals-only > roles.sql迁移时优先使用 pg_dump -Fc(custom)或 -Fd(directory)格式;两者都支持用 pg_restore 进行选择性/并行加载。由于 pg_dump 在单个事务内运行,其输出自动就是一致的快照。角色和表空间等集群级对象需用 pg_dumpall --globals-only 单独导出。
MySQL
# Consistent, lock-free export for InnoDB
mysqldump --single-transaction --quick --routines --triggers --events \
--source-data=2 shop > shop.sql
# Note: older versions use --master-data=2 instead of --source-data.
# Parallel, fast alternative: mydumper -B shop -o ./backup --threads 4在 InnoDB 上,--single-transaction 提供单一一致性快照且不锁表。--source-data=2(原 --master-data=2)会在输出顶部以注释写入启动复制/CDC 所需的 binlog 位置/GTID 信息;这对于零停机切换必不可少。大数据量时,mydumper 可并行运行。
SQL Server
-- Full backup for physical migration (to the same/compatible version):
BACKUP DATABASE shop TO DISK = 'C:\backup\shop.bak' WITH COMPRESSION, CHECKSUM;
-- Portable/version-independent logical export (BACPAC = schema+data):
-- sqlpackage /Action:Export /SourceConnectionString:"..." /TargetFile:shop.bacpac
-- For a single table: bcp shop.dbo.customer out customer.dat -n -S server -T迁移到相同或更新版本的 SQL Server 时,BACKUP ... RESTORE 是最可靠的途径(使用 CHECKSUM 验证完整性)。对于与版本/平台无关的迁移或迁移到 Azure SQL,推荐使用 SqlPackage /Action:Export 将 schema+数据导出为单个 .bacpac 文件。单张表使用 bcp。
MongoDB
# Consistent binary (BSON) export; point-in-time consistency with the oplog
mongodump --uri="mongodb://source:27017" \
--oplog --out=./backup --numParallelCollections=4
# --oplog captures changes made during the dump (requires a replica set);
# it works only with a FULL dump and cannot be combined with --db / --collection.
# On restore, mongorestore --oplogReplay brings it to a single point in time.
# If you need a single database: take it with --db=shop (without the oplog) or use --nsInclude='shop.*' on restore.mongodump 是用于迁移的标准二进制(BSON)导出。在副本集上,--oplog 选项会从 oplog 捕获 dump 运行期间写入的变更,提供一致的时间点(配合 mongorestore --oplogReplay);该选项仅对完整 dump 有效。对于大型集群的集群级迁移,mongosync/在线工具更合适。
Redis
# Create an RDB snapshot in the background (non-blocking)
redis-cli BGSAVE
# Check for completion:
redis-cli INFO persistence | grep rdb_bgsave_in_progress
# Move the resulting file (path: CONFIG GET dir + dbfilename)
# e.g. /var/lib/redis/dump.rdb -> copy to the target server在 Redis 中,一致性导出就是 RDB 快照。BGSAVE 通过 fork 在后台生成一致的 dump.rdb,不阻塞主进程(SAVE 会阻塞,不应使用)。文件路径可通过 CONFIG GET dir 和 dbfilename 获取;将该 dump.rdb 复制到目标服务器的数据目录即可完成迁移。
Elasticsearch
# 1) Register a snapshot repository (shared file system / S3)
PUT _snapshot/migration_repo
{ "type": "fs", "settings": { "location": "/mnt/es_backup" } }
# 2) Take a consistent snapshot
PUT _snapshot/migration_repo/migration_1?wait_for_completion=true
{ "indices": "customer,orders", "include_global_state": false }在 Elasticsearch 中,用于迁移的一致性导出方法是快照。先注册一个存储库(fs、S3、GCS 等),然后执行快照;快照是增量的,捕获的是执行时刻的一致状态。小数据量/单个索引也可以使用 elasticdump,但快照是标准方法。