ファイルサーバの刷新などでデータ移行が発生して、膨大な数のディレクトリやファイルをコピーする機会がある。その時、cpコマンドやrsyncコマンドで使用してうまくコピーできないことがある。
ここで、このtarコマンドを使用したディレクトリおよびファイルコピーの手法が
過去に難を乗り切ったことが少なくとも数回はあったため、お役に立てればと思い紹介したい。
###■ディレクトリ構成###
旧サーバー : /data1 ※元データがあるところ
新サーバー : /stroge1 ※移行先
###■ファイルの確認(元データ)###
コマンド操作はrootで新サーバから実行する。
新サーバから旧サーバの/data1には事前にNFSなどでマウントしておく。
※ログイン認証情報は新旧のサーバで同一ものをしていると仮定。
※NFSなどでマウント時に、新サーバのrootでアクセスが出来るようにしておく。
####(1) ディレクトリの移動
元データのあるディレクトリdata1に移動する。
# cd /data1
####(2) ファイルの確認
treeコマンドでファイル・ディレクトリ構造を確認する。
# tree -F
.
├── subdir/
│ └── test03.txt
├── test01.txt
└── test02.txt
lsコマンドでファイルの所有権アクセス権を確認する。
# ls -l
drwxr-xr-x 2 root root 4096 11月 13 02:52 subdir
-rw-r--r-- 1 ishimatsu ishimatsu 7 11月 13 02:50 test01.txt
-rw-r--r-- 1 ishimatsu ishimatsu 7 11月 13 02:51 test02.txt
# ls -l subdir/
-rw-r--r-- 1 ishimatsu ishimatsu 7 11月 13 02:52 test03.txt
###■ファイルコピーの実行###
# tar cvf - . |(cd /storage1;tar xvf -)
./
./subdir/
./subdir/test03.txt
./test01.txt
./test02.txt
./
./subdir/
./subdir/test03.txt
./test01.txt
./test02.txt
コピーが完了。
###■ファイルの確認(移行先)
####(1) ディレクトリの移動
移行したデータのあるディレクトリstorage1に移動。
# cd /storage1
####(2) ファイルの確認
treeコマンドでファイル・ディレクトリ構造を確認する。
# tree -F
.
├── subdir/
│ └── test03.txt
├── test01.txt
└── test02.txt
lsコマンドでファイルの所有権アクセス権を確認する。
# ls -l
drwxr-xr-x 2 root root 4096 11月 13 02:52 subdir
-rw-r--r-- 1 ishimatsu ishimatsu 7 11月 13 02:50 test01.txt
-rw-r--r-- 1 ishimatsu ishimatsu 7 11月 13 02:51 test02.txt
ls -l subdir/
-rw-r--r-- 1 ishimatsu ishimatsu 7 11月 13 02:52 test03.txt
ファイルが移行されていることが確認できた。
以上になります。