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:
1 2 3 4 5 6 7 8 9 10 |
# Create a folder called dropbox: mkdir dropbox # cd in the dropbox folder: cd dropbox # Download the dropbox_uploader.sh script into the dropbox folder: curl "https://raw.githubusercontent.com/andreafabrizi/Dropbox-Uploader/master/dropbox_uploader.sh" -o dropbox_uploader.sh # Allow us to execute the dropbox_uploader.sh script: chmod 755 dropbox_uploader.sh # Run the script itself: ./dropbox_uploader.sh |
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
#!/bin/bash # Specify the path to the DropBox script DROPBOX_UPLOADER_SCRIPT="dropbox/dropbox_uploader.sh" # Specify the path of the WordPress Backups BKP_DIR="softaculous_backups" # Get the filename of the newest backup BKP_GET_FILE=$(ls $BKP_DIR | tail -n 1) # Provide the full path to the file that will be uploaded BKP_FILE="$BKP_DIR/$BKP_GET_FILE" # Get the name of the oldest file in Dropbox GET_OLD_FILE=$(dropbox/dropbox_uploader.sh -f ~/.dropbox_uploader list | head -n 1) # Strip out the [F] XXX prefix from the filename GET_OLD_FILENAME=$(echo $GET_OLD_FILE | sed 's/^.*wp/wp/') # Delete the oldest file in Dropbox $DROPBOX_UPLOADER_SCRIPT -f ~/.dropbox_uploader delete "$GET_OLD_FILENAME" / # Upload the latest backup to Dropbox $DROPBOX_UPLOADER_SCRIPT -f ~/.dropbox_uploader upload "$BKP_FILE" / # Send an email with the details mail -s "$(echo -e "Dropbox WordPress Backup\nFrom: WordPress Backup<from.some@address.com>\nReply-to: from.some@address.com")" to.some@address.com <<<"The WordPress backup $BKP_GET_FILE has been copied to Dropbox. The old backup $GET_OLD_FILENAME has been deleted." |
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:
1 |
chmod 755 /path/to/WPBackup.sh |
Last step is to create the cron job which will run the backup script. To do this, run the following command:
1 |
crontab -e |
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:
1 |
0 0 * * * dropbox/WPBackup.sh |
Basically, this means that the WPBackup.sh
script will be run at midnight every day. Cron jobs follow the format below:
1 2 3 4 5 6 7 8 |
* * * * * command to be executed - - - - - | | | | | | | | | ----- Day of week (0 - 7) (Sunday=0 or 7) | | | ------- Month (1 - 12) | | --------- Day of month (1 - 31) | ----------- Hour (0 - 23) ------------- Minute (0 - 59) |
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:
1 |
Listing "/"... DONE |
What was happening is that the script would interpret the greater-than symbol ( > ) as a command, displaying the following error:
1 |
-jailshell: >: command not found |
So, the workaround is to edit the dropbox_upload.sh
file and comment-out the sections that display
1 |
> Listing "/"... DONE |
Simply search for the following two values and comment (#) them out:
1 2 |
print " > Listing \"$DIR_DST\"... " print "DONE\n" |
Save the file. The next time you run the script, the oldest file will actually be deleted.