自从实现FTP上传功能之后就一直未停止过对脚本的改进,但改来改去shell脚本还是有些地方做不过于是把脚本换成perl脚本了,该脚本为避免工作中服务器调整的原因造成忘记更脚本变量,所以就直接去抓取另一个备份数据的脚本里的变量然后分割为FTP服务器上的用户名跟所需要上传至FTP的目录,开始是实现不了判断FTP服务器上是否存在目录问题,目前已解决,先上传抓取切换目录的信息,如果信息里有报错就走创建目录后上传文件语句,如果没有问题就直接上传。上脚本了
#!/usr/bin/perl
my $user="root";
my $password="xx";
my $host="localhost";
my $datapath="/databasedb/setup/data/";
my $file="$datapath/log_everyday.csv";
my $FTP_ADDR="xx 21";
my $FTP_pass="xx";
############################################################################
################获取系统时间 时间格式为xxxx-xx-xx为当系统时间,变量为$date
#本小段脚本参照: http://bbs.chinaunix.net/viewthread.php?tid=821196
############################################################################
sub getTime
{
my $time = shift || time();
my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime($time);
my $user="root";
my $password="xx";
my $host="localhost";
my $datapath="/databasedb/setup/data/";
my $file="$datapath/log_everyday.csv";
my $FTP_ADDR="xx 21";
my $FTP_pass="xx";
############################################################################
################获取系统时间 时间格式为xxxx-xx-xx为当系统时间,变量为$date
#本小段脚本参照: http://bbs.chinaunix.net/viewthread.php?tid=821196
############################################################################
sub getTime
{
my $time = shift || time();
my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime($time);
$year += 1900;
$mon ++;
$mon ++;
$min = '0'.$min if length($min) < 2;
$sec = '0'.$sec if length($sec) < 2;
$mon = '0'.$mon if length($mon) < 2;
$mday = '0'.$mday if length($mday) < 2;
$hour = '0'.$hour if length($hour) < 2;
$sec = '0'.$sec if length($sec) < 2;
$mon = '0'.$mon if length($mon) < 2;
$mday = '0'.$mday if length($mday) < 2;
$hour = '0'.$hour if length($hour) < 2;
my $weekday = ('Sun','Mon','Tue','Wed','Thu','Fri','Sat')[$wday];
return { 'second' => $sec,
'minute' => $min,
'hour' => $hour,
'day' => $mday,
'month' => $mon,
'year' => $year,
'weekNo' => $wday,
'wday' => $weekday,
'yday' => $yday,
'date' => "$year-$mon-$mday"
};
}
my $date=getTime();
my $date=$date->{date};
'minute' => $min,
'hour' => $hour,
'day' => $mday,
'month' => $mon,
'year' => $year,
'weekNo' => $wday,
'wday' => $weekday,
'yday' => $yday,
'date' => "$year-$mon-$mday"
};
}
my $date=getTime();
my $date=$date->{date};
###注由于下面文件需要日期变量放在变量下面引用。。
my $accountfile="$datapath/tbl_sch_account$date.csv";
my $charactorfile="$datapath/tbl_sch_charactor$date.csv";
my $accountfile="$datapath/tbl_sch_account$date.csv";
my $charactorfile="$datapath/tbl_sch_charactor$date.csv";
open(STR,"/databasedb/mcsh") or die "Can't open the file $!";
while($line=<STR>){
$string = $line if$line =~ /str=/;
}
@SP = split(/=/,$string);
@FTP_dir = split(/_/,$SP[1]);
#################################################################################
#
#FTP_dir是取至/databasedb/mcsh的含有str=字符行的=与_之间的数据
#
################################################################################
while($line=<STR>){
$string = $line if$line =~ /str=/;
}
@SP = split(/=/,$string);
@FTP_dir = split(/_/,$SP[1]);
#################################################################################
#
#FTP_dir是取至/databasedb/mcsh的含有str=字符行的=与_之间的数据
#
################################################################################
#####################################################################################################################
#
#substr,0表示从左边开始,-2表示删除最右边两位数,两位数正则往左边,负为右边
#
########################################################################################################################
$FTP_username=substr($FTP_dir[0],0,-2);
if (!$FTP_username){
print "请确认/databasedb/mcsh下=与_符号或str=字符是否存在。。,\n";
exit;
}
elsif($FTP_username =~ /%y%m%d%H%M/){
print "请核对str=字符串问题。。,\n";
exit;
}else{
print "OK...继续下一步!\n";
}
#
#substr,0表示从左边开始,-2表示删除最右边两位数,两位数正则往左边,负为右边
#
########################################################################################################################
$FTP_username=substr($FTP_dir[0],0,-2);
if (!$FTP_username){
print "请确认/databasedb/mcsh下=与_符号或str=字符是否存在。。,\n";
exit;
}
elsif($FTP_username =~ /%y%m%d%H%M/){
print "请核对str=字符串问题。。,\n";
exit;
}else{
print "OK...继续下一步!\n";
}
#*****************
#
#从MYSQL库里提取数据
#
#********************
use DBI;
my $driver="DBI:mysql";
my $log_everyday="select id,onlineMax,onlineAvg,activeIdNum,payIdNum,payIdNumAll,payNum,newAccount,goldUse,addTime from gameLog.log_everyday;";
my $tbl_sch_charactor="select id,guid,name,gender,tg,`to`,classId,exp,expSkill,exPoint from database.tbl_sch_charactor order by id;";
my $tbl_sch_account="select id,last,lastIp from database.tbl_sch_account;";
#
#从MYSQL库里提取数据
#
#********************
use DBI;
my $driver="DBI:mysql";
my $log_everyday="select id,onlineMax,onlineAvg,activeIdNum,payIdNum,payIdNumAll,payNum,newAccount,goldUse,addTime from gameLog.log_everyday;";
my $tbl_sch_charactor="select id,guid,name,gender,tg,`to`,classId,exp,expSkill,exPoint from database.tbl_sch_charactor order by id;";
my $tbl_sch_account="select id,last,lastIp from database.tbl_sch_account;";
my $dbh=DBI->connect("$driver:database=$database;host=$host;user=$user;password=$password") ;
$dbh->do("set names utf8;") or die "Can't set names". dbh->errstr;
my $sth=$dbh->prepare($log_everyday) or die "Can't connect: ". sth->err;
$sth->execute() or die sth->err;
print "*"x20,"\n","提取log_everyday表里的数据,\n","*"x20,"\n";
my($id,$onlineMax,$onlineAvg,$activeIdNum,$payIdNum,$payIdNumAll,$payNum,$newAccount,$goldUse,$addTime);
$sth->bind_columns(\$id,\$onlineMax,\$onlineAvg,\$activeIdNum,\$payIdNum,\$payIdNumAll,\$payNum,\$newAccount,\$goldUse,\$addTime);
open (HEAD,">$file")|| die "Can't open $accountfile:$!\n";
printf HEAD "%s\t%-s\t%-s\t%-s\t%-s\t%-s\t%-s\t%-s\t%-s\t%-s\n","id","onlineMax","onlineAvg","activeIdNum","payIdNum","payIdNumAll","payNum","newAccount","goldUse","addTime";
$dbh->do("set names utf8;") or die "Can't set names". dbh->errstr;
my $sth=$dbh->prepare($log_everyday) or die "Can't connect: ". sth->err;
$sth->execute() or die sth->err;
print "*"x20,"\n","提取log_everyday表里的数据,\n","*"x20,"\n";
my($id,$onlineMax,$onlineAvg,$activeIdNum,$payIdNum,$payIdNumAll,$payNum,$newAccount,$goldUse,$addTime);
$sth->bind_columns(\$id,\$onlineMax,\$onlineAvg,\$activeIdNum,\$payIdNum,\$payIdNumAll,\$payNum,\$newAccount,\$goldUse,\$addTime);
open (HEAD,">$file")|| die "Can't open $accountfile:$!\n";
printf HEAD "%s\t%-s\t%-s\t%-s\t%-s\t%-s\t%-s\t%-s\t%-s\t%-s\n","id","onlineMax","onlineAvg","activeIdNum","payIdNum","payIdNumAll","payNum","newAccount","goldUse","addTime";
while($sth->fetch()){
open (HEAD,">>$file")|| die "Can't open newfile:$!\n";
printf HEAD "%s\t%-s\t%-s\t%-s\t%-s\t%-s\t%-s\t%-s\t%-s\t%-s\n",$id,$onlineMax,$onlineAvg,$activeIdNum,$payIdNum,$payIdNumAll,$payNum,$newAccount,$goldUse,$addTime;
}
open (HEAD,">>$file")|| die "Can't open newfile:$!\n";
printf HEAD "%s\t%-s\t%-s\t%-s\t%-s\t%-s\t%-s\t%-s\t%-s\t%-s\n",$id,$onlineMax,$onlineAvg,$activeIdNum,$payIdNum,$payIdNumAll,$payNum,$newAccount,$goldUse,$addTime;
}
my $sth_account=$dbh->prepare($tbl_sch_account) or die "Can't connect: ". $sth_account->err;
$sth_account->execute() or die $sth_account->err;
print "提取表tbl_sch_account数据。。。\n";
my($id,$last,$lastIP);
$sth_account->bind_columns(\$id,\$last,\$lastIP);
open (HEAD,">$accountfile")|| die "Can't open $accountfile:$!\n";
printf HEAD "%s\t%-s\t%-s\n","id","last","lastIP";
$sth_account->execute() or die $sth_account->err;
print "提取表tbl_sch_account数据。。。\n";
my($id,$last,$lastIP);
$sth_account->bind_columns(\$id,\$last,\$lastIP);
open (HEAD,">$accountfile")|| die "Can't open $accountfile:$!\n";
printf HEAD "%s\t%-s\t%-s\n","id","last","lastIP";
while($sth_account->fetch()){
open (HEAD,">>$accountfile")|| die "Can't open newfile:$!\n";
printf HEAD "%s\t%-s\t%-s\n",$id,$last,$lastIP;
}
open (HEAD,">>$accountfile")|| die "Can't open newfile:$!\n";
printf HEAD "%s\t%-s\t%-s\n",$id,$last,$lastIP;
}
#tbl_sch_charactor
print "提取tbl_sch_charactor表数据\n";
my $tbl_charactor=$dbh->prepare($tbl_sch_charactor) or die "Can't connect: ". $tbl_charactor->errstr ;
$tbl_charactor->execute() or die $tbl_charactor->errstr;
my($id,$guid,$name,$gender,$tg,$to,$classId,$exp,$expSkill,$exPoint);
$tbl_charactor->bind_columns(\$id,\$guid,\$name,\$gender,\$tg,\$to,\$classId,\$exp,\$expSkill,\$exPoint);
open (SELECTOUT,">$charactorfile")|| die "Can't open newfile:$!\n";
printf SELECTOUT "%-s\t%-s\t%-s\t%-s\t%-s\t%-s\t%-s\t%-s\t%-s\t%-s\n","id","guid","name","gender","tg","to","classId","exp","expSkill","exPoint";
my $tbl_charactor=$dbh->prepare($tbl_sch_charactor) or die "Can't connect: ". $tbl_charactor->errstr ;
$tbl_charactor->execute() or die $tbl_charactor->errstr;
my($id,$guid,$name,$gender,$tg,$to,$classId,$exp,$expSkill,$exPoint);
$tbl_charactor->bind_columns(\$id,\$guid,\$name,\$gender,\$tg,\$to,\$classId,\$exp,\$expSkill,\$exPoint);
open (SELECTOUT,">$charactorfile")|| die "Can't open newfile:$!\n";
printf SELECTOUT "%-s\t%-s\t%-s\t%-s\t%-s\t%-s\t%-s\t%-s\t%-s\t%-s\n","id","guid","name","gender","tg","to","classId","exp","expSkill","exPoint";
while($tbl_charactor->fetch()){
open (SELECTOUT,">>$charactorfile")|| die "Can't open newfile:$!\n";
printf SELECTOUT "%-s\t%-s\t%-s\t%-s\t%-s\t%-s\t%-s\t%-s\t%-s\t%-s\n",$id,$guid,$name,$gender,$tg,$to,$classId,$exp,$expSkill,$exPoint;
}
open (SELECTOUT,">>$charactorfile")|| die "Can't open newfile:$!\n";
printf SELECTOUT "%-s\t%-s\t%-s\t%-s\t%-s\t%-s\t%-s\t%-s\t%-s\t%-s\n",$id,$guid,$name,$gender,$tg,$to,$classId,$exp,$expSkill,$exPoint;
}
#####################
##FTP上传
#本小段参考: http://www.diybl.com/course/4_webprogram/cgi/perljs/200862/120244.html
###################################################################################
use Net::FTP;
$ftp=Net::FTP->new("$FTP_ADDR",Timeout=>30) or die "Could not connect.\n";
#登录到FTP
$ftp->login($FTP_username,$FTP_pass) or die "Could not login.\n";
#切换目录
$ftp->cwd("/$FTP_dir[0]") ,$FTP_error=$ftp->message;
if ( $FTP_error =~ /Failed/){
print "FTP返回目录不存在错误信息,开始创建目录$FTP_dir[0]...,\n";
$ftp->mkdir($FTP_dir[0]);
print "$FTP_dir[0]目录创建完毕..并切换到目录创建目录,\n";
$ftp->cwd("/$FTP_dir[0]");
print "开始上传文件$file...\n";
$ftp->put($file) or die "上传$file失败。。。,\n";
$ftp->put($charactorfile) or die "上传$charactorfile失败。。。,\n";
$ftp->put($accountfile) or die "上传$accountfile失败。。。,\n";
$ftp->quit;
}
else
{
$ftp->put($file) or die "上传$file失败。。。,\n";
$ftp->put($charactorfile) or die "上传$charactorfile失败。。。,\n";
$ftp->put($accountfile) or die "上传$accountfile失败。。。,\n";
$ftp->quit;
}
print "上传文件$file,$charactorfile,$accountfile至FTP的$FTP_dir[0]完成。。。,\n";
##FTP上传
#本小段参考: http://www.diybl.com/course/4_webprogram/cgi/perljs/200862/120244.html
###################################################################################
use Net::FTP;
$ftp=Net::FTP->new("$FTP_ADDR",Timeout=>30) or die "Could not connect.\n";
#登录到FTP
$ftp->login($FTP_username,$FTP_pass) or die "Could not login.\n";
#切换目录
$ftp->cwd("/$FTP_dir[0]") ,$FTP_error=$ftp->message;
if ( $FTP_error =~ /Failed/){
print "FTP返回目录不存在错误信息,开始创建目录$FTP_dir[0]...,\n";
$ftp->mkdir($FTP_dir[0]);
print "$FTP_dir[0]目录创建完毕..并切换到目录创建目录,\n";
$ftp->cwd("/$FTP_dir[0]");
print "开始上传文件$file...\n";
$ftp->put($file) or die "上传$file失败。。。,\n";
$ftp->put($charactorfile) or die "上传$charactorfile失败。。。,\n";
$ftp->put($accountfile) or die "上传$accountfile失败。。。,\n";
$ftp->quit;
}
else
{
$ftp->put($file) or die "上传$file失败。。。,\n";
$ftp->put($charactorfile) or die "上传$charactorfile失败。。。,\n";
$ftp->put($accountfile) or die "上传$accountfile失败。。。,\n";
$ftp->quit;
}
print "上传文件$file,$charactorfile,$accountfile至FTP的$FTP_dir[0]完成。。。,\n";
下面是以前的shell脚本
#!/bin/bash
str=`date +%Y-%m-%d`
path="/database/setup/data"
logpath="/var/log/ftpdata.log"
str=`date +%Y-%m-%d`
path="/database/setup/data"
logpath="/var/log/ftpdata.log"
#从/database/mcsh脚本里取相关FTP的用户及目录字段并判断是否正确正确则上传至FTP
dir=` cat /database/mcsh |grep str=|awk -F = '{print $2}'|awk -F _ '{print $1}'`
user=`echo $dir|sed 's/[0-9]\{2\}$//'`
ftppath="/DataCenter/DataCenter/$user/$dir"
dir=` cat /database/mcsh |grep str=|awk -F = '{print $2}'|awk -F _ '{print $1}'`
user=`echo $dir|sed 's/[0-9]\{2\}$//'`
ftppath="/DataCenter/DataCenter/$user/$dir"
if [ ! -f /database/mcsh ]
then
exit
echo "请核对/database/mcsh文件是否存在" >> $logpath 2>&1
fi
then
exit
echo "请核对/database/mcsh文件是否存在" >> $logpath 2>&1
fi
string="`echo $user|grep \.gz`"
if [ -z "$user" ]
then
echo "`date +%Y-%m-%d\ %R` FTP传递参数有误请核对/database/mcsh文件的str里的=与_间隔是否与FTP的用户及目录对应" >> $logpath
elif [ ! -z "$string" ]
then
echo "`date +%Y-%m-%d\ %R` FTP传递参数有误数据中心不存在$ftppath目录请核对/database/mcsh文件的str里的=与_间隔是否与FTP的用户及目录对应" >> $logpath
else
mysql -ppassword -e 'select * from gameLog.log_everyday;' > $path/log_everyday.txt
cd $path
#sed 1d log_everyday.txt > log_everyday.csv
#rm -rf log_everyday.txt
mv log_everyday.txt log_everyday.csv
then
echo "`date +%Y-%m-%d\ %R` FTP传递参数有误请核对/database/mcsh文件的str里的=与_间隔是否与FTP的用户及目录对应" >> $logpath
elif [ ! -z "$string" ]
then
echo "`date +%Y-%m-%d\ %R` FTP传递参数有误数据中心不存在$ftppath目录请核对/database/mcsh文件的str里的=与_间隔是否与FTP的用户及目录对应" >> $logpath
else
mysql -ppassword -e 'select * from gameLog.log_everyday;' > $path/log_everyday.txt
cd $path
#sed 1d log_everyday.txt > log_everyday.csv
#rm -rf log_everyday.txt
mv log_everyday.txt log_everyday.csv
ftp -n <<EOF
open xxx 21
user $user password
binary
prompt off
cd $dir
mput *
bye
EOF
echo "`date +%Y-%m-%d\ %R` 上传至FTP的$ftppath !!!" >> $logpath
fi
open xxx 21
user $user password
binary
prompt off
cd $dir
mput *
bye
EOF
echo "`date +%Y-%m-%d\ %R` 上传至FTP的$ftppath !!!" >> $logpath
fi
本文转自 qwjhq 51CTO博客,原文链接:http://blog.51cto.com/bingdian/278341