Since you host your site on dreamhost, a new problem will happen,that is how to backup your database automatically?
Knowledge You Should Know
To aim to this, you should be familiar with some knowledge:
because WordPress is based on MySQL, and DreamHost also use it.
Since this task will run daily, and do it automatically, Linux service is required.
Since Dreamhost doesn’t provide the auto-backup service, but Shell function , we can make use of it so that the task can be implemented.
The database backup package should be sent the email address you specify, so we should email it, Mutt makes sense for it.
- Package Compression and Extraction
Compress the packge
Ok,Let’s take a look at the script and explain how to implement it.
The SHELL Script
The scripts block is like below:
#!/bin/bash
# Backup your MySQL database and have it mailed to you
# requires mutt or another command line email client
# config
tmp=”/backup_db”
db_user=”user”
db_pw=”db-pws”
db_name=database_name
db_host=”dbhost”
recp=Reciept mail
today=`date +%Y%m%d`
mysqldump_opts=”–add-drop-table -acQq”
# email settings
subject=”DB Backup for WordPress”
sql_file=$tmp/$db_name.$today.sql
gzip_file=$tmp/$db_name.$today.sql.gz
# create the backup
mysqldump $mysqldump_opts -u $db_user -h $db_host -p$db_pw $db_name > $sql_file
#create the gzip’d attachment
gzip $sql_file
#send the mail
#we could also scp it offsite instead (better)
echo “Automated database backup” | mutt -a $gzip_file -s “$subject” $recp
#delete temp file
rm $gzip_file
Description
The process is very simple.Generally, there are 3 steps here.
- Step1: Set the variables, such as the file name for the database backup file.In order to make you distinguish the attachment file name, it is renamed according to the current day. why it is current day, becuase it backups daily.
this code is written in Shell script, which is similiar with the command script on Microsoft Windows system.it also includes variables, linear structure, condition and loop flow.
- step 2: dump the data from database.there is a command: mysqldump can do this.
mysqldump is useful, and has many options. In this case,the options string is “–add-drop-table -acQq”, –add-drop-table is to add or drop the table before the generated sql,for more information, please visit mysql website .
- step 3:compress the data file exported from mysql
Package compression and extraction are the common function on Linux operation system, such as tar,gzip,unzip.etc. for more information ,you can use “man command ” in terminal .
- step 4: move it to main inbox, and delete the temporary file
Mutt is used here, please notice that it is not a mail client, and it can’t send mail, what it does only is move the email to mail server, and fortunately, dreamhost supports it.
Config it
please save the script as “backup.sh”, and then enter ctrontab -e,input below code:
0 0 * * * ./backup.sh
then press Ctrl+o ,then Enter to save
finally, press Ctrl +X
All is OK!