Generating automatic full backups with CPanel

Submitted by Jochus on Sun, 17/06/2012 - 22:15 | Posted in: Website
This blogpost is tested on CPanel 86.0.17

Generating backup

Each month, I take a full backup of my webfolder at http://www.jochenhebbrecht.be. It contains all webdata, as also a seperate SQL file for each of my databases. My host (http://www.vivior.net) offers me CPanel, which is giving me a lot of oppurtunities to do funky things. Up until today, I always manually logged in to CPanel, browsed to the Full backup wizard and started the job.

But today, I configured a cron job which does this job for me.

  • Login to CPanel
  • Go to the File Manager app and upload the following file - full_backup.sh - into your home directory:
    #!/bin/bash
    curl --user ##USERNAME##:##PASSWORD## https://##DOMAIN_NAME##/frontend/paper_lantern/backup/wizard-dofullbackup.html --data-binary $'------WebKitFormBoundaryGww39ElKNvLB43AS\r\nContent-Disposition: form-data; name="dest"\r\n\r\nhomedir\r\n------WebKitFormBoundaryGww39ElKNvLB43AS\r\nContent-Disposition: form-data; name="email_radio"\r\n\r\n1\r\n------WebKitFormBoundaryGww39ElKNvLB43AS\r\nContent-Disposition: form-data; name="server"\r\n\r\n\r\n------WebKitFormBoundaryGww39ElKNvLB43AS\r\nContent-Disposition: form-data; name="user"\r\n\r\##USERNAME##\r\n------WebKitFormBoundaryGww39ElKNvLB43AS\r\nContent-Disposition: form-data; name="pass"\r\n\r\n##PASSWORD##\r\n------WebKitFormBoundaryGww39ElKNvLB43AS\r\nContent-Disposition: form-data; name="port"\r\n\r\n\r\n------WebKitFormBoundaryGww39ElKNvLB43AS\r\nContent-Disposition: form-data; name="rdir"\r\n\r\n\r\n------WebKitFormBoundaryGww39ElKNvLB43AS--\r\n' --compressed

  • Provide the file execute permissions
  • Now go to the Cron jobs app and add the following line. It will create a full backup the first day of the month:
    0 0 1 * *   sh ~/full_backup.sh
  • A full backup is now generated to your homedir.

Automatic download of full backup

I'm running Drupal 7 on a local Ubuntu installation. I've created a simpel module which automatically downloads the file at the 2 day of the month (as the 1th day, the backup is generated). This is the script which you can reuse:

<?php
function ftp_downloader_cron() {
	if (date('d') == '2') {
		// Configuration
		$ftp_server = '##FTP_SERVER##';
		$ftp_user_name = '##FTP_USER##';
		$ftp_user_pass = '##FTP_PASSWORD##';
 
		// Set up basic connection
		$conn_id = ftp_connect($ftp_server);
 
		// Login with username and password
		$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);
 
		// Get all files in FTP home dir
		$files = ftp_nlist($conn_id, '.');
 
		// Get name of backup file
		foreach($files as $file) {
			if (preg_match("/^backup\-.*jochenhebbrecht\.tar\.gz/", $file)) {
				watchdog("jochus_ftp_downloader", "Found file: %file", $variables = array('%file' => $file), $severity = WATCHDOG_NOTICE, $link = NULL);
 
				// Download file
				if (ftp_get($conn_id, "##SERVER_LOCAL_PATH##/$file", $file, FTP_BINARY)) {
					watchdog("jochus_ftp_downloader", "Successfully downloaded file", $variables = array(), $severity = WATCHDOG_NOTICE, $link = NULL);
				} else {
					watchdog("jochus_ftp_downloader", "File could not be downloaded", $variables = array(), $severity = WATCHDOG_ERROR, $link = NULL);
				}
 
			}
		}
 
		// Close the connection
		ftp_close($conn_id);
	)
}

Comments

Submitted by hfinlay10 (not verified) on Thu, 15/11/2012 - 12:46
 

Hi,

In running this I get the error:

wget: invalid option -- b
Usage: wget [OPTION]... [URL]...

Any clues?

Thx

Submitted by Jochus on Thu, 15/11/2012 - 12:58
 

hfinlay10 ,

Can you post your script somewhere like pastebin?
I didn't specify an option "-b", so I think you're missing something ...

Jochen

Submitted by hfinlay10 (not verified) on Thu, 15/11/2012 - 22:47
 

Hi Jochen,

I have posted it in pastebin: cpanelautobackup

I thought it odd too, as there is no option "b", but anyway, it is posted there.

Cheers, Hugh

Submitted by hfinlay10 (not verified) on Fri, 16/11/2012 - 07:43
 

Hi,

Got it working--created a new copy of the file and uploaded that. Somehting must have been wrong with the original;.

By the way, what I'd really like to do is to get the output to save to a different directory. Any idea how to make that work?

Cheers.

Submitted by Jochus on Fri, 16/11/2012 - 09:43
 

Hi,

Glad you could work it out!

If you want to store the output to a different directory, you could use:

$ wget ... 2>&1 >/path/to/my/directory/output.txt

More information on output/error redirection can be found here: http://www.mathinfo.u-picardie.fr/asch/f/MeCS/courseware/users/help/gen…

Btw, I didn't find the possibility to get your pastebin based on a name. Normally, I receive an id, but not a name. How does that work?

Jochen

Submitted by hfinlay10 (not verified) on Fri, 16/11/2012 - 10:16
 

OK, thanks, I think dobackupfull will only go to home directory and is not configurable.

Don't know about pastebin--that was the first time I have used it.

Thx for help. Cheers.

Add new comment

The content of this field is kept private and will not be shown publicly.

Full HTML

  • Lines and paragraphs break automatically.
  • You can caption images (data-caption="Text"), but also videos, blockquotes, and so on.
  • Web page addresses and email addresses turn into links automatically.
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <bash>, <cpp>, <css>, <html5>, <java>, <javascript>, <php>, <sql>, <xml>. The supported tag styles are: <foo>, [foo].
CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.