由于实际工作中遇到客户现场紧急停电,发现linux系统上有脚本丢失情况,客户要求按时备份应用代码,并定时删除旧文件,客户仅提供一个FTP容量可供备份。
之前写过类似FTP文件上传备份脚本,当时无法满足要求 ?
#/bin/bash
#本地目录
localdir="/opt/Oracle/....../logs";
#目标目录
targetdir="/test";
#临时目录
tempdir="/opt/backuptmp";
#把一天内的日志复制到临时目录,1>/dev/null 2>&1不输出错误信息
find $localdir -mtime -1 -name "*" -exec cp -f {} $tempdir \;
#标识成对
ftp -v -n <<EOF
open 10.1.111.111
user ftp用户名 ftp密码
binary
hash
cd $targetdir
lcd $tempdir --切换本地计算机上的目录
prompt --切换提示不显示
mput *
bye
#here document
EOF
echo "commit to ftp successfully"
cd $tempdir
rm -rf $tempdir/*
查看类似帖子,发现有相关示例: ?
#!/bin/bash
# get a list of files and dates from ftp and remove files older than ndays
ftpsite="ftp.yourserver.com"
ftpuser="loginusername"
ftppass="password"
putdir="/public_ftp/admin/logs"
ndays=7
# work out our cutoff date
MM=`date --date="$ndays days ago" +%b`
DD=`date --date="$ndays days ago" +%d`
echo removing files older than $MM $DD
# get directory listing from remote source
listing=`ftp -i -n $ftpsite <<EOMYF
user $ftpuser $ftppass
binary
cd $putdir
ls
quit
EOMYF
lista=($listing)
# loop over our files
for ((FNO=0; FNO<${#lista[@]}; FNO+=9));do
# month (element 5), day (element 6) and filename (element 8)
#echo Date ${lista[`expr $FNO+5`]} ${lista[`expr $FNO+6`]} File: ${lista[`expr $FNO+8`]}
# check the date stamp
if [ ${lista[`expr $FNO+5`]}=$MM ];
then
if [[ ${lista[`expr $FNO+6`]} -lt $DD ]];
then
# Remove this file
echo "Removing ${lista[`expr $FNO+8`]}"
ftp -i -n $ftpsite <<EOMYF2
user $ftpuser $ftppass
binary
cd $putdir
delete ${lista[`expr $FNO+8`]}
quit
EOMYF2
fi
fi
done
进供参考。 ?
|