Linux shell script for delete old files from ftp

后端 未结 5 1582
醉梦人生
醉梦人生 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:37

    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.

    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.

    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 <

提交回复
热议问题