歡迎您光臨本站 註冊首頁

linux ftp perl上傳腳本

←手機掃碼閱讀     火星人 @ 2014-03-09 , reply:0
自從實現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); $year = 1900;
$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; 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}; ###注由於下面文件需要日期變數放在變數下面引用..
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=字元行的=與_之間的數據
#
################################################################################ #####################################################################################################################
#
#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;"; 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 "%st%-st%-st%-st%-st%-st%-st%-st%-st%-sn","id","onlineMax","onlineAvg","activeIdNum","payIdNum","payIdNumAll","payNum","newAccount","goldUse","addTime"; while($sth->fetch()){
open (HEAD,">>$file")|| die "Can't open newfile:$!n";
printf HEAD "%st%-st%-st%-st%-st%-st%-st%-st%-st%-sn",$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 "%st%-st%-sn","id","last","lastIP"; while($sth_account->fetch()){
open (HEAD,">>$accountfile")|| die "Can't open newfile:$!n";
printf HEAD "%st%-st%-sn",$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 "%-st%-st%-st%-st%-st%-st%-st%-st%-st%-sn","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 "%-st%-st%-st%-st%-st%-st%-st%-st%-st%-sn",$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"; 下面是以前的shell腳本 #!/bin/bash
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" if [ ! -f /database/mcsh ]
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 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


[火星人 ] linux ftp perl上傳腳本已經有516次圍觀

http://coctec.com/docs/linux/show-post-51980.html