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?
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.