3

i've got a remote FTP server where i store some backups via cronjob.

The Problem is that i only got a little amount of space so im doing incremental backups. I want to keep the backups about 14 days.

I don't have any other access to the server except FTP and i don't now how to delete files older than x days. Every file got a name with a date in it

yxzNamezxy-date-y-m-d.tar.bz2 (datev-20100111.tar.bz2(

Hope to get some help here.

Cheers and thanks for the hopefully coming answeres,

Dennis

/Edit

I'm trying to use the curl thing which is mantioned in an answere with this part of code

curl ftp://$FTP_SERVER --user $FTP_USER:$FTP_PASS --list-only > files.tmp
declare -a aFiles
let iCount=0
exec < files.tmp
while read sLine
do
        aFiles[$iCount]=$sLine
        ((iCount++))
done

echo -e "\n Files: \n\n\n"
echo ${aFiles[@]}

echo -e "\nfor ...\n\n\n"
for sFile in $aFiles
do
        echo -e "\nFile:" $sFile
done

but the secound loop doesn't give me more than 1 filename

Cheers,

Dennis

1
  • See my edited answer for help with the second loop. Jan 14, 2010 at 15:37

3 Answers 3

5

You could send a series of commands to lftp and wrap it in a shell script which calculates the filename of the too old files (see the script below).

TODAY=$(date --iso)                   # Today's date like YYYY-MM-DD
RMDATE=$(date --iso -d '6 days ago')  # TODAY minus X days - too old files
FTPUSER=username
FTPPW=password                        # Better load this from an encrypted file
FTPSERVER=FQDN_or_IP
LFTP=/usr/bin/lftp                    # Path to binary
TMPDIR=$(mktemp -d)                   # Your archives are here

# CAUTION: mput -E deletes local files after upload
echo -n "Uploading files via FTP... "
$LFTP << EOF
open ${FTPUSER}:${FTPPW}@${FTPSERVER}
cd backups/${HOSTNAME}
mkdir ${TODAY}
cd ${TODAY}
mput -E ${TMPDIR}/*
cd ..
rm -rf ${RMDATE}
bye
EOF
echo "Done."

Make sure nobody execpt root can read this script or put the credentials somewhere else. TMPDIR is the directory where the backups reside locally. Of course you need to edit the obivous parts.

HTH,
PEra

4
  • Thank i'll look into it by Tomorrow or so, if it works that would be my prefered answer
    – user25634
    Jan 13, 2010 at 7:39
  • I can send you my script if you like.
    – PEra
    Jan 13, 2010 at 18:54
  • That would be great, Cheers
    – user25634
    Jan 14, 2010 at 8:41
  • That part seems to work quite well. Thanks a lot PEra
    – user25634
    Jan 21, 2010 at 9:20
2

At the high risk of being voted down for posting a late comment on this thread, here's an idea.

If you don't care about having dates on your filenames, and if you keep a small manageable number of backups, you can use ordinal filenames: ie backup.1.tgz, backup.2.tgz, backup.3.tgz, and so on.

Given these names, you can simply rename the previous backups before a new one is uploaded.

  • rename backup.2.tgz backup.3.tgz
  • rename backup.1.tgz backup.2.tgz
  • put backup.1.tgz
1
  • This idea has solved a huge headache - many different clients/installs uploading backups - their install can track what it's uploaded and set the names appropriately so they overwrite the oldest backup automagically. Thanks Julio! Mar 1, 2019 at 14:35
0

You should be able write a script to:

  • Get the list of files using the --list-only option of curl
  • Parse the list for the files you want to delete
  • Use the --quote or --request option to send a rm or DELE command with the selected filenames

Edit:

Your second loop is referring to $aFiles as a scalar. You need to reference it as an array like this:

for sFile in ${aFiles[@]}
do
        echo -e "\nFile:" $sFile
done

Also, you don't need a temp file. You should be able to pipe curl into the while.

1
  • Well yes im still trying to figure out how exactly, im stucking with the part (edited in the original post) it keeps not giving me the right files in the 2nd for loop
    – user25634
    Jan 14, 2010 at 15:23

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .