文章目录
#!/bin/sh
##############################
## 名称: TarCdrAndTransfer.sh
## 描述: 将/bakcdr/cdrbak/VOICE[DATA/SMS]/normal目录下的日期目录Tar成GZ包,mv到 /bakcdr/cdrbak/VOICE/gz_file,
## 然后通过FTP传递到目标主机 152 cdr/cdr用户对应的目录下
## 参数: 暂无
## 作者: Yang.Shangwei
## 日期: 2017-06-17
## 版本:V1.1
## 修改:当天的话单不压缩
## 备注:远程主机的目录目前是提前创建,暂未在脚本中校验创建
## 注意:如果ls获取的文件长度超过系统默认的命令缓冲区最大长度会导致错误。不适合目录下文件特别多的情况.
##############################
#定义退出标识符, 脚本执行后,通过echo $? 查看退出标识符,即上个命令或者脚本的返回结果
EXIT_FAILURE=1 #Failing exit status
EXIT_SUCCESS=0 #Successful exit status
#当前时间
BEGIN_TIME=`date +%s`
#当天,用于判断是否压缩话单
CDR_TODAY=`date "+%Y%m%d"`
#当前执行脚本的全路径
SCRIPT_PATH=$(cd `dirname $0`; pwd)
SCRIPT_NAME=`basename $0`
#日志路径
LOG_LOCATION=/ocs/tools/OperCDR/logs
#话单主机标识
CDR_HOST=`hostname | awk 'BEGIN{FS="_"}{print substr($2,1,4)}'|tr '[a-z]' '[A-Z]'`
DATA_FLAG=_DATA_
VOICE_FLAG=_VOICE_
SMS_FLAG=_SMS_
#存放Tar过的文件目录
STORED_DIR_DATA=/bakcdr/cdrbak/TARGED_MENU/DATA
STORED_DIR_VOICE=/bakcdr/cdrbak/TARGED_MENU/VOICE
STORED_DIR_SMS=/bakcdr/cdrbak/TARGED_MENU/SMS
#话单目录文件
VOICE_NORMAL=/bakcdr/cdrbak/VOICE/normal
VOICE_GZ_FILE=/bakcdr/cdrbak/VOICE/gz_file
DATA_NORMAL=/bakcdr/cdrbak/DATA/normal
DATA_GZ_FILE=/bakcdr/cdrbak/DATA/gz_file
SMS_NORMAL=/bakcdr/cdrbak/SMS/normal
SMS_GZ_FILE=/bakcdr/cdrbak/SMS/gz_file
#远程目标主机IP 用户名 密码
HOST="172.25.243.152"
USER="cdr"
PASSWD="cdr"
#远程目标主机话单目录
REMOTE_DATA_PATH=/backup_storage/bakcdr_ocs/DATA
REMOTE_VOICE_PATH=/backup_storage/bakcdr_ocs/VOICE
REMOTE_SMS_PATH=/backup_storage/bakcdr_ocs/SMS
#判断存放话单的目标目录是否存,不存在则创建目录
Check_TargetMenu(){
if [ ! -d $1 ] ; then
mkdir -p $1
echo $1 does not exists , create successfully >>$LOG_LOCATION/TarCdrAndTransfer.log 2>&1
fi
}
#将每个话单的normal下的日期目录 tar.gz
TarCDRMenu2GZ(){
if [ -d $1 ] ; then
cd $1
for dateMenu in `ls`
do
echo 目录名称 $dateMenu
#判断是否是目录
if [ -d $1/$dateMenu ] ; then
#因凌晨作业,当天的话单不完整,当天目录不进行压缩操作.
if [ $dateMenu -lt $CDR_TODAY ] ; then
#按照特定格式压缩gz
if [[ $1 =~ "DATA" ]] ; then
#tar.gz
tar -zcvf $CDR_HOST$DATA_FLAG$dateMenu.tar.gz $dateMenu
TARED_GZ_FIEL=$CDR_HOST$DATA_FLAG$dateMenu.tar.gz
elif [[ $1 =~ "VOICE" ]] ; then
#tar.gz
tar -zcvf $CDR_HOST$VOICE_FLAG$dateMenu.tar.gz $dateMenu
TARED_GZ_FIEL=$CDR_HOST$VOICE_FLAG$dateMenu.tar.gz
else
#tar.gz
tar -zcvf $CDR_HOST$SMS_FLAG$dateMenu.tar.gz $dateMenu
TARED_GZ_FIEL=$CDR_HOST$SMS_FLAG$dateMenu.tar.gz
fi
#判断上个执行结果是否成功,成功转移目录
if [ $? == 0 ] ; then
echo "$1/$dateMenu.tar.gz tared successfully " >>$LOG_LOCATION/TarCdrAndTransfer.log 2>&1
#将GZ文件传到$2
mv $TARED_GZ_FIEL $2
echo "$TARED_GZ_FIEL moved to $2 successfully " >>$LOG_LOCATION/TarCdrAndTransfer.log 2>&1
#暂时不删除rm $1/$dateMenu -rf
TaredFileMenu=$1/$dateMenu
#检查目录
Check_TargetMenu $STORED_DIR_DATA
Check_TargetMenu $STORED_DIR_VOICE
Check_TargetMenu $STORED_DIR_SMS
#处理文件
DealTaredFileMenu $TaredFileMenu
fi
fi
else
echo -e "\033[31m ERROR $1/$dateMenu is not a directory \033[0m" >>$LOG_LOCATION/TarCdrAndTransfer.log 2>&1
fi
done
else
echo -e "\033[31m ERROR $1 does not exist \033[0m" >>$LOG_LOCATION/TarCdrAndTransfer.log 2>&1
fi
}
#存放tared的目录文件到对应的目录
DealTaredFileMenu(){
if [[ $1 =~ "DATA" ]] ; then
mv $1 $STORED_DIR_DATA
echo "$1 moved to $STORED_DIR_DATA successfully " >>$LOG_LOCATION/TarCdrAndTransfer.log 2>&1
elif [[ $1 =~ "VOICE" ]] ; then
mv $1 $STORED_DIR_VOICE
echo "$1 moved to $STORED_DIR_VOICE successfully " >>$LOG_LOCATION/TarCdrAndTransfer.log 2>&1
else
mv $1 $STORED_DIR_SMS
echo "$1 moved to $STORED_DIR_SMS successfully " >>$LOG_LOCATION/TarCdrAndTransfer.log 2>&1
fi
}
#传递话单
FtpFile(){
echo FtpFile $1 >>$LOG_LOCATION/TarCdrAndTransfer.log 2>&1
for file in `ls $1`
do
#判断是否包含关键字,走不同的方法 (ftp中不识别包含命令,暂时没提取到公共方法中)
if [[ $1 =~ "DATA" ]] ; then
PutDataFileByFtp $1 $file
echo "$1/$file put to remote server $HOST $REMOTE_DATA_PATH successfully " >>$LOG_LOCATION/TarCdrAndTransfer.log 2>&1
elif [[ $1 =~ "VOICE" ]] ; then
PutVoiceFileByFtp $1 $file
echo "$1/$file put to remote server $HOST $REMOTE_VOICE_PATH successfully " >>$LOG_LOCATION/TarCdrAndTransfer.log 2>&1
else
PutSmsFileByFtp $1 $file
echo "$1/$file put to remote server $HOST $REMOTE_SMS_PATH successfully " >>$LOG_LOCATION/TarCdrAndTransfer.log 2>&1
fi
done
}
#put 流量文件
PutDataFileByFtp(){
ftp -i -n $HOST <<EOF
quote USER $USER
quote PASS $PASSWD
cd $REMOTE_DATA_PATH
put $1/$2 $2
quit
EOF
}
#put 语音文件
PutVoiceFileByFtp(){
ftp -i -n -d $HOST <<EOF
quote USER $USER
quote PASS $PASSWD
cd $REMOTE_VOICE_PATH
put $1/$2 $2
quit
EOF
}
#put 短信文件
PutSmsFileByFtp(){
ftp -i -n -d $HOST <<EOF
quote USER $USER
quote PASS $PASSWD
cd $REMOTE_SMS_PATH
put $1/$2 $2
quit
EOF
}
echo "==========================begin `date "+%Y-%m-%d %H:%M:%S"`===============================================" >>$LOG_LOCATION/TarCdrAndTransfer.log 2>&1
#处理流量话单文件
TarCDRMenu2GZ $DATA_NORMAL $DATA_GZ_FILE
#处理语音话单文件
TarCDRMenu2GZ $VOICE_NORMAL $VOICE_GZ_FILE
#处理短信话单文件
TarCDRMenu2GZ $SMS_NORMAL $SMS_GZ_FILE
#传递DATA到远程目标主机
FtpFile $DATA_GZ_FILE
#传递SMS到远程目标主机
FtpFile $SMS_GZ_FILE
#传递VOICE到远程目标主机
FtpFile $VOICE_GZ_FILE
#关于EOF的说明:
#EOF 只是一个标识而已,可以替换成任意的合法字符
#作为结尾的delimiter一定要顶格写,前面不能有任何字符
#作为结尾的delimiter后面也不能有任何的字符(包括空格)
#作为起始的delimiter前后的空格会被省略掉
#结束时间
END_TIME=$(date +%s)
echo "==========================end `date "+%Y-%m-%d %H:%M:%S"`=================================================" >>$LOG_LOCATION/TarCdrAndTransfer.log 2>&1
#计算脚本执行时间
#也可以 通过下面的方式统计 time 脚本名称
#....省略执行过程
#real 0m2.024s
#user 0m0.007s
#sys 0m0.008s
echo "==========================time consuming $(($END_TIME - $BEGIN_TIME)) seconds=================================================" >>$LOG_LOCATION/TarCdrAndTransfer.log 2>&1
#输出一行空行到日志中,方便区分每次执行的日志
echo "" >>$LOG_LOCATION/TarCdrAndTransfer.log 2>&1
exit $EXIT_SUCCESS