Web Hosting Forum | Lunarpages


*
Welcome, Guest. Please login or register.
Did you miss your activation email?



Login with username, password and session length
February 09, 2012, 09:10:43 PM

Pages: [1]   Go Down
  Print  
Author Topic: Automated fullbackup to a ftp/ssh server using cPanel's fullbackup  (Read 3695 times)
TeraHz
Spaceship Captain
*****
Offline Offline

Posts: 100


WWW
« on: November 11, 2005, 11:41:55 PM »

Hi there,
Just noticed how nice cnapel will backup my entire user (email, DB, web files, logs etc.) and upload it to my own server! So I started looking at how to add this fullbackup to my crontab. Unfortunately it is a form that has to be filled and it is not easy to do with simple unix command. So using an old cpanel script I ended up with this one, that will login to cpanel for you, start the backup and upload it to wherever you requested:

http://joro.geodar.com/diary/2005/11/11/233 is the URL and the script is in PHP:
Code:
< ?php

// PHP script to allow periodic cPanel backups automatically.
// Based on script posted by max.hedroom in cpanel.net forums
// This script contains passwords. KEEP ACCESS TO THIS FILE SECURE!
// It is updated to support secure ftp connections and remote directories

// ********* THE FOLLOWING ITEMS NEED TO BE CONFIGURED *********

// Info required for cPanel access
$cpuser = "username"; // Username used to login to CPanel
$cppass = "password"; // Password used to login to CPanel
$domain = "www.host.com"; // Domain name where CPanel is run
$skin = "x"; // Set to cPanel skin you use (script won't work if it doesn't match)

// Info required for FTP/SFTP host
$ftpuser = "sshusername"; // Username for FTP(SCP) account
$ftppass = "sshpassword"; // Password for FTP(SCP) account
$ftphost = "sshhost"; // Full hostname or IP address for FTP(SCP) host
$ftpmode = "scp"; // FTP mode ("ftp" for active, "passiveftp" for passive, "scp" for secure ftp)
$ftpport = "22"; // PORT for FTP(SCP) host
$ftprdir = "/path/to/directory"; //Remote Directory on SCP host

// Notification information
$notifyemail = "mailto@somehost.com"; // Email address to send results

// Secure or non-secure mode
$secure = 1; // Set to 1 for SSL (requires SSL support), otherwise will use standard HTTP

// Set to 1 to have web page result appear in your cron log
$debug = 0;

// *********** NO CONFIGURATION ITEMS BELOW THIS LINE *********

if ($secure) {
$url = "ssl://".$domain;
$port = 2083;
} else {
$url = $domain;
$port = 2082;
}

$socket = fsockopen($url,$port);
if (!$socket) { echo "Failed to open socket connection!\n"; exit; }

// Encode authentication string
$authstr = $cpuser.":".$cppass;
$pass = base64_encode($authstr);

$params = "dest=$ftpmode&email=$notifyemail&server=$ftphost&user=$ftpuser&pass=$ftppass&port=$ftpport&rdir=$ftprdir&submit=Generate Backup";

// Make POST to cPanel
fputs($socket,"POST /frontend/".$skin."/backup/dofullbackup.html?".$params." HTTP/1.0\r\n");
fputs($socket,"Host: $domain\r\n");
fputs($socket,"Authorization: Basic $pass\r\n");
fputs($socket,"Connection: Close\r\n");
fputs($socket,"\r\n");

// Grab response even if we don't do anything with it.
while (!feof($socket)) {
$response = fgets($socket,4096);
if ($debug) echo $response;
}

fclose($socket);

?>


Just save the code in a file, edit the variables to reflect your own data and execute from anywhere with: php nameoffile.php

so I added to my own crontab :
00 02 * * * /usr/bin/php /path/to/my/phpfile.php >/dev/null

and every day at 2 local time I start a backup of my entire username. and in about an hour (depending on the speed and size of your directory this time will vary) I have something like :
backup-MM.DD.YY_HH-MM-SS_myusername.tar.gz
in the directory I've specified in the file.

For any problems you can mail me or post here.

P.S. If admins think it is OK, they can put this post in the How-to's section. I guess that's the right place for it to be.

Enjoy
« Last Edit: November 12, 2005, 07:05:44 AM by Lupine1647 » Logged

-
?? Huh ?? HuhHuh??!
Lupine1647
Berserker Poster
*****
Offline Offline

Posts: 10868


« Reply #1 on: November 12, 2005, 07:06:13 AM »

Very Handy Tera. I modified your post a bit to put the PHP code in code tags. Make it a bit easier to read.
Logged
tegralens
Spacescooter Operator
*****
Offline Offline

Posts: 39


« Reply #2 on: April 05, 2006, 05:51:52 AM »

I tried this but did not work Cron did not even send me the email.  Help
Logged
TeraHz
Spaceship Captain
*****
Offline Offline

Posts: 100


WWW
« Reply #3 on: April 05, 2006, 10:41:01 PM »

