2010年5月8日土曜日

rsyncを(ほぼ)毎日やるのでスクリプトを書いた

最初にYYYYMMDDのディレクトリにフルバックアップする作業は手作業でやる必要がある
#!/usr/bin/perl
use feature qw(say);
use warnings;
use strict;

### INITIAL SETTING ##########
our $BACKUP_DIR="/mnt";
our $EXC_FILE="/mnt/exc.txt";
##############################

if ($BACKUP_DIR!~/\/$/){
$BACKUP_DIR.="/";
}

chdir $BACKUP_DIR or die $!;
my ($today,$target);
$today=`date +%Y%m%d`;
chomp $today;
say "*** START BACKUP!!! ***";
system "mkdir $today 2> /dev/null";


{
#前回バックアップディレクトリの探索
opendir DIR,"./" or die $!;
my @list;
@list = readdir DIR;
close DIR;
@list = grep {-d $_ and !/\./} @list;
$target="";
for (reverse sort @list){
next if (!/\d{8}/);
$target=$_;
last if ($target ne $today);
}
}


my $rsync_cmd = << "EOF";
rsync -av --delete-excluded \\
--exclude-from=$EXC_FILE \\
--link-dest=/mnt/$target/ \\
/ $BACKUP_DIR$today/
EOF

print << "MESSAGE";
Today -> $today
Previous -> $target

__ exec command __
$rsync_cmd

Run rsync OK? (y/n)
MESSAGE

my $input=<STDIN>;
chomp $input;

if ($input eq "y"){
say "rsync run!";
exec $rsync_cmd;

}
else {
say "rsync cancel!"
}

0 件のコメント: