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 はソースを別のスキーマに、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 形式のダンプが並列(複数プロセス)でロードされる。大きなデータベースではロードが大幅に高速化する。--clean --if-exists はターゲットのオブジェクトを先に削除し、--no-owner は異なるロール構成に適応させる。プレーン SQL のダンプは 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 ダンプは mysql クライアントにリダイレクトしてロードする(mysql shop < dump.sql)。並列ではるかに高速にロードするには、mydumper で取得したディレクトリを myloader --threads でリストアする。ロード中に 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 ファイル(スキーマ + データ)から新しいデータベースを作成する。

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 の変更を再生し、ダンプを単一の一貫性のある時点に揃える。データベース/コレクション名は --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 で開く →