Chinaunix首页 | 论坛 | 博客
  • 博客访问: 484858
  • 博文数量: 279
  • 博客积分: 4467
  • 博客等级: 上校
  • 技术积分: 2830
  • 用 户 组: 普通用户
  • 注册时间: 2007-04-03 14:43
文章分类

全部博文(279)

文章存档

2013年(1)

2012年(39)

2011年(35)

2009年(29)

2008年(131)

2007年(44)

分类:

2008-04-22 10:40:08

#!/use/bin/perl

use Net::FTP;
use strict;
use warnings;
use LWP::UserAgent;

my $local_dir = 'E:\Blog_Backup';
my $login_url = '';
my $backup_url = '';

my $user = "fake_user";
my $pass = "fake_pass";
my $ftp_user = "yunshu";
my $ftp_pass = "yunshu_password";

my $ie = LWP::UserAgent->new( );
$ie->cookie_jar( {} );

sub Login
{
    my $response = $ie->post( $login_url, [ 'action' => 'login', 'username' => $user, 'password' => $pass ] );
    if ( not $response->is_success )
    {
        die "login error: ", $response->status_line. "\n";
    }
    
    if( $response->content =~ /登陆成功/ )
    {
        print "login successful!\n";
    }
    else
    {
        die "login error: wrong password!";
    }
}

sub MakeBackFileName
{
    my ($sec, $min, $hour, $day, $mon, $year, $wday, $yday, $isdst) = localtime( );
    
    $year = $year + 1900;
    $mon = $mon + 1;
    if( length($mon) == 1 )
    {
        $mon = "0$mon";
    }
    
    return "Icy$year$mon$day.sql";
}

sub BackUp
{
    my $backup_file = shift;
    
    my $back_data = "table%5B%5D=icy_adminlog&table%5B%5D=icy_attachment&table%5B%5D=icy_blog&table%5B%5D=icy_comment&table%5B%5D=icy_info&table%5B%5D=icy_link&table%5B%5D=icy_loginlog&table%5B%5D=icy_setting&table%5B%5D=icy_sort&table%5B%5D=icy_user&table%5B%5D=yunshu_adminlog&table%5B%5D=yunshu_attachment&table%5B%5D=yunshu_blog&table%5B%5D=yunshu_comment&table%5B%5D=yunshu_info&table%5B%5D=yunshu_link&table%5B%5D=yunshu_loginlog&table%5B%5D=yunshu_setting&table%5B%5D=yunshu_sort&table%5B%5D=yunshu_user&table%5B%5D=yunshugb_guestbook&path=.%2Fbackupdata%2F" .$backup_file. "&action=dobackup&submit=+%CC%E1%BD%BB";

    my $response = $ie->post( $backup_url, Content => $back_data );
    if ( not $response->is_success )
    {
        die "backup database error: ", $response->status_line. "\n";
    }
    
    if( $response->content =~ /数据库已备份/ )
    {
        print "backup successful!\n";
    }
    else
    {
        die "backup database error!";
    }
}

sub DownLoadFromFtp
{
    my $back_file = shift;
    
    my $ftp = Net::FTP -> new( "", debug=>0 ) || die "connect to ftp server error: $@";
    
    $ftp->login( $ftp_user, $ftp_pass ) || die "login to ftp server error: ", $ftp->message;
        
    $ftp->cwd( 'web/yunshu/admin/backupdata/' ) || die "can't change working directory: ", $ftp->message;
        
    print "donwload backup file from ftp server.....\n";
    
    $ftp->get( $back_file,  "$local_dir\$back_file") || die "get $back_file failed: ", $ftp->message;
    
    $ftp->delete( $back_file ) || die "delete $back_file failed: ", $ftp->message;
    
    $ftp->quit( );
    
     print "all done!\n";
}

my $backup_file = MakeBackFileName( );

print "Will back database as: $backup_file\n";

Login( );

BackUp( $backup_file );

DownLoadFromFtp( $backup_file );


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