can you give me more info what exactly did you set up so I can tell you what could be wrong?
here is my setup:
Homeserver -> the machine I have at home
LPserver -> the machine that has my website on lunarpages

on Homeserver I have:
in /etc/cron.daily a file that contains "/usr/bin/php /sbin/backup.php"
the contents of /sbin/bachup.php are the script in my first post.

on Homesever I've created a new user "upload" with home folder /storage/backup/geodar.com
in backup.php I've set after "// Info required for FTP/SFTP host" ftpuser to be "upload", "ftppaass" to be the password of user "upload", "ftphost" is the hostname of my server (it works with IP address also), ftpmode is set to "scp", ftpport is "22" and "ftpdir" is "/storage/backup/geodar.com"
notifyemail is set to an email I use to receive the notifications.

below "//info required for cPanel access" I've enter my cpanel username, cpanel password, my domain name, and the default skin is "x" in cpanel.

Once you are sure the information for your site is correct and all usernames and passwords and paths and servernames are correct, you can test it with the command
"/usr/bin/php /sbin/backup.php" (That is if you have stored the file in /sbin/)
the script exits right away and in fiew minutes you should start receiving your backup.

Tell me what is your setup and I'll try to help.
Logged

-
?? Huh ?? HuhHuh??!
8bitkid
Space Explorer
***
Offline Offline

Posts: 8


« Reply #4 on: August 24, 2010, 01:13:29 AM »

i am unable to get this to work.  i get an email sent when the cron job starts, but nothing seems to happen.
Logged
MrPhil
Berserker Poster
*****
Offline Offline

Posts: 5083



« Reply #5 on: August 24, 2010, 08:45:01 AM »

(Just a note on the original cron entry) don't add >/dev/null until you're satisfied that the job is working correctly. cron will email you all stdout and stderr output. If you redirect stdout to null, there is nothing to email. stderr is normally not redirected into the bit bucket (2>/dev/null), as it has error messages that you want to know about.

@8bitkid, try adding some debug print or echo statements in the code, to see how far you're getting and what certain values are. You can run the script from the browser while debugging, but keep in mind while doing that the run-time environment is quite different between the browser and the cron command line. Assume nothing about the directory you start in or the values of environment variables.
Logged

8bitkid
Space Explorer
***
Offline Offline

Posts: 8


« Reply #6 on: August 25, 2010, 12:34:32 PM »

@8bitkid, try adding some debug print or echo statements in the code, to see how far you're getting and what certain values are. You can run the script from the browser while debugging, but keep in mind while doing that the run-time environment is quite different between the browser and the cron command line. Assume nothing about the directory you start in or the values of environment variables.

what exactly should i enter in? i'm not very experienced with cron.  additionally, the email i get is the entire script.  does this mean that it isn't running at all?
Logged
MrPhil
Berserker Poster
*****
Offline Offline

Posts: 5083



« Reply #7 on: August 25, 2010, 03:34:02 PM »

You get the entire script spat back out at you? It doesn't sound like it's running. Are there any error messages mixed in? What is the exact cron command line you're using?
Logged

8bitkid
Space Explorer
***
Offline Offline

Posts: 8


« Reply #8 on: August 25, 2010, 03:35:34 PM »

yes, the entire script, just emailed to me.

i am using: "/usr/local/php5/bin/php-cgi /home/*********/fullbackup.php "
Logged
8bitkid
Space Explorer
***
Offline Offline

Posts: 8


« Reply #9 on: August 25, 2010, 03:44:47 PM »

in addition, this is at the top of the email before the code:

X-Powered-By: PHP/5.2.9
Content-type: text/html
Logged
8bitkid
Space Explorer
***
Offline Offline

Posts: 8


« Reply #10 on: August 25, 2010, 04:22:36 PM »

so was able to get things going by deleting the space between < and ?php at the beginning of the file.

now, it does seem to create a file on the lunarpages host, but it is about half as big as it should be.  further, it transferred a file of the correct name to my home ftp server, but it is 0 bytes.
Logged
MrPhil
Berserker Poster
*****
Offline Offline

Posts: 5083



« Reply #11 on: August 25, 2010, 08:19:18 PM »

1. That's odd that eliminating the PHP-start tag (<?php) would help get PHP running.
2. Is there some reason that you have to use the php-cgi rather than regular PHP? And do you need PHP 5 specifically, or can you use PHP 4 if that's the default for your server? If so, just php would be enough.
3. Use the -q flag (php -q) to suppress the X-Powered-By and Content-type lines.

Try just php -q /home/*********/fullbackup.php, with and without the <?php tag fixed.
Logged

8bitkid
Space Explorer
***
Offline Offline

Posts: 8


« Reply #12 on: August 26, 2010, 01:43:01 PM »

i was able to finally get this working once i fixed the tag and switched FTP mode from passive to active.
Logged
Pages: [1]   Go Up
  Print  
 
Jump to: