Skip to content

Instantly share code, notes, and snippets.

@RadGH
Last active August 29, 2015 14:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save RadGH/aa44d385f7ac1cdbaa3b to your computer and use it in GitHub Desktop.
Save RadGH/aa44d385f7ac1cdbaa3b to your computer and use it in GitHub Desktop.
Upload a backup file to external FTP. Delete local copy if successful.
#!/bin/bash
# Upload a backup file to external FTP. Delete local copy if successful.
# CONFIGURATION ----------
LOCALPATH='/path/to/backup'
HOST=''
USER=''
PASS=''
REMOTEPATH='/remote/storage/path'
# RUN PROGRAM ------------
cd $LOCALPATH
# Determine the file which should be uploaded
FILE=(*.tar)
if [[ -f "$FILE" ]];
then
echo "Backup file found: $FILE"
else
echo "No backups found to migrate, aborting"
exit 0
fi
echo "Backup is starting, please wait..."
# Create a log. Clear the file if it already exists.
FTPLOG="$LOCALPATH/ftplog.txt"
> "$FTPLOG"
# Perform FTP operations
ftp -inv $HOST <<ENDFTP > $FTPLOG
quote USER $USER
quote PASS $PASS
cd $REMOTEPATH
binary
put $FILE
quit
ENDFTP
# Check the FTP Log file for the success status text.
SUCCESSMSG="226 Transfer complete"
if fgrep "$SUCCESSMSG" $FTPLOG ;
then
echo "File uploaded successfully"
rm -f $FILE
echo "Cleaned up local file"
echo "Backup completed successfully."
else
echo "--- FTP OUTPUT START ---"
cat $FTPLOG
echo "--- FTP OUTPUT END ---"
echo "ERROR: The file was not uploaded. Please see the log above."
fi
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment