用于从ftp删除旧文件的Linux shell脚本 [英] Linux shell script for delete old files from ftp

查看:1225
本文介绍了用于从ftp删除旧文件的Linux shell脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

存在一个问题 - 需要将数据库备份存储在FTP上。在FTP上不应该超过10个备份,也就是说,在添加备份到FTP之后应该删除,使得最老的文件总数不能超过10个。
我们如何从ftp执行这样的删除操作?
我想写一个脚本,但不起作用删除:

There is a problem - need to store the database backup on the FTP. On the FTP should not be more than 10 back-ups, ie, After you add backup to FTP should be removed, the oldest files to make the total number of files can not exceed 10 pieces. How can we implement such a removal from the ftp? I'm trying to write a script, but does not work delete:

x=1
ftp -vn $FTP_SERVER<<!
user $FTP_LOGIN $FTP_PASSWORD
binary
put $DUMP_FILE_NAME

for i in `ls -t` do
    if [ $x -le $keep ] then
        ((x++))
        continue
    fi
    delete $i
done

bye
EOF
</i>


推荐答案

这是我写的一个脚本,一个远远超过7天的远程ftp站点。它通过检索目录列表,解析修改日期,然后重新连接删除比ndays更早的任何文件。

This is a script I wrote to remove any files on a remote ftp site older than 7 days. It works by retrieving a listing of the directory, parsing the modified date, and then re-connecting to delete any files older than ndays.

我怀疑,编码到循环中(元素日期)可能会根据系统的设置而改变。 ls命令的返回格式取决于本地系统设置。

I suspect that the numbers hard-coded into the loop (element date) may change depending on the setup of your system. The return formatting of the ls command is dependent on the local system settings.

假设您的备份每天都在运行,那么将ndays设置为10可能会解决您的问题。

Assuming your backups are every day, then setting ndays to 10 might solve your problem.

#!/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

这篇关于用于从ftp删除旧文件的Linux shell脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