Websegment.net

If you put your mind to it, you can accomplish anything.

コンテンツデータ移行時に使うスクリプト

without comments

サーバ移行時に若干困ったので、ついでにスクリプトを組んだので、更についでにブログに書いてみる。

今回はPerlで書いてみた。

#!/usr/bin/perl
use strict;
my @chown;
my @chmod;

#ディレクトリの指定
my $directory = $ARGV[0];
my @filelist = `find $directory -ls`;
foreach my $file(@filelist){
	$file =~s/\s+/\t/g;
	my @info = split(/\t/, $file);
	#ファイルのステータスを取得
	my @status = stat $info[10];
	#パーミッションを8進数に変換
	my $permission = substr((sprintf "%03o", $status[2]), -3);

	my $chownCommand = sprintf("chown %s:%s %s\n",
		$info[4], $info[5], $info[10]);
	my $chmodCommand = sprintf("chmod %s %s\n",
		$permission, $info[10]);

	push(@chown, $chownCommand);
	push(@chmod, $chmodCommand);
}
foreach my $command(@chown){
	print $command;
}
foreach my $command(@chmod){
	print $command;
}

tarコマンドで固めて移行先サーバにアップすればいいやーんと思われるかもしれないけど、そういう状況でない場合もあるのでw

使い方

linux限定にはなるけど、上記perlスクリプトがかかれたplファイルをどこでもいいので配置して、下記コマンドを実行。

 [user@localhost] $ ./save_fileinfo.pl /var/www/html
 

ダウンロード出来るようにGitHubにもアップしておきました。
https://github.com/websegment/filelist

Written by Daisuke Sakata

12月 23rd, 2011 at 2:08 am

Posted in Server

Tagged with ,