Posted on 2008-08-13 10:02
Prayer 閱讀(635)
評論(0) 編輯 收藏 引用 所屬分類:
Shell
#!/usr/bin/ksh
#! |#
#! Usage: 自動上傳到備份服務器 |#
#!---- 配置參數 -------------------------------|#
PING=/usr/sbin/ping
work_dir="/home/andy" # 程序運行路徑
log_src_dir="/home/andy/" # 日志所在路徑
log_backup_dir="/home/andy/logbak" # 日志備份路徑
put_file_type="0" # 傳文件給對方主機系統的方式:(1:以FTP方式上傳文件到遠程主機;0:拷貝文件到本地目錄,給遠程主機進行FTP)
file_title="shdntc" # 文件名開始6位標識
ftp_hours_ago="12" # 上傳多少小時前的數據
#! 下面配置FTP方式下遠程主機參數
remote_ip="10.1.1.45" # 遠程主機IP地址
remote_user="andy" # 登錄遠程主機用戶名
remote_passwd="andy" # 登錄遠程主機密碼
remote_dir="." # 遠程主機路徑
#! 下面配置LOCAL方式下的本地主機參數
local_path="/home/andy/log" # 本地給遠程主機采集的目錄,需要有讀寫權限
now_time=`date '+%Y%m%d%H'` # 取系統時間
date_str=`date '+%Y%m%d'` #
put_file_lists="" #
#!---- 開始判斷文件并且上傳 -----------------------|#
if [ -d ${log_src_dir} ];then
echo "`date '+%Y-%m-%d %R'` == $log_src_dir exsits, wait..."
else
echo "`date '+%Y-%m-%d %R'` == ${log_src_dir} not exsits!!"
exit 0
fi
cd $log_src_dir
echo "`date '+%Y-%m-%d %R'` == Starting get file lists!"
r_time_str=`expr ${now_time} - ${ftp_hours_ago}`
for file in ${smgid}*
do
echo "$file"
time_str=`echo "$file"|cut -c 10-19`
echo "${time_str} ${r_time_str}"
if [ ${time_str} -le ${r_time_str} ];then
put_file_lists=${put_file_lists}" "${file}
echo "`date '+%Y-%m-%d %R'` == Add filelists [ ${file} ]"
fi
done
if [ -z "${put_file_lists}" ]
then
echo "`date '+%Y-%m-%d %R'` == Get file Failed, no file exsits!\n"
exit 0
else
echo "`date '+%Y-%m-%d %R'` == Complete get put file lists!"
if [ ${put_file_type} = "1" ];then
$PING -c 5 ${remote_ip} >/dev/null
if [ "$?" = "0" ];then
echo "`date '+%Y-%m-%d %R'` == Network test ok! Starting put file..."
(echo "open ${remote_ip}"
echo "user ${remote_user} ${remote_passwd}"
echo "bi"
echo "cd ${remote_dir}"
echo "mput ${put_file_lists}"
echo "ls ${smgid}* ${log_src_dir}/list.dat"
echo "bye") |ftp -in 2>/dev/null
else
echo "`date '+%Y-%m-%d %R'` == Network test error! put file next time!\n"
exit 0
fi
else
if [ -d ${local_path} ];then
echo "`date '+%Y-%m-%d %R'` == ${local_path} exsits! Starting cp file..."
cp ${put_file_lists} ${local_path}
if [ "$?" = "0" ];then
echo "`date '+%Y-%m-%d %R'` == CP file to ${local_path} OK!"
else
echo "`date '+%Y-%m-%d %R'` == Err: CP file to ${local_path} ERROR!"
fi
else
echo "`date '+%Y-%m-%d %R'` == Err: ${local_path} not found! Please check it!"
fi
chmod a+r+w ${local_path}/* 2>/dev/null
for file in ${local_path}/${smgid}*
do
basename ${file} >>${log_src_dir}/list.dat 2>/dev/null
done
fi
fi
if [ -d ${log_backup_dir}/${date_str} ];then
echo "`date '+%Y-%m-%d %R'` == ${log_backup_dir}/${date_str} exsits, wait for backup"
else
mkdir -p ${log_backup_dir}/${date_str}
if [ "$?" != "0" ];then
echo "`date '+%Y-%m-%d %R'` == Create ${log_backup_dir} failed"
exit 0
fi
chmod a+r+w+x ${log_backup_dir}/${date_str}
fi
if [ -f list.dat ];then
mv `cat list.dat` ${log_backup_dir}/${date_str}
compress -f ${log_backup_dir}/${date_str}/* 2>/dev/null
rm list.dat
echo "`date '+%Y-%m-%d %R'` == Deal file complete\n"
原文地址 http://blog.chinaunix.net/u1/35038/showart_286568.html