Linux shell script for delete old files from ftp

后端 未结 5 1579
醉梦人生
醉梦人生 2021-01-02 14:21

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 old

5条回答
  •  北海茫月
    2021-01-02 14:33

    Here's a shorter solution using lftp, that'll also work with subdirectories:

    #!/bin/bash
    # get a list of files and dates from ftp and remove files older than ndays
    ftpsite="ftpback-xxx.ovh.net"
    ftpuser="user"
    ftppass="pass"
    #remote folder in which you want to delete files
    putdir="/"
    nullfolder="/tmp/null"
    
    ndays=19
    
    mkdir -p nullfolder
    MM=`date --date="$ndays days ago" +%b`
    DD=`date --date="$ndays days ago" +%d`
    
    echo removing files older than $MM $DD
    #The no-ssl is there because it's on a local network and remote doesn't support ftp
    lftp -e "set ftp:ssl-allow no; mirror $putdir $nullfolder --older-than=now-${ndays}days --Remove-source-files; exit" -u $ftpuser,$ftppass $ftpsite
    
    rm $nullfolder/* -Rf
    

    It has two drawbacks though:

    • It will first download those old files (before removing them later), so it uses some more bandwidth
    • Those old files will take some space on the local disk before being removed. You can use nullfs to pally to that

提交回复
热议问题