myback.plの続き
結局こんな風になった。
#!/usr/bin/perl -w # myback.pl # created:2005-03-19 22:57:18+09:00 # # todo:もちっときれいに書く # todo:ログファイルの出力 (一応作れるようにした。) # todo:ディレクトリ問題(ディレクトリの最後の文字が/を含むときにアクセスできない。対応策は不明。) use strict; use Cwd; use File::Copy; my $dir = cwd; my $backdir = "backs"; my $timestamp = &get_timestamp(0); my $backpath = "$dir/$backdir/$timestamp"; my $logpath = $backpath; my $logfilename = 'back.log'; MAIN: { opendir(DH, "$dir"); my @dir_list = readdir(DH); closedir(DH); unless (-e $backdir) { mkdir($backdir); } unless (-e $backpath) { mkdir($backpath); } &open_logfile($logpath, $logfilename); copy_dir($dir, $backpath); close(LOG); exit(0); } ######################################## # subs ######################################## sub copy_dir { my ($from, $to) = @_; opendir(DH, $from); my @dir_list = readdir(DH); closedir(DH); foreach my $dir_and_file (@dir_list) { next if($dir_and_file eq '.' or $dir_and_file eq '..' or $dir_and_file eq "$backdir"); if (-d $dir_and_file) { # ディレクトリだったらもぐる mkdir("$to/$dir_and_file"); chdir("$from/$dir_and_file"); c_dir("$from/$dir_and_file", "$to/$dir_and_file"); chdir("$from"); } else { # ディレクトリではない場合 &File::Copy::copy("$from/$dir_and_file", "$to/$dir_and_file") and print LOG "COPY $from/$dir_and_file\n $to/$dir_and_file\n"; } } } sub open_logfile { my ($path, $logfilename) = @_; if ($path eq '') { $path = &cwd; } open(LOG, ">$path/$logfilename"); } sub get_timestamp { my ($arg) = @_; if ($arg) { return ''; } else { my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst) = localtime(); return sprintf("%04d_%02d_%02d_%02d_%02d_%02d", $year+1900, $mon+1, $mday, $hour, $min, $sec); } } # myback.pl ends here.
自分が使う分には十分。