A WordPress installation consists of both files and a MySQL database. To prevent data loss, it is essential that you backup both the files and the database.
There are many free and commercial WordPress plugins that can do this for you. For instance, search for “backup” from the Install Plugins page of your WordPress installation.
However, to have full control over the process, I decided to make my own backup system. This guide describes the main parts:
As you will learn below, I use the following tools to carry out the task:
- Transmit from panic.com to sync files between my remote WordPress installation and a local folder on my Mac.
- Hazel from Noodlesoft to fetch and rename database backups and to purge old files from the backup folder.
- The OS X Automator to control Transmit during the syncing of the WordPress files.
- A Linux system, with MySQL installed, to do the database dumps from the remote WordPress installation.
With this short introduction, it is time for the detailed description.
How to backup the files
With Hazel a sync process is set up to run once a day. Hazel launches an Automator workflow that only consists of a Transmit sync task. At a later time, also once a day, Hazel checks if any of the locally synced files have changed during the last 24 hours. If so, the changed folder is archived. The use of the Automator and Transmit combination for backing up WordPress files is inspired by the post “Automating WordPress Backups using Automator and Transmit” at Bits of Tech.
Setting up Automator and Transmit
To setup the Automator sequence, follow these steps:
- Start Transmit and make your blog’s site a Favorite site. You make your connection a Favorite by dragging it and dropping it on the Favorites node in the left sidebar (c.f. the figure below).
- Start Automator and select Workflow as the document type.
- Add a Transmit Synchronize action, by dragging it from the left Internet Library category to the right Automator window.
- The sync task is setup by defining the favorite site that you defined in step 1, setting the Sync Direction to Download and the Local Path to the local folder where you want to store the synchronized copy of your WordPress site. Check the Delete Orphaned Destinations Items box to remove local items that don’t exists in your remote WordPress site. The figure below shows my setup.
- The workflow is then finished by adding a Quit Application task, to end the Transmit application. Below you find the total Automator workflow.
- Save the workflow on your Mac as a Workflow.
Syncing the WordPress files
Hazel is then used to launch the Automator workflow once a day. The Hazel rule is defined by the following steps:
- Launch the Hazel settings pane, by selecting Open Hazel… from the Hazel icon in the menu bar or by opening the Hazel settings from the System Preferences application.
- Add the parent folder to the Local Folder (named sync above) that you defined in the Automator Transmit Synchronize task to the left Folders window. The sync folder is a sub-folder to a folder called backup in my setup. Thus, add backup to the Folders window.
- For the backup folder, add a rule in the right Rules window, by clicking the plus button. Add the following conditions for the rule:
- Add an Automator workflow action, where you select the previously defined Automator Workflow. Below you find the complete Hazel rule.
Creating archives of modified folders
To save some history, the following Hazel rule creates archives if any synced files has been modified during the last day. The rule is defined by the following steps:
-
Add a folder in the left folder window. In this case select the Local Folder, in my case called sync, that was previously defined in the Automator Transmit sync workflow.
- Add a rule for the sync folder, by clicking the plus sign in the right Rules window. Add the following conditions and actions for the rule:
-
Set the time when the rule should be run. Allow for sufficient time that the previous synchronization task has completed.
-
Make sure that only folders are recognized, by setting Kind to Folder.
-
Add the following Unix command that returns true if any sub-folder has been modified during the last 24 hours:
find $1 -type f -mtime 1
Run man find from a Mac Terminal to get more information about the find command. ‘$1’ is replaced by the folder satisfying the conditions defined in step A and B above.
-
Copy the folder to the parent folder called backup.
-
Archive the copied folder.
-
Rename the archive by adding a modification time stamp to the archive file name. In my case, the WordPress files are included in a folder called public_html below the sync folder. Thus, the archive will get a name similar to public_html_2012–05–18_040003.zip. Below you find the Hazel rule for the sync folder. The date format is defined by clicking the little date modified selector and then selecting the Edit Date Pattern… from the menu obtained by clicking the small down arrow on the date modified selector.
-
How to back up the database
To backup the MySQL database I use a Linux machine, since I don’t have MySQL installed on my Mac. The command to perform the actual database backup is called mysqldump
.
Dump the database to the Linux machine
Follow the steps below to perform the backup:
-
Create a file called .my.cnf in your home directory on the Linux machine, to store your database password. Thus, with your favorite text editor create the file.
-
Insert the following text in the file:
[mysqldump] user = user_name password = pass_word
Replace
user_name
andpass_word
with the particular information you have received from your website provider. -
Save the file.
-
Make sure that no one else can read the file, by issuing the command
chmod 600 .my.cnf
-
Create a new file, in my case called ~/bin/backup_ulfolin_se_db, with the following content:
cd mysqldump --host=host_name --port=3306 --user=user_name ulfolin_se_db > tmp.sql 2>&1 mv tmp.sql ulfolin_se_db.sql
Again, replace
host_name
anduser_name
with information you have received from your website provider.ulfolin_se_db
is the name of my database. Replace it with the name of your database. The port number 3306 is used be my website provider. Your website provider might use another port number. To prevent triggering premature Hazel actions on the database dump file, I first dump the database to a temporary file calledtmp.sql
and then renames that file to a name that will trigger Hazel actions. The first command,cd
, just makes sure that you are performing all statements from your home folder on the Linux machine. Note that themysqldump
statement is a long one and might be displayed above on two lines. -
Save the file and close the editor.
-
Create a crontab entry by issuing the command
crontab -e
This will bring up your text editor with your crontab file.
-
Add the following line
0 4 * * * ~/bin/backup_ulfolin_se_db
This will make sure the file you created above is run every day at four o’clock in the morning.
-
Save the file and close the text editor.
Copy the database dump to your Mac
The following setup works if you have your Linux home directory mounted to your Mac with write access. Then Hazel can watch the Linux home directory for the file ulfolin_se_db.sql and move it to the previously defined backup folder, when the file is available. Thus, define the following Hazel rule:
- Add your mounted Linux home directory as a folder that Hazel should watch (this folder is called uol in my case).
- Add the condition that the Full Name is ulfolin_se_db.sql (or whatever you chose to call you database dump file).
- Move the file to the previously defined backup folder .
- As for the WordPress file archives, add a modification time stamp to the file name.
The complete Hazel rule is found below.
Delete old files
Finally, create a Hazel rule that deletes old files from your backup folder.
- Select the backup folder in the left Hazel Folders window. You have already created the sync rule for that folder.
- Add a condition that Date Last Modified is not in the last 2 months (or whatever retention period that you want).
- Add the action to Move to folder Trash.
The complete Hazel rule is found below.
After writing this post, I found an excellent tool called BackWPup. I am giving it a try now.