0
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

tarコマンドを使ったファイルコピー

Posted at

ファイルサーバの刷新などでデータ移行が発生して、膨大な数のディレクトリやファイルをコピーする機会がある。その時、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

ファイルが移行されていることが確認できた。
以上になります。

0
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?