Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
mysql backup and upload to FTP
#!/bin/bash
############### Infos - Edit them accordingly ########################
DATE=`date +%Y-%m-%d_%H%M`
LOCAL_BACKUP_DIR="/backups"
DB_NAME="database_name"
DB_USER="root"
DB_PASSWORD="root_password"
FTP_SERVER="111.111.111.111"
FTP_USERNAME="ftp-user"
FTP_PASSWORD="ftp-pass"
FTP_UPLOAD_DIR="/upload"
LOG_FILE=/backups/backup-DATE.log
############### Local Backup ########################
mysqldump -u $DB_USER -p$DB_PASSWORD $DB_NAME | gzip > $LOCAL_BACKUP_DIR/$DATE-$DB_NAME.sql.gz
############### UPLOAD to FTP Server ################
ftp -nv $FTP_SERVER << EndFTP
user "$FTP_USERNAME" "$FTP_PASSWORD"
binary
cd $FTP_UPLOAD_DIR
lcd $LOCAL_BACKUP_DIR
put "$DATE-$DB_NAME.sql.gz"
bye
EndFTP
############### Check and save log, also send an email ################
if test $? = 0
then
echo "Database Successfully Uploaded to the Ftp Server!"
echo -e "Database Successfully created and uploaded to the FTP Server!" | mail -s "Backup from $DATE" your_email@email.com
else
echo "Error in database Upload to Ftp Server" > $LOG_FILE
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.