Rsync

MintheWiki

Jump to: navigation, 찾기


목차

usage

example

rsync --exclude=.* --exclude=*.sh --exclude=*.pl -e /usr/bin/ssh -avzr ~/woori/* root@'192.168.192.226':/var/www/wooriwm


perl expect install

쉘에서 cpan이라고 실행하거나, 펄5.6이하에서는 perl -MCPAN -eshell이라고 하여 cpan 쉘을 띄울 수 있다.</code> 

제일 처음 실행할 때 여러 가지 옵션들의 값을 어떻게 지정할 지 묻는다. 아는 건 답해주고 모르는 건 그냥 Enter를 눌러서 기본값을 사용한다. 

    * urllist 옵션은 펄 모듈이 배포되고 있는 사이트의 주소를 적어주면 되는데, 주인장은 ftp://ftp.daum.net/CPAN/를 사용하고 있음 

모듈을 설치하기 위해서는 install 모듈명 해주면 된다. 그 외 사용법은 help 명령을 내려서 간단하게 볼 수 있고, 자세한 것은 cpan 도움말이나 FAQ[1]새 창으로 열기를 참조 

rsync with perl(nopass)

[SectionName] 
source=/source/directory 
host=source.host.net 
dest=/dest/directory 
user=backupUser 
passwd=123456 
time=5
sub getCfg 
{ 
  my ($cfgfile) = @_; 
  open(FD, "<$cfgfile"); 
  $rSection = ""; 
  $idx= 0; 
  %cfgmap=(); 
  while($line = <FD>) 
  { 
    if($line =~ /\[[a-zA-Z0-9]+\]/) 
    { 
      $line =~ s/\n|\[|\]//g; 
      push(@cfgSection, $cfgValue); 
      $Section = $line; 
      $rSection = $Section; 
      next; 
    } 
    if($line =~ /[a-zA-Z0-9]+=/) 
    { 
      $line =~ s/\s//g; 
      ($key, $value) = split '=', $line; 
      $cfgmap{$Section}{$key} = $value; 
    } 
  } 
  close(FD); 
  return \%cfgmap; 
} 
 
 
 
 
 
 
 
#!/usr/bin/perl 
# rsync 기반 백업 솔류션 
use Getopt::Std; 
use Expect; 
 
$VERSION="1.0"; 
 
$SUC_MESSAGE = "OK "; 
$ERR_MESSAGE = "FAILURE "; 
 
sub getVersion 
{ 
  print "1.0\n"; 
} 
 
############################## 
# MAIN 
############################## 
%opts=(); 
getopts("vhc:",\%opts); 
 
$cfgfile = "backup.cfg"; 
 
if (defined $opts{v}) 
{ 
  getVersion(); 
  exit 0; 
} 
if (defined $opts{c}) 
{  
  $cfgfile = $opts{c}; 
}    
 
# 
# 설정 파일을 읽어들인다.  
# 
my $cfgmap = getCfg($cfgfile); 
 
# rsync를 이용해서 백업을 수행한다. 
for my $section (keys %$cfgmap) 
{ 
 
  rsync($cfgmap{$section}{"host"}, 
      $cfgmap{$section}{"user"}, 
      $cfgmap{$section}{"passwd"}, 
      $cfgmap{$section}{"source"}, 
      $cfgmap{$section}{"dest"}); 
} 
 
################# 
###### subroutine 
################# 
sub rsync 
{ 
  my($host,$user, $passwd, $source, $dest) = @_; 
  #my $exp = Expect->spawn("rsync -avz -e ssh $user\@$host:$source $dest"); 
  my $exp = Expect->spawn("rsync --exclude=.* --exclude=*.sh --exclude=*.pl --exclude=*.cfg -e ssh -avzr $source $user\@$host:$dest");
  my $time = 10; 
 
  # $exp->log_stdout(0); 
  $exp->expect($timeout, 
      [qr 'password: $' => \&passwd, $passwd], 
      [timeout => \&timeouterr],); 
  if ($exp->exitstatus() == 0) 
  { 
    print "BackUp Success\n"; 
  } 
  else 
  { 
    print "BackUp Failure\n"; 
  } 
} 
 
sub passwd 
{ 
  my $lexp = shift; 
  my ($password) = @_; 
  $lexp->send("$password\n"); 
  exp_continue; 
} 
 
sub timeout 
{ 
  print "Time Out\n"; 
} 
 
sub getCfg 
{ 
  my ($cfgfile) = @_; 
  open(FD, "<$cfgfile"); 
  $rSection = ""; 
  $idx= 0; 
  %cfgmap=(); 
  while($line = <FD>) 
  { 
    if($line =~ /\[[a-zA-Z0-9]+\]/) 
    { 
      $line =~ s/\n|\[|\]//g; 
      push(@cfgSection, $cfgValue); 
      $Section = $line; 
      $rSection = $Section; 
      next; 
    } 
    if($line =~ /[a-zA-Z0-9]+=/) 
    { 
      $line =~ s/\s//g; 
      ($key, $value) = split '=', $line; 
      $cfgmap{$Section}{$key} = $value; 
    } 
  } 
  close(FD); 
  return \%cfgmap; 
}
원본 주소 ‘http://wiki.songks.net/Rsync