#!/bin/sh # vim: autoindent ts=4 cindent expandtab # ============================================================================= # # ftp-push-backup.sh (2005-06-22) # # $Id: ftp-push-backup.sh.txt,v 1.1 2005/06/22 12:45:58 xaphod Exp $ # # - A script to push a file to an FTP server with support for # multiple, configurable, generations. # # LICENCE: http://creativecommons.org/licenses/by-nc/2.5/ # # ============================================================================= # Backup script # - if it's not there execution will continue # - it it is there it will be run in this context # - abort if errorlevel is non-zero after it's run SCRIPT=/tmp/dothebackup.sh # Source Filename # - the single file to backup # - can be created by BACKUP above # - abort if not there or size is zero SOURCE=/tmp/thebackup.tgz # Target Filename # - how the file is to be named after FTP upload # - most recent upload is called "BASENAME00.EXTENSION" # - oldest file is called "BASENAMExx.EXTENSION", # where 'xx' is zero padded (VERSIONS - 1) VERSIONS=5 BASENAME='backup' EXTENSION='tgz' # FTP Details # - Must be verified prior to use! HOST="192.168.254.254" USER="us3rn4m3" PASS="p4ssw0rd" ## ## DO NOT edit past here (Unless you really do know what you're doing) ## ============================================================================ ## FTPPROG='ftp' FTPARGS='-V -n' PATH=/bin:/usr/bin ## If there is a SCRIPT, run it if [ -f $SCRIPT ]; then . $SCRIPT if [ $? -ne 0 ]; then echo "$0:" echo " Child script returned non-zero, ABORT!" echo " SCRIPT: $SCRIPT" exit 1 fi else fi # Check the backup file exists if [ ! -s $SOURCE ]; then echo "$0:" echo " Source file does not exist or has no mass, ABORT!" echo " SOURCE: $SOURCE" exit 1 fi ## Create the empty temp script tempfoo=$( basename $0 ) TMPSOURCE=$( mktemp -q /tmp/$tempfoo-$$.XXXXXX ) if [ $? -ne 0 ]; then echo "$0:" echo " Can't create temp script, ABORT!" exit 1 fi ## Populate it echo "$FTPPROG $FTPARGS $HOST <&1 > /dev/null" >> $TMPSOURCE echo "quote USER $USER" >> $TMPSOURCE echo "quote PASS $PASS" >> $TMPSOURCE echo "binary" >> $TMPSOURCE FN0="" FN1="" COUNT=$( expr \( $VERSIONS \) - 1 ) while [ $COUNT -ge 0 ]; do FN0=$FN1 FN1=$( printf "$BASENAME%02s.tgz" $COUNT) if [ "$FN0" = "" ]; then echo delete $FN1 >> $TMPSOURCE elif [ $COUNT -ge 0 ]; then echo rename $FN1 $FN0 >> $TMPSOURCE fi if [ $COUNT -eq 0 ]; then echo put $SOURCE $FN1 >> $TMPSOURCE fi COUNT=$( expr \( $COUNT \) - 1 ) done echo "END_SCRIPT" >> $TMPSOURCE ## Run the generated script #cat $TMPSOURCE . $TMPSOURCE if [ $? -ne 0 ]; then echo "$0:" echo " Temp script returned non-zero, ABORT!" echo " TMPSOURCE: $TMPSOURCE" exit 1 fi ## Delete the generated script rm -f $TMPSOURCE ## That's all Folks! exit 0