Backup your MySQL databases over FTP
Vultr offers automatic backup as an option for any Performance Series VPS. This article provides, in addition to this feature, an option to automatically create daily database backups on the server, and also send them over FTP to your backup server or even file hosts.
Overview
- Automatic backups run daily. The time that the backup occurs is configurable.
- Many file hosts offer file transfer over FTP, so you can automatically send your database to your personal account.
- It sends an email after the backup is finished.
Description
First, create and change to the /backups
directory.
mkdir /backups
cd /backups
Then, using nano
or your favorite editor, create the script file:
nano backupdb.sh
Now, copy and paste this script, and edit the variables at the top according to your settings:
#!/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
After you have finished editing the script and have saved the file, we make the file executable by applying the following command:
chmod +x backupdb.sh
You can test it now by typing in the terminal.
/backups/backupdb.sh
... and after it finishes the execution, type ls -a
to see if the database has been backed up. Also confirm if it was sent to your FTP server.
If everything worked well so far, we can make it run daily by using Crontab.
Crontab Sections
You can edit your crontab with the following command:
crontab -e
This will bring up a text editor where you can input your schedule with each job on a new line.
Each of the sections is separated by a space, with the final section having one or more spaces in it. This is how a cron job is laid out:
minute (0-59), hour (0-23, 0 = midnight), day (1-31), month (1-12), weekday (0-6, 0 = Sunday), command
So, in the editor, type or paste this line:
30 02 * * * /backups/backupdb.sh
The above example will run /backups/backupdb.sh
at 02:30am on every day of every month. Of course, you can change the time as you prefer.
Now just save and close the file. At the time scheduled, the backup will be performed.