Menu Close

Automated WordPress backup via Linux Shell

I am using No Support Linux Hosting as my webhost, mostly because it’s cheap ($12/year!) and meets my needs for hosting.

One thing about my host is, well, no support! So if anything goes wrong or I have some technical questions I’m on my own. With that in mind, I wanted to implement a solution that would allow me to copy the WordPress backups generated by Softaculous in case something happens with my host.

The goal: total automation of the backup and email notification once the job was completed, and I think I’ve come up with very nice process to accomplish just that.

The idea I had was to:
– Utilize the shell
– Upload the latest backup to Dropbox
– Delete the oldest backup from Dropbox
– Send an email once the process was completed

I came across DropBox-Uploader which is a BASH script that can used to manipulate DropBox.

So, let’s just jump right in. These instructions should apply to any Linux host, but your mileage may vary.

Log into your shell and run the following commands:

Once you have run the ./dropbox_uploader.sh script, the script will ask you to visit https://www.dropbox.com/developers/apps and create a Dropbox app using the Dropbox API, this will allow you to connect your shell to Dropbox. Follow the on-screen instructions in your shell.

Once you have entered your keys, the script will tell you to visit a specific URL (you can copy + Paste) into your browser. This will take you a Dropbox window which will ask you to authorize your app to allow it to access your account. After completing this, hit enter in your shell to finish the process.

You should now have a new folder with your app name inside of the Apps folder in Dropbox – this is where your files will be saved to.

The next step is to now write a script in your favourite editor (nano!) that will get the last backup generated by Softaculous, upload it to Dropbox and then email us once completed.

Below is the script, modify as needed for your environment.

Great, now save this file as <code>WPBackup.sh</code>, or whatever name you want, and now let’s set the permissions on the file to allow us to run the script:

Last step is to create the cron job which will run the backup script. To do this, run the following command:

Now we need to create the job. I won’t go into detail about cron jobs here, you can read up more about them at http://www.cyberciti.biz/faq/how-do-i-add-jobs-to-cron-under-linux-or-unix-oses/
but to give you an idea, here is the cron job for my backup:

Basically, this means that the WPBackup.sh script will be run at midnight every day. Cron jobs follow the format below:

Once you have your cron job entered, save the file.

…and that’s it! Your backups should now start appearing in your Dropbox folder at whatever time you’ve scheduled your job and an email will be delivered to you when completed.

Update (2016/02/16): I’ve recently noticed that the script was not actually listing the oldest file on Dropbox due to the way the dropbox_upload.sh script displayed the data.

The script would first display the following line when listing the oldest file:

What was happening is that the script would interpret the greater-than symbol ( > ) as a command, displaying the following error:

So, the workaround is to edit the dropbox_upload.sh file and comment-out the sections that display

Simply search for the following two values and comment (#) them out:

Save the file. The next time you run the script, the oldest file will actually be deleted.

Posted in Code, Linux, Site, Tech, WordPress

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.