Last active
August 29, 2015 14:01
-
-
Save RadGH/aa44d385f7ac1cdbaa3b to your computer and use it in GitHub Desktop.
Upload a backup file to external FTP. Delete local copy if successful.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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