对rsync进行封装的shell脚本

抓取

#!/bin/bash

. push.sh

# 错误处理:尝试查找备份文件
function onError()
{
local errFile="err"
local serverInfo=($ $ $) # ip username password
local logInfo=($ $) # srcPath destPath local srcPath=${logInfo[]}
logInfo[]="disk1/"${srcPath##*/}".gz" #备份文件路径
logInfo[]=${logInfo[]}".gz" push ${serverInfo[*]} ${logInfo[*]} $errFile if [ -s $errFile ]; then
echo "the file ${logInfo[0]} was not found"
fi
} # 抓取指定日期指定时间的日志文件
function catchLogByHour()
{
local errFile="err"
local serverInfo=($ $ $) # serverInfo=(ip username password)
local logInfo=($ $) # logInfo=(srcPath destPath)
local endHour=$ local i=
while [ $i -le $endHour ];
do
hour=$i
if [ $hour -lt ]; then
hour=""$hour
fi local hourLogInfo
hourLogInfo[]=${logInfo[]}"."$hour
hourLogInfo[]=${logInfo[]}"."$hour push ${serverInfo[*]} ${hourLogInfo[*]} $errFile if [ -s $errFile ]; then
onError ${serverInfo[*]} ${hourLogInfo[*]}
fi i=`expr $[$i+]`
done
} # for test
# logDate=""
# serverInfo=([]="172.18.11.200" []="log" []="wappplog")
# logInfo=([]="logs/syslog/project_log."$logDate []="./data/project_log."$logDate []="")
# catchLogByHour ${serverInfo[*]} ${logInfo[*]}

推送

#!/bin/bash

MAX_RE_CONNECT=
SLEEP_TIME= # 实际推送动作
function pushAction()
{
local ip=$
local user=$
local password=$ local path=$
local localPath=$
local error=$ :>$error # 清空文件相当于rm,touch命令
export RSYNC_PASSWORD="$password"
rsync -Cavz $user@$ip::$path $localPath > $error >& # 判断是否发送错误
grep "rsync error" $error > $error"_copy"
cp $error"_copy" $error
rm -f $error"_copy"
} # 失败重新尝试,一旦超过最大尝试次数认为无法完成
function push()
{ local serverInfo=($ $ $) # ip username password
local logInfo=($ $) # srcPath destPath
local error=$ local i=
while [ $i -lt $MAX_RE_CONNECT ]
do
pushAction ${serverInfo[*]} ${logInfo[*]} $error # no error
if [ ! -s $error ]; then
break
fi echo "try again..."
sleep $SLEEP_TIME
i=`expr $[$i+]`
done
} # for test
# serverInfo=([]="172.18.11.201" []="log" []="wappplog")
# fileInfo=([]="logs/syslog/project_log.20131229.16" []="./lishujun.log" []="aaa")
# push ${serverInfo[*]} ${fileInfo[*]}

main.sh

#! /bin/sh

cd `dirname $`

. catch.sh

if [ $# -eq  ]; then
# 取昨天数据
logDate=`date -d"1 day ago" +'%Y%m%d'`
hour=
else
# 取指定日期时间 today=`date +'%Y%m%d'`
logDate=$
if [ "$1" == "$today" ]; then
# 取当天数据已产生的数据
hour=`date -d "1 hour ago" +"%H"`
else
# 取历史数据
hour=
fi
fi mkdir -p "./logs/project/$logDate"
serverInfo=([]="172.18.10.104" []="stat" []="3gppstat")
logInfo=([]="logs/project/project_log."$logDate []="./logs/project/$logDate/project_log."$logDate []=$hour)
catchLogByHour ${serverInfo[*]} ${logInfo[*]}
上一篇:SQL Server Profiler:使用方法和指标说明


下一篇:python小练习,打出1-100之间的所有偶数,设计一个函数,在桌面上创建10个文件,并以数字命名,复利计算函数