Darcara
Darcara

Reputation: 1618

Delete old backup folders over lftp / ftp

I'm writing a small backup script for my Ubuntu server. The files are tar'ed and zipped locally to a temporary folder, uploaded to the ftp server via lftp and finally deleted locally.

Save files to the server:

FTPSUBDIR=`date --utc +"%Y-%m-%d"`
echo "mkdir -p /daily/${FTPSUBDIR}; mirror --reverse ${TEMPDIR} /daily/${FTPSUBDIR};" | /usr/bin/lftp -u "$FTPUSER,$FTPPASS" "$FTPSERV"

The folder structure on the ftp server:

/
  daily
    2011-10-25
    2011-10-24
    2011-10-23
  weekly
    2011-10-23
    2011-10-16
    2011-10-09

How do I keep only the x newest backups(5 for daily, 4 for weekly) and delete the other folders on the ftp server?

Upvotes: 0

Views: 2150

Answers (3)

morteza khadem
morteza khadem

Reputation: 514

you can use this golang repository for managing ftp backup files: https://github.com/mortezakhademan/deleteOldFiles

This repository has 3 configs in go file:

  1. Hold backup file monthly and remove backup file older than X months
  2. Hold backup file weekly and remove backup file older than X weeks
  3. Hold backup file daily and remove backup file older than X days

Also this repository has a config file for configuring ftp account

Upvotes: 0

Kohjah Breese
Kohjah Breese

Reputation: 4136

This is the simplest way I found to delete old backups:

HOST='ftpback.net'
USER='username'
PASSWD='pass'
FILE="file.tar.gz"
DELPREFIX=$( date -d 'now -2 month' +'%y-%m' )

ftp -n $HOST <<END_SCRIPT
quote USER $USER
quote PASS $PASSWD
cd mysql
put $FILE
mdel "$DELPREFIX*"
y
quit
END_SCRIPT
exit 0

The DELPREFIX in this case equals the month before last, so if it is November, the date will be September, e.g. '15-09'.

When in FTP, it will then delete any files beginning with DELPREFIX, e.g. '15-09'. This doesn't give you the ability to delete files to the day, but if you are happy to have variable amounts of backups kept, its is easier than other scripting solutions.

You may need to tweak this a little for your files names.

Upvotes: 1

Gilbert
Gilbert

Reputation: 3776

With just ftp operations on the remote system you would need to be more proactive on the ftp client side.

Non-debugged code fragments follow... you will have to flesh out and debug.

# print results of directory list to standard out
ftp_dir ()
{
  typeset dir="$1";
  ftp <<'FTP'
login
connection and
cd
directory commands
FTP
}

# read delete commands (or others) from stdin using inline login
ftp_delete()
{
   cat <<FTP - | ftp
send FTP login and delete commands
FTP
}

do_delete ()
{
   typeset dir="$1";
   typeset cnt="$2";
   if [ ${#names} -gt $cnt ]; then
          typeset a_end=$(( ${#names} - 8 ));
          ( typeset n=0;
            while [ $n -lt a_end ]; do
                echo "delete $dir/${names[$n]}";
                 n=$(( $n + 1 ));
             done; ) | ftp_delete
      fi
}

names=( $( ftp_dir weekly | sort ) );  #get all entries
do_delete dir 4

If I was more awake I might come up with a better answer.

Upvotes: 2

Related Questions