How to backup your website with cron, Transmit, Automator, Cronnix, Syncback and Jungledisk. The goal here is to (1) have the website backed up to your machine and also backed up using Jungledisk and (2) for this to happen automatically at whatever time interval you want. The idea is to set and forget. Part One discusses the other nightly database backups you may want to make.
Part One - the db cron job.
Create a directory called ’site-backup’ - this directory MUST be above /public_html/.
DBNAME=set-your-db-name
DBPASS=set-your-password
DBUSER=and-set-your-username
mysqldump --opt -u $DBUSER -p$DBPASS $DBNAME > backup.sql
gzip backup.sql
mv backup.sql.gz ~/site-backup/db-backup.sql.gz
This compresses the database and saves it in the same directory.
Note: If your db backups are huge and you realise that stats tables really are not needed in the backup then change this:
mysqldump --opt -u $DBUSER -p$DBPASS $DBNAME > backup.sql
to this:
mysqldump --opt -u $DBUSER -p$DBPASS $DBNAME wp_users wp_posts [and all the other table names] > backup.sql
Use your db prefix (in WordPress check your wp-config.php file) and just use the core tables.
Copy the above into Notepad/Textedit/whatever and save it as ‘cron-db-backup.sh’
Part Two - the website cron job.
#!/bin/sh
SITE=69105
tar -czf ~/site-backup/$SITE.tar.gz ~/public_html
Call this one cron-site-backup.sh and save it also in the ’site-backup’ directory
It compresses the /public_html/ directory and saves that in the same directory. (Do not have variables in the name.)
Give both .sh files permissions of 700. Now go to your domain control panel and set the cron jobs. You should test it to check it works. Be warned that if you have a huge site it can take a while and that you should not run it continuously - your host will get annoyed. The result should be that you get 2 files saved into that directory.
For now just run this cron job once - set the time for it to run a few minutes ahead of where you are now. All you need is for the 2 files to be in that directory. (If this does not work, check all the details and if necessary talk to your host). In cPanel cron jobs are in the Advanced box.

Part Three - Getting and copying the files.
If you use Windows, just go buy Syncback. That is all you will need to do. Sure it costs a few $$ but if you lost your site because you didn’t have a backup you’d pay much more to have it back. Go to Part Four.
If you use Linux, I have no idea what you do next. Also go to Part Four.
If you use OS X, read on.
You need to create 2 workflows in Automator. This will get the files, rename the files, copy the files then move the files to trash.
Open Automator, choose the Files/Folders option. Ignore the Library column. In the middle column drag “Open Finder Items” to the right pane. In the dropdown choose Transmit.
Next drag “Download Files” to the right pane. I have this domain as a Favourite so I choose Favorite, then this domain. Then I have Replace and then I have the destination. This is a file called 69105 in the Sites folder (/Users/mark/Sites/69105).
The remote filepath just needs the directory as you can see. (Remember this directory must not be inside /public_html). You have to give the exact filenames or Transmit won’t quit.
So far then it logs in by FTP, downloads the 2 files into the correct directory.
Next you need to rename them. This backup is not going to run daily so adding the Day Month Year should be okay.
Once it has renamed them you copy them to an external HD. The backup directory I have is also called 69105 but it is on the other HD.
If you do not have an external HD you must still copy them to another directory - they will be deleted if not.
And then Transmit quits.
Save this file as “one”. Inside Sites I have a folder called “workflows” which has my workflow files in.
On the top right of the Automator you can see Run. So do so and check it does what you need.
You should have the backup files on your computer and if you have an external HD they should be on there as well.

Part Four - Jungledisk
I created a separate Jungledisk bucket just for this task so I can schedule it as I need. Set it to copy from the folder you downloaded the files into.
Part Five - back to Automator
Now we move the downloaded files to Trash. You need the Actions as below. Because Jungledisk copies everything it sees you need to remove the old files to make way for the new. But they are only going to Trash - you can still get at them.
Save this file as “two” in the “workflows” directory.
Again, Run it.

You have all the right parts now, they just need assembling.
Time. You need to know the time where your site is hosted. If your site’s server time is different to yours then you need to know that before setting the times you are about to. Just ask your host. I’m assuming my server is next to me.
1:00pm is when I want the website to be backed up and only on a Sunday but every week.
2:00pm on Sunday is when I want workflow one to go
3:00pm for Jungledisk.
4:00pm for workflow two (On Sunday again)
The website cron has a panel just for that. You will have used that for your database backups.
Jungledisk has it’s own scheduling and using a separate bucket lets me schedule this job just for Sunday.
The workflows need a cron job to tell them. So download Cronnix. Choose the Simple tab. For 2pm on Sunday it looks like this:

Save the file. Do another but for 4pm and two.workflow
That’s almost it. Test by shortening the time gaps and run it when you are there for a couple of times until you are happy.
Part Six - Delete files
You still have the 2 files on the server and they need deleting.
Create a new file - call it cron-delete.sh and upload it to the same directory you made in Part One.
#!/bin/sh
rm ~/site-backup/*.gz
Please copy that carefully. The ‘rm’ command should be treated with every respect. Those 2 letters together are powerful. Set that to be run at say 5pm on the Saturday. You could add these lines before the cron that creates the files if you wished.
Other items:
I accept no responsibility for anything at all anywhere at any time if you do the above and data goes missing. It works for me but I can’t check what you have.
If the cron job just doesn’t start, open Terminal and type your version of “automator /Users/mark/Sites/workflows/one.workflow/Contents/document.wflow” and see what happens. If it says the file does not exist check your path.
I have no idea if and if so how other FTP clients may be used with Automator.
Talk to your host before setting cron jobs to run frequently. I can’t see why you’d need to backup a whole site more then weekly but hey, your choice.
If you are testing with Jungledisk and want to test say hourly then JD starts the hour countdown when you press OK on the schedule screen. I couldn’t see a way to set it better than that - but it’s only while testing I used this.

One Trackback/Pingback
[...] and could save your blog. This was originally published here (by me, I’m copying no-one): Part 2 of this backup plan uses this information too. Using your ftp client (or file manager) create a directory above public_html called [...]
Post a Comment