DBAtlas · 数据库运维图谱

导入目标库

Oracle

# Import with Data Pump; remap schema and tablespace
impdp system/password directory=DIR_DMP dumpfile=shop_%U.dmp \
  logfile=shop_imp.log \
  remap_schema=shop:shop_new \
  remap_tablespace=USERS:DATA parallel=4

# For existing objects: TABLE_EXISTS_ACTION=TRUNCATE|APPEND|REPLACE

impdp 将 expdp 导出的 .dmp 文件加载到目标库。REMAP_SCHEMA 将源迁移到不同的 schema,REMAP_TABLESPACE 迁移到不同的表空间;跨环境迁移时经常使用。对已存在表的处理方式通过 TABLE_EXISTS_ACTION 设置,速度通过 PARALLEL 设置。

PostgreSQL

# Parallel restore (from custom/directory format)
pg_restore -j4 -d targetdb --no-owner --clean --if-exists shop.dump

# A specific table only: pg_restore -d targetdb -t customer shop.dump
# For a plain .sql dump:  psql -d targetdb -f shop.sql

使用 pg_restore -j 可并行(多进程)加载 custom/directory 格式的 dump;对大型数据库可显著加快加载速度。--clean --if-exists 会先删除目标中的对象,--no-owner 可适配不同的角色结构。纯 SQL dump 使用 psql -f 加载(不支持并行)。

MySQL

# Load a plain SQL dump
mysql --binary-mode shop < shop.sql

# Parallel/fast load (if taken with mydumper):
myloader --directory=./backup --database=shop --threads=4 --overwrite-tables

# Tip: SET FOREIGN_KEY_CHECKS=0; unique_checks=0 before loading speeds it up.

mysqldump 的 SQL dump 通过重定向给 mysql 客户端加载(mysql shop < dump.sql)。若需并行且更快的加载,用 myloader --threads 恢复 mydumper 导出的目录。加载期间禁用 FOREIGN_KEY_CHECKS 和 UNIQUE_CHECKS 可提升性能。

SQL Server

-- Restore from a physical backup (move file paths to the new environment)
RESTORE DATABASE shop FROM DISK = 'C:\backup\shop.bak'
WITH MOVE 'shop_data' TO 'D:\data\shop.mdf',
     MOVE 'shop_log'  TO 'D:\log\shop.ldf', REPLACE;

-- Import from a BACPAC (schema+data):
-- sqlpackage /Action:Import /TargetConnectionString:"..." /SourceFile:shop.bacpac

.bak 备份通过 RESTORE DATABASE 加载;迁移到不同的磁盘布局时,WITH MOVE 将数据/日志文件重定向到新路径。对于与版本无关的/Azure SQL 目标,SqlPackage /Action:Import 会将 .bacpac 文件(schema + 数据)创建为新数据库。

MongoDB

# Restore the BSON backup; replay the oplog to land on a single point in time
mongorestore --uri="mongodb://target:27017" --oplogReplay \
  --numParallelCollections=4 ./backup

# Renaming: --nsFrom='shop.*' --nsTo='shop_new.*'
# Drop existing data before loading: --drop

mongorestore 将 mongodump 的 BSON 输出加载到目标。--oplogReplay 会重放 --oplog 备份中的 oplog 变更,使 dump 达到单一一致的时间点。数据库/集合名称可通过 --nsFrom/--nsTo 更改,现有数据可通过 --drop 清除。

Redis

# Stop the server, place dump.rdb in the data directory, start it again
# 1) systemctl stop redis  (or SHUTDOWN NOSAVE)
# 2) cp dump.rdb  <path found with CONFIG GET dir>/dump.rdb
# 3) systemctl start redis   -> Redis loads the RDB into memory at startup

# Alternative (live): feed a RESP command stream with redis-cli --pipe

Redis 只在启动时加载 RDB。因此需停止目标服务器,将迁移来的 dump.rdb 放入数据目录(CONFIG GET dir),然后重启服务器;启动时会从 RDB 填充内存。若要在不停止服务器的情况下加载,用 redis-cli --pipe 以 RESP 格式流式写入键。

Elasticsearch

# Register the same repository on the target cluster, then restore the snapshot
PUT _snapshot/migration_repo
{ "type": "fs", "settings": { "location": "/mnt/es_backup" } }

POST _snapshot/migration_repo/migration_1/_restore
{ "indices": "customer,orders",
  "rename_pattern": "(.+)", "rename_replacement": "restored_$1" }

在目标集群上注册与源相同的快照仓库(文件必须可访问),然后通过 _snapshot/.../_restore 恢复索引。索引名称可通过 rename_pattern/rename_replacement 更改。已存在的索引在打开状态下无法恢复;需先关闭或重命名。

在 DBAtlas 中打开 →