Posted on 2008-08-13 10:02
Prayer 阅读(631)
评论(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