Generating automatic full backups with CPanel 11

Submitted by Jochus on Sun, 17/06/2012 - 22:15 | Posted in: Website
This blogpost is tested on CPanel 11.34.1 (build 7)

Generating backup

Each month, I take a full backup of my webfolder at http://www.jochus.be. It contains all webdata, as also a seperate SQL file for each of my databases. My host (http://www.bedesign.be) offers me CPanel, which is giving me a lot of oppurtunities to do funky things. Until today, I 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
    wget -O /dev/null --http-user=##USERNAME## --http-password=##PASSWORD## <a href="https://localhost:2083/frontend/x3/backup/wizard-dofullbackup.html">https://localhost:2083/frontend/x3/backup/wizard-dofullbackup.html</a> --auth-no-challenge --post-data="dest=homedir&email=##EMAIL##"

    Mind the --auth-no-challenge option. It fixes the following issue:

    HTTP request sent, awaiting response... 401 Access Denied
    Unknown authentication scheme.
    Authorization failed.
  • If you encounter certificate issues, you could add the --no-check-certificate option:

    ERROR: certificate common name `XYZ' doesn't match requested host name `MYHOSTNAME'.
    To connect to MYHOSTNAME insecurely, use `--no-check-certificate'.
    Unable to establish SSL connection

  • 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\-.*_jochus\.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.