Chinaunix首页 | 论坛 | 博客
  • 博客访问: 111719
  • 博文数量: 25
  • 博客积分: 1094
  • 博客等级: 少尉
  • 技术积分: 284
  • 用 户 组: 普通用户
  • 注册时间: 2009-05-25 16:36
文章分类

全部博文(25)

文章存档

2011年(14)

2010年(11)

分类: Python/Ruby

2011-05-23 10:42:32


  1. #!/usr/bin/perl -w
  2. use strict;
  3. use Net::FTP;
  4. use Config::Tiny;
  5. use POSIX "strftime";

  6. # 获取系统当前日期
  7. my $data_now = strftime( "%Y-%m-%d", localtime( time() - 86400 * 0 ) );

  8. # 获取前7天日期
  9. my $data5 = strftime( "%Y-%m-%d", localtime( time() - 86400 * 7 ) );

  10. my $Config = Config::Tiny->new();
  11. $Config = Config::Tiny->read('Config.ini');

  12. # Net::Ftp
  13. my $FTP_User = $Config->{Net_FTP}->{FTP_User};
  14. my $FTP_Password = $Config->{Net_FTP}->{FTP_Password};
  15. my $FTP_Ipaddre = $Config->{Net_FTP}->{FTP_Ipaddre};

  16. # 备份列表
  17. my $Backup_List = $Config->{Settings}->{Backup_List};

  18. # Subversion 仓库路径
  19. my $svn_repos = $Config->{Settings}->{Svn_Repos_Path};

  20. # 目标路径
  21. my $backup_dir = $Config->{Settings}->{Backup_Tirget};

  22. # Password
  23. my $Zip_Passwd = $Config->{Settings}->{Zip_Passwd};

  24. # Svn仓库
  25. my @repos = split /,/ => $Backup_List;
  26. foreach (@repos) {
  27.     &start_thread($_);
  28. }

  29. sub start_thread {
  30.     my ($repos_loop) = @_;

  31.     my $next_backup_file = "$repos_loop\_$data_now";

  32.     # 定义backup路径
  33.     my $repos_Path = "$backup_dir/Temp/$repos_loop";

  34.     # 如果目录不存就创建
  35.     system("mkdir -p $repos_Path") if ( !-e "$repos_Path" );

  36.     &LOGINFO( 'INFO',"正在备份 ---> $svn_repos/$repos_loop");

  37.     system("svnadmin hotcopy $svn_repos/$repos_loop $repos_Path");

  38. }

  39. # 存放包路径
  40. mkdir("$backup_dir/Package") if ( !-e "$backup_dir/Package" );

  41. # 设置口令为123456
  42. &LOGINFO( 'INFO',"正在打包 7z a -bd -p$Zip_Passwd $backup_dir/Package/Svn_$data_now.7z $backup_dir/Temp");

  43. system("7z a -bd -p$Zip_Passwd $backup_dir/Package/Svn_$data_now.7z $backup_dir/Temp");

  44. # 删除本地前次备份的7z文件
  45. system("rm -rf $backup_dir/Package/Svn_$data5.7z");

  46. # Delete Temp
  47. system("rm -rf $backup_dir/Temp");

  48. &Net_FTP( "$FTP_User", "$FTP_Password", "$FTP_Ipaddre", "Subversion", "Put" );

  49. #Net_FTP文件上传部分
  50. sub Net_FTP {
  51.     my ( $FTP_User, $FTP_Password, $FTP_Ipaddre, $FTP_Backup, $Action ) = @_;

  52.     my $ftp = Net::FTP->new(
  53.         "$FTP_Ipaddre",
  54.         Debug => 0,
  55.         Timeout => 360
  56.     ) or die &LOGINFO( 'ERROR', "不能连接到FTP服务器,错误原因:" . $@ );

  57.     if ( $ftp->login( $FTP_User, $FTP_Password ) ) {
  58.         &LOGINFO( 'INFO', "Net::FTP 成功连接服务器$FTP_Ipaddre" );
  59.     }
  60.     else {
  61.         &LOGINFO( 'ERROR', "Net::FTP 不能登陆FTP $FTP_Ipaddre服务器,错误原因:" . $@ );
  62.     }

  63.     #切换目录
  64.     $ftp->cwd("/$FTP_Backup"), my $FTP_error = $ftp->message;
  65.     if ( $FTP_error =~ /failed/i ) {
  66.         &LOGINFO( 'INFO', "$FTP_Backup 目录不存在,开始创建目录 $FTP_Backup" );
  67.         $ftp->mkdir($FTP_Backup);
  68.         &LOGINFO( 'INFO', "$FTP_Backup 目录创建完毕,并切换到目录创建目录" );
  69.         $ftp->cwd("/$FTP_Backup");
  70.     }

  71.     #删除FTP上前5天的备份
  72.     &LOGINFO( 'INFO', "删除FTP前5天的备份 /$FTP_Backup/Svn_$data5.7z" );
  73.     $ftp->delete("/$FTP_Backup/Svn_$data5.7z"), my $FTP_Error = $ftp->message;
  74.     &LOGINFO( 'INFO', "/$FTP_Backup/Svn_$data5.7z" );
  75.     &LOGINFO( 'INFO', "$FTP_Error" );

  76.     &LOGINFO( 'INFO', "Net::FTP 使用binary二进制格式上传" );
  77.     $ftp->binary();

  78.     #如果动作等于Put
  79.     if ( $Action eq "Put" ) {
  80.         &LOGINFO( 'INFO', "正在上传 至 $FTP_Backup" );
  81.         $ftp->put("$backup_dir/Package/Svn_$data_now.7z") or die &LOGINFO( 'ERROR', "上传 $backup_dir/Package/Svn_$data_now.7z 失败" );
  82.     }

  83.     &LOGINFO( 'INFO', "File Svn_$data_now.7z 上传 successfully\n" );
  84.     $ftp->quit;

  85. }

  86. sub current_time {
  87.     my $time_now = POSIX::strftime( "[%Y-%m-%d %H:%M:%S]", localtime );
  88.     return $time_now;
  89. }

  90. sub LOGINFO {
  91.     my ( $INFO, $Message ) = @_;
  92.     open FH, ">>logs/$data_now\.txt";
  93.     my $start_time = &current_time();
  94.     print("$start_time $INFO $Message \n");
  95.     print FH ("$start_time $INFO $Message \n");
  96.     close(FH);
  97. }
Config配置文件
  1. [Net_FTP]
    FTP_User=backup
    FTP_Password=110110
    FTP_Ipaddre=192.168.100.24

  2. [Settings]
  3. Backup_List=authcenter,buildtools  #需要备份的列表
  4. Svn_Repos_Path=/data/d_tech        #仓库根路径
  5. Backup_Tirget=/data/Svnback        #备份tirget路径
  6. Zip_Passwd=123456                  #Zip打包时设置的秘密


阅读(1956) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~