Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Linux shell script for delete old files from ftp

Tags:

shell

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>
like image 366
user1482144 Avatar asked Jun 26 '12 09:06

user1482144


People also ask

How do I delete a file from an FTP server in Linux?

To delete files on the FTP server use the delete command. To delete several files at once, use the mdelete command. You will be asked to provide a “y” or “n” confirmation for the deletion of each file. Here our FTP user has listed the files to see their names and then chosen one to delete.

How do I delete multiple files using FTP?

Type this command: FTP> mdelete [directory] *.

Can you delete files via FTP?

When you are managing your files on your server, you may need to delete files off the server. You can delete files several different ways from your server. You can connect with an FTP program like FileZilla and delete files, or you can Use your cPanel File Manager.


2 Answers

This one is dealing with sftp and has proper date check as the script from @Graeme is only working within one month:

#!/bin/bash
# get a list of files and dates from ftp and remove files older than ndays
ftpsite="sftp -b-  -oIdentityFile=<KEYFILE> -oPort=<PORT>  <USER>@<HOST>"
putdir="/data"

ndays=19

# work out our cutoff date
MM=`date --date="$ndays days ago" +%b`
DD=`date --date="$ndays days ago" +%d`
TT=`date --date="$ndays days ago" +%s`

echo removing files older than $MM $DD

# get directory listing from remote source
echo "
cd $putdir
ls -l
"|$ftpsite >dirlist

# skip first three and last line, ftp command echo
listing="`tail -n+4 dirlist|head -n-1`"

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`]}

  fdate="${lista[`expr $FNO+5`]} ${lista[`expr $FNO+6`]} ${lista[`expr $FNO+7`]}"
  sdate=`date --date="$fdate" +%s`
  # check the date stamp
  if [ $sdate -lt $TT ]
  then
      # Remove this file
      echo "$MM $DD: Removing  ${lista[`expr $FNO+5`]} /  ${lista[`expr $FNO+6`]} / ${lista[`expr $FNO+8`]}"
      $ftpsite <<EOMYF2
      cd $putdir
      delete ${lista[`expr $FNO+8`]}
      quit
EOMYF2

  fi
done
like image 145
Axel Amthor Avatar answered Sep 16 '22 15:09

Axel Amthor


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 <<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
like image 32
Graeme Avatar answered Sep 18 '22 15:09

Graeme