Jump to content
Farnes

Cronjob Of Cpanel Backup

Recommended Posts

I've scoured the forums but can't seem to find an answer...

 

How can I set up a cronjob to run (say every morning at 3:30am) to perform the cPanel home directory backup and ftp it somewhere?

 

Thanks much for the help!

Share this post


Link to post
Share on other sites

cPanel itself doesn't have an option to cron individual cPanel accounts.

 

I put this together based on some info I found on cpanel.net and looking at cPanel itself. It works by posting a request to cPanel to run the backup (so it can maintain the same method of backup as cpanel is upgraded). You could actually run this on any machine that has web access to your TCH host and PHP installed, you wouldn't necessarily need to run it on your account.

 

Disclaimers:

  • I wouldn't recommend using this script unless you at least minimally understand what it does.

  • This does contain passwords, so I recommend setting the permissions on this file to 600 and not placing this file within your public_html subdirectories. Use this script at your own risk!

To run it as a cron job (at 3:30am like you wanted), put something in your crontab like the following (if the script is in your top directory):

>30 3 * * * /usr/local/bin/php /home/youraccount/fullbackup.php

 

Here's the actual script (name it something like fullbackup.php and make sure to set all of the configuration variables). Make sure when you cut and paste it that the $data line remains one line (the if statement is the beginning of the next line).

><deleted>  See updated version in new post below.

Share this post


Link to post
Share on other sites

I've updated the script to include SSL support. Although not so important if you run the script on the server you are generating the backup (besides, I don't believe the PHP on the TCH servers has SSL support compiled in)... the SSL support would be quite important if you decide to run this script somewhere else to connect to your TCH account.

 

><?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!

// ********* 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 = "example.com"; // Domain name where CPanel is run
$skin = "monsoon"; // Set to cPanel skin you use (script won't work if it doesn't match)

// Info required for FTP host
$ftpuser = "ftpusername"; // Username for FTP account
$ftppass = "ftppassword"; // Password for FTP account
$ftphost = "ftp.example.com"; // Full hostname or IP address for FTP host
$ftpmode = "ftp"; // FTP mode ("ftp" for active, "passiveftp" for passive)

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

// Secure or non-secure mode
$secure = 0; // 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... Bailing out!\n"; exit; }

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

$params = "dest=$ftpmode&email=$notifyemail&server=$ftphost&user=$ftpuser&pass=$ftppass&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);

?>

Share this post


Link to post
Share on other sites

Get this error when I tried to download the PHP script from the instructions page today:

Warning: fsockopen(): unable to connect to example.com:2082 in /home/total/public_html/help/HTMLobj-728/fullbackup.php on line 40

Failed to open socket connection... Bailing out!

Yikes! :goof:

 

as a side question: I'm guessing this won't do a backup of my MT SQL database right? Do I need another script for that?

 

later,

!!blue

Share this post


Link to post
Share on other sites

You can just cut and paste the script from above until Kevin gets a chance to fix the help site.

 

All the script does is tell cPanel to run a full backup. That full backup does include databases, but not in a way that would be easy to restore individual databases if you needed to.

 

It shouldn't be hard to create a script for the individual databases part. So if people want it I could whip something up for that, too.

Share this post


Link to post
Share on other sites
It shouldn't be hard to create a script for the individual databases part.  So if people want it I could whip something up for that, too.

Yes please!

 

A friend of mine was going to write me a perl script but he needed my table structure for the mt DB and it would take that & dump the db into a csv, then tar&gz it. Then I would ftp in & download it.

 

I said to wait & see if the backup you wrote would do it; but like you said, it wouldn't be easy to restore. Hmm... how would the one you would potentially write work? Would it just tar the whole db or would it read the .... :goof: wait, I'm getting myself more confused!!

 

help!

!!blue

Share this post


Link to post
Share on other sites

I'll take a look at it when I get some free time (tonight or tomorrow hopefully).

 

I could either use cPanel's db backups which do a mysql dump, gzip's it, and sends it to you. Or just do a mysql dump directly and bypass cPanel completely.

Share this post


Link to post
Share on other sites

I'm trying to use your PHP script to FTP a backup file to my PC running an FTP server. I've tried both FileZilla and Windows FTP Server on my PC but I can't seem to get your script to communicate with the FTP server. I've heard that it can be done, but I can't find the post that I first read about it. Do you know how to set the FTP settings in the PHP to communicate with the FTP server? :)

 

Also, I was getting good responses from your script, but now I'm getting an error.

 

Good responses:

 

Net::FTP>>> Net::FTP(2.74)

Net::FTP>>> Exporter(5.567)

Net::FTP>>> Net::Cmd(2.25)

Net::FTP>>> IO::Socket::INET(1.26)

Net::FTP>>> IO::Socket(1.27)

Net::FTP>>> IO::Handle(1.21)

Net::FTP=GLOB(0x82884ec)<<< 220-=(<*>)=-.:. (( Welcome to PureFTPd 1.0.12 )) .:.-=(<*>)=-

Net::FTP=GLOB(0x82884ec)<<< 220-You are user number 4 of 50 allowed.

Net::FTP=GLOB(0x82884ec)<<< 220-Local time is now 17:21 and the load is 1.17. Server port: 21.

Net::FTP=GLOB(0x82884ec)<<< 220 You will be disconnected after 15 minutes of inactivity.

Net::FTP=GLOB(0x82884ec)>>> user jrnhost

Net::FTP=GLOB(0x82884ec)<<< 331 User jrnhost OK. Password required

Net::FTP=GLOB(0x82884ec)>>> PASS ....

Net::FTP=GLOB(0x82884ec)<<< 230-User jrnhost has group access to: jrnhost

Net::FTP=GLOB(0x82884ec)<<< 230 OK. Current restricted directory is /

Net::FTP=GLOB(0x82884ec)>>> TYPE I

Net::FTP=GLOB(0x82884ec)<<< 200 TYPE is now 8-bit binary

Net::FTP=GLOB(0x82884ec)>>> ALLO 1302233

Net::FTP=GLOB(0x82884ec)<<< 200 Zzz...

Net::FTP=GLOB(0x82884ec)>>> PASV

Net::FTP=GLOB(0x82884ec)<<< 227 Entering Passive Mode (127,0,0,1,52,7)

Net::FTP=GLOB(0x82884ec)>>> STOR backup-4.13.2004_16-21-24_jrnhost.tar.gz

Net::FTP=GLOB(0x82884ec)<<< 150 Accepted data connection

Net::FTP=GLOB(0x82884ec)<<< 226-31809.2 Mbytes free disk space

Net::FTP=GLOB(0x82884ec)<<< 226-File successfully transferred

Net::FTP=GLOB(0x82884ec)<<< 226 0.037 seconds (measured here), 33.13 Mbytes per second

Net::FTP=GLOB(0x82884ec)>>> QUIT

Net::FTP=GLOB(0x82884ec)<<< 221-Goodbye. You uploaded 1272 and downloaded 0 kbytes.

Net::FTP=GLOB(0x82884ec)<<< 221 Logout - CPU time spent: 0.020 seconds.

--------------------------------------------------------

Copying Reseller Config...Done

Copying Mail files....Done

Copying proftpd file....Done

Copying www logs.............

.........

Done

Grabbing mysql dbs...Done

Grabbing mysql privs...Done

Copying mailman lists....Done

Copying mailman archives....Done

Copying homedir....Done

Copying cpuser file.......Done

Copying crontab file.......Done

Copying quota info.......Done

Storing Subdomains....

Done

Storing Parked Domains....

Done

Storing Addon Domains....

Done

Copying password.......Done

Copying shell.......Done

pkgacctfile is: /tmp/backup-4.13.2004_16-21-24_jrnhost.tar.gz

Creating Archive ....Done

md5sum is: 8eec6d0519a45641c33888c25ee6768e

 

Bad Responses:

 

Can't call method "login" on an undefined value at /usr/local/cpanel/bin/ftpput line 24.

--------------------------------------------------------

Copying Reseller Config...Done

Copying Mail files....Done

Copying proftpd file....Done

Copying www logs.............

.........

Done

Grabbing mysql dbs...Done

Grabbing mysql privs...Done

Copying mailman lists....Done

Copying mailman archives....Done

Copying homedir....Done

Copying cpuser file.......Done

Copying crontab file.......Done

Copying quota info.......Done

Storing Subdomains....

Done

Storing Parked Domains....

Done

Storing Addon Domains....

Done

Copying password.......Done

Copying shell.......Done

pkgacctfile is: /tmp/backup-4.13.2004_17-11-22_jrnhost.tar.gz

Creating Archive ....Done

md5sum is: ebdc5c580beb928fe2491374f3c43aa9

Share this post


Link to post
Share on other sites

My script (and PHP) doesn't actually do any FTP. All it does is tell cPanel to run the backup and do the FTP, so it's the cPanel process that is FTPing to you. All my script does is pass parameters to cPanel.

 

I'd recommend doing a backup manually (without the script) and try troubleshooting the problem that way. Once you have it confirmed working again, then reenable the script. If it works fine without the script, double check all of the values you have set.

 

Let me know if you find anything.

Share this post


Link to post
Share on other sites
It shouldn't be hard to create a script for the individual databases part.  So if people want it I could whip something up for that, too.

Yes please!

Btw, I haven't forgotten about this. I had the flu all last week so I didn't get to doing much of anything I wanted to during that time. :)

 

Once I finish my last minute tax stuff I'll get to it. :)

Share this post


Link to post
Share on other sites
I've tried both FileZilla and Windows FTP Server on my PC but I can't seem to get your script to communicate with the FTP server. I've heard that it can be done, but I can't find the post that I first read about it.

Ok.... after reading your post on webhostingtalk, I think I know more about what you are asking.

 

Have you successfully done any backup that FTP'd to your home machine? It sounded like you were still working that out. The address for your FTP is going to be your router's IP address, and you will have to allow FTP through your router to your PC (how depends on your router, look for port forwarding in the configuration). Since you are on a cable modem, it's going to be more difficult to use an automated script because your FTP address can change.

 

Is that where you are getting hung up, though?

Share this post


Link to post
Share on other sites

Correct. I basically can't get the FTP portion of the process to work. My PC is connected to the internet via a cable modem behind a DLink Cable/DSL Residential Gateway (DI-704). The DI-704 Gateway says that the IP address is 68.x.xxx.33. I set up FileZilla FTP Server to listen on port 21 using passive ftp IP address set to 68.x.xxx.33. I then attempt to FTP from my web account (RedHat Linux) to 68.x.xxx.33 but the connectiction times out. I understand that the IP address will change in the future (a future issue to deal with). I'll look to see if there is a port forwarding option in the router as you suggest (there are so many firewall options in the router that it may take a lot of trial and error).

Share this post


Link to post
Share on other sites

Connection Success ... FTP Failure!! I finally found the proper settings for the D-Link 704 to connect to the FileZilla FTP server but the copy commands appear to be unrecognized by the FTP server. Any ideas?

 

From your PHP email:

 

Net::FTP=GLOB(0x82883bc)<<< 220-FileZilla Server version 0.8.9 beta

Net::FTP=GLOB(0x82883bc)<<< 220 Nelli FileZilla Server

Net::FTP=GLOB(0x82883bc)>>> user ....

Net::FTP=GLOB(0x82883bc)<<< 331 Password required for jrnbackup

Net::FTP=GLOB(0x82883bc)>>> PASS ....

Net::FTP=GLOB(0x82883bc)<<< 230 Logged on

Net::FTP=GLOB(0x82883bc)>>> TYPE I

Net::FTP=GLOB(0x82883bc)<<< 200 Type set to I

Net::FTP=GLOB(0x82883bc)>>> ALLO 40607502

Net::FTP=GLOB(0x82883bc)<<< 500 Syntax error, command unrecognized.

Net::FTP=GLOB(0x82883bc)>>> PASV

Net::FTP=GLOB(0x82883bc)<<< 500 Syntax error, command unrecognized.

Net::FTP=GLOB(0x82883bc)>>> QUIT

Net::FTP=GLOB(0x82883bc)<<< 221 Goodbye

 

From FileZilla FTP Server:

 

(000002) 4/15/04 19:51:47 PM - (not logged in) (63.247.69.242)> Connected, sending welcome message...

(000002) 4/15/04 19:51:47 PM - (not logged in) (63.247.69.242)> 220-FileZilla Server version 0.8.9 beta

(000002) 4/15/04 19:51:47 PM - (not logged in) (63.247.69.242)> 220 Nelli FileZilla Server

(000002) 4/15/04 19:51:47 PM - (not logged in) (63.247.69.242)> user ******

(000002) 4/15/04 19:51:47 PM - (not logged in) (63.247.69.242)> 331 Password required for jrnbackup

(000002) 4/15/04 19:51:47 PM - (not logged in) (63.247.69.242)> PASS ******

(000002) 4/15/04 19:51:47 PM - jrnbackup (63.247.69.242)> 230 Logged on

(000002) 4/15/04 19:51:47 PM - jrnbackup (63.247.69.242)> TYPE I

(000002) 4/15/04 19:51:47 PM - jrnbackup (63.247.69.242)> 200 Type set to I

(000002) 4/15/04 19:51:47 PM - jrnbackup (63.247.69.242)> ALLO 40607502

(000002) 4/15/04 19:51:47 PM - jrnbackup (63.247.69.242)> 500 Syntax error, command unrecognized.

(000002) 4/15/04 19:51:47 PM - jrnbackup (63.247.69.242)> P@SW

(000002) 4/15/04 19:51:47 PM - jrnbackup (63.247.69.242)> 500 Syntax error, command unrecognized.

(000002) 4/15/04 19:51:47 PM - jrnbackup (63.247.69.242)> QUIT

(000002) 4/15/04 19:51:47 PM - jrnbackup (63.247.69.242)> 221 Goodbye

(000002) 4/15/04 19:51:47 PM - jrnbackup (63.247.69.242)> disconnected.

Share this post


Link to post
Share on other sites

It looks like you probably have $ftpmode set to passiveftp. Try changing that to ftp to enable Active FTP mode.

Share this post


Link to post
Share on other sites

When I do a regular ftp from the linux box, all commands are unrecognized unless I issue "passive" to turn passive mode OFF. All commands work after that. When executing your PHP, under either "passiveftp" or "ftp", I get unrecognized commands. It behaves as if passive mode is on in either case. Any ideas how to get the ftp in the php exec (or cpanel?) to set passive off?

Share this post


Link to post
Share on other sites

I'd love to see a cpanel (mysql) backup script version with the same functionality (ftp to remote server) - with date on the file name!

 

For the theme variable, what is the correct data for the X theme (X, xtheme....) ?

Share this post


Link to post
Share on other sites

The theme variable should be the same as what shows up in your cPanel info as the theme selected. For the X theme, it should be simply "x".

 

To double check, look in the URL. The path should show something like: /frontend/x/index.html ... the directory after frontend is what your theme variable needs to be set to.

Share this post


Link to post
Share on other sites

Thanks....

 

 

I'm looking for a script like that you've previously mentioned that will allow a cpanel like zipped mysql DB backup.

 

I've tried phpmyadmin for backing up plus lots of gzip and bzip2 scripts and whenever I test a recovery from these, the unzip process tells me the file is corrupt - but NOT when it's a cpanel backup, hence my interest... so at the moment I do a croned uncompressed ftp backup.

 

I'm not sure how cpanel do theirs, but it appears to be a different method to every other script I've used.

 

 

 

I've also been tested a more raw version of the script you have above, with the full backup line commented out and what I had hoped would do the mysql DB backup added, shown below, but it's a no go:

 

 

 

<?php

$target_host = 'currenthost';

$cpanel_userid = 'userid';

$cpanel_pass = 'thepassword';

$cpanel_theme = 'Xskin';

$ftp_host = 'ftp://myftpsite.com/httpdocs/backup/';

$ftp_user = 'mysite';

$ftp_pass = 'theftpsitepwd';

$email = 'admin@mysite.com';

 

//

// DO NOT ALTER ANYTHING BELOW THIS LINE

//

$user_pass = "$cpanel_userid:$cpanel_pass";

$authentication = base64_encode($user_pass);

$socket = fsockopen($target_host,2082);

if (!$socket) {

echo "$errstr ($errno)<br />\n";

} else {

// fputs($socket,"POST /frontend/$cpanel_theme/backup/dofullbackup.html?dest=ftp&email=$email&server=$ftp_host&user=$ftp_user&pass=$ftp_pass&submit=Generate Backup\r\n");

fputs($socket,"POST /getsqlbackup/DBname.gz?dest=ftp&email=$email&server=$ftp_host&user=$ftp_user&pass=$ftp_pass\r\n");

fputs($socket,"HTTP/1.0\r\n");

fputs($socket,"Host: $target_host\r\n");

fputs($socket,"Authorization: Basic $authentication \r\n");

fputs($socket,"Connection: close\r\n\r\n");

 

while (!feof($socket)) {

$request = fgets($socket, 4096);

}

echo 'Done.';

fclose($socket);

}

?>

Share this post


Link to post
Share on other sites

i tried the script above and all i seem to get is a blank screen after executing the php script via the browser by loading the page. i am using the default skin x and all my passwords and login information is correct does anyone have any ideas on what may be wrong or what i am doing wrong.

thanks in advance :D

Share this post


Link to post
Share on other sites

The script is meant to be run under a cron, so it doesn't have any output unless you turn debug on, or it produces an error.

 

You should have received an email, however, at your $notifyemail address if you defined it, from the cpanel backup process.

Share this post


Link to post
Share on other sites

just in case anyone is interested in doing an automated backup to their pc, this site has a nifty little batch file for windows users:

 

http://www.absoblogginlutely.net/mtblogarchive/003217.php

 

it can be set to do all cpanel backups or just databases.

 

could be set up via your Windows scheduled jobs for ease of use when not around.

 

the author is very helpful with any queries!

Share this post


Link to post
Share on other sites
as a side question: I'm guessing this won't do a backup of my MT SQL database right? Do I need another script for that?

FYI, cPanel now does mysql dumps of your databases instead of hotbackups of the mysql directories when you do full backups.

 

So if you do a full backup of your homedir, you will get restorable mysql databases now with it.

Share this post


Link to post
Share on other sites

works fine !

 

1 question : :)

 

If i set cron job in standard mode, and choos option every 6 hours

wil cron be executed at 0:00, 6:00, 12:00 and 18:00 :(

or will cron be executed 6 hours from the moment you set the cron job? :)

and then 6 hours after that and 6 hours after that ? :)

 

 

well if you don't answer i'l know pretty soon any way it's 3:42 and i'm going to bed :)

will see in the morning if cron has done his job @ 6:00 or at 9:42 :)

 

see you all later, will be back to post findings :)

Share this post


Link to post
Share on other sites

It'll run every 6 hours calculated from 0 (midnite).

 

So if you set it to run every 6 hours, it will run at 0600, noon, 1800, and midnite.

Share this post


Link to post
Share on other sites

Mike or anyone,

 

Is there a way to modify this script to tell cpanel to just backup a particular directory instead of a full backup? I have directory that I would like to backup often (like daily or every few days) and do not want to do a full backup as my site is large. I only need a full backup once a month or longer.

 

Would be nice to just backup and zip one directory.

 

Dennis

Share this post


Link to post
Share on other sites

All this script does is basically automate the process of using the cPanel web interface to initiate a backup, so it is therefore limited to what you can do via cPanel.

 

To do individual directories would require a something completey different.

Share this post


Link to post
Share on other sites

I'm using this script, which is awesome BTW, and I'm having some troubles displaying the resulting pages images. They show up as red x's and it appears that the port (2082) isn't being used. Is there a way to change this?

 

Thanks,

MePH

Share this post


Link to post
Share on other sites
It shouldn't be hard to create a script for the individual databases part.  So if people want it I could whip something up for that, too.

Yes please!

Btw, I haven't forgotten about this. I had the flu all last week so I didn't get to doing much of anything I wanted to during that time. :)

 

Once I finish my last minute tax stuff I'll get to it. :blink:

You don't need a script to do this. Just setup individual crons for each database and put this as the task

 

mysqldump -u cpanelusernamename_dbusername -ppassworrd cpanelname_dbname | gzip -c > /home/cpanelname/backups/backup-dbname.sql.gz

 

cpanelusernamename = your login username for cpanel

password = your password for the dabase (note, there is NO space between -p and password)

dbname = the name of your database

 

Basically this just backups up your db and zips it down !

 

I recommend you don't put this backup in your public_html folder

Share this post


Link to post
Share on other sites

Is it possible to backup only the DB and save them on a remote location??

 

The full backup is great, but I need to run it only once a month, while I need to DB to be backuped every 12h :)

 

Thxs in advance

Ludo

Share this post


Link to post
Share on other sites
Is it possible to backup only the DB and save them on a remote location??

Easiest way is to wget the backup from the remote location (obviously, you need to have wget).

 

If the database was called "weblog" for example, you would do something like this:

>wget --http-user=cpaneluser --http-passwd=cpanelpassword https://example.com:2083/getsqlbackup/weblog.gz

 

So if you have scheduling ability at the remote location, just set that up to run every 12 hours. See the wget man page if you need to do different file names to handle multiple copies, or other additional tasks.

Share this post


Link to post
Share on other sites

I have fullbackup.php set up as a daily cron job on a couple of my TCH-hosted sites, just in case I do something stupid - which happens on a fairly regular basis :D

It has been working great for, say, the past 6 months or so, creating a full backup everyday for both sites, and sending me a little email to let me know.

 

Today, after I screwed a file up on one of my sites, I went to download one of these archives to extract a backup copy of that file - only to find that there hasn't been one made for the past three days. Even though the script has been sending me emails for the past 3 days, they contained an error message that I did not detect until today. Here's a copy of the email from today:

backup_error.html

 

This archive, backup-10.13.2004....tar.gz, did not make it to my home directory - and it seems this has been happening since 10-11-2004.

After submitting a ticket with help desk, I was told that they don't help with development issues.

Help?

Share this post


Link to post
Share on other sites

Cron only sends an email on failure - so just for future reference, if you get one, that's a bad thing.

 

As far as your error - it looks like it doesn't have write permissions to the folder you're trying to save to. You might check the permissions and see that they're 666 (I think that should do it. )

 

Hope that helps!

Share this post


Link to post
Share on other sites

Lisa -

Thanks for your help.

The cPanel script is saving to my home directory, which I don't have the ability to modify permissions on!

Any other ideas?

Thanks again

Share this post


Link to post
Share on other sites

The error is coming from the host you are ftping the file to. It appears it doesn't have permission to write the backup file to the remote host anymore. Check the permissions on your backup machine.

Share this post


Link to post
Share on other sites

Dear Mike:

Thanks for your help, too.

The host I am ftp-ing the file to is my own host, the same host that is being backed up. As I stated before, I don't have the ability to modify permissions on my own home directory. Does this mean I need to have the file put into a directory lower down on the tree?

And thanks for writing this cool script, anyways!

Billy

Share this post


Link to post
Share on other sites

Thanx a lot for this script, it was exactly what I was looking for! :thumbup1:

 

I added some code to optimize all tables from all my DBs before the backup is executed. Then I defined the post-variable that specifies the folder of the remote ftp host in which the backup should be put, which was missing.

Share this post


Link to post
Share on other sites

Great script, but I am having the same problem that Billy did, and the answer never seems to have been provided.

 

The ftp home directory permissions do not allow the backup file to be created there. As I do not wish to change the permissions on the home directory, how can the script be modified so as to upload the backup to a specific directory like /backup for instance?

 

Thank you.

Share this post


Link to post
Share on other sites

Any idea why my backup is not working ?

 

Net::FTP=GLOB(0x831400c)>>> STOR backup-2.19.2005_09-26-01_domains.tar.gz

Net::FTP=GLOB(0x831400c)<<< 553 Can't open that file: Permission denied Net::FTP=GLOB(0x831400c)>>> QUIT Net::FTP=GLOB(0x831400c)<<< 221-Goodbye. You uploaded 0 and downloaded 0 kbytes.

Net::FTP=GLOB(0x831400c)<<< 221 Logout.

Share this post


Link to post
Share on other sites

The reason it is not working is because your permissions on your home directory do not allow files to be stored there. I do not recommend getting these changed. The script needs to be modified to allow for storing a backup file on another directory like /home/backup. Any suggestions?

Share this post


Link to post
Share on other sites

I got it working .... It was my FTP username that was not configured right.

 

It would be neat to see some code to save only the last 3 days and any older backup files then to be deleted.

 

Any ideas ?

Share this post


Link to post
Share on other sites
Great script, but I am having the same problem that Billy did, and the answer never seems to have been provided.

 

The ftp home directory permissions do not allow the backup file to be created there. As I do not wish to change the permissions on the home directory, how can the script be modified so as to upload the backup to a specific directory like /backup for instance?

 

Thank you.

have you tried creating a folder in the home directory like

/home/backup/

and change its permissions ?

 

 

Oh yes welcome to the forum all :)

Share this post


Link to post
Share on other sites

Thing is that where can I tell the FTP to wich directory to store the backup ?

 

I created a new FTP account and by default the directory is created under the public_html side of things.

 

With my main FTP account in CPANEL, I have no idea where the default directory is or how can I customize it ?

 

Need help ! :)

Share this post


Link to post
Share on other sites

When you log in with your main FTP account yuu are placed in the root folder of your account. That would be "/home/cpanelname"

Share this post


Link to post
Share on other sites
When you log in with your main FTP account yuu are placed in the root folder of your account.  That would be "/home/cpanelname"

 

I'm trying to back stuff up on my local PC and getting the following email from cron:

/bin/sh: line 1: 30: command not found

 

what is it? and how do I fix it - any ideas?

 

Thank you!

Share this post


Link to post
Share on other sites
What is the cron job you are running?

 

the last one was

7 13 * * * /usr/local/bin/php /home/ibsteam/fullbackup.php

 

the number 30 in the message I figured stands for the minutes in the cron job.

The las message was:

/bin/sh: line 1: 7: command not found

Share this post


Link to post
Share on other sites
the last one was

7 13 * * * /usr/local/bin/php /home/ibsteam/fullbackup.php

 

the number 30 in the message I figured stands for the minutes in the cron job.

The las message was:

/bin/sh: line 1: 7: command not found

 

It looks like you entered the time arguments as part of the command line (made obvious by the fact it's trying to run '7' as a command). Assuming you are using cPanel's cron form to enter your crontab info, the actual command should be "/usr/local/bin/php /home/ibsteam/fullbackup.php" and you should use the other form fields available to enter the five scheduling components (i.e., if you used the Advanced cron form, you'll see 5 fields before the command line field. Enter the 5 items in "7 13 * * *" in each of those fields respectively).

Share this post


Link to post
Share on other sites

the last one was

7 13 * * * /usr/local/bin/php /home/ibsteam/fullbackup.php

 

the number 30 in the message I figured stands for the minutes in the cron job.

The las message was:

/bin/sh: line 1: 7: command not found

 

It looks like you entered the time arguments as part of the command line (made obvious by the fact it's trying to run '7' as a command). Assuming you are using cPanel's cron form to enter your crontab info, the actual command should be "/usr/local/bin/php /home/ibsteam/fullbackup.php" and you should use the other form fields available to enter the five scheduling components (i.e., if you used the Advanced cron form, you'll see 5 fields before the command line field. Enter the 5 items in "7 13 * * *" in each of those fields respectively).

 

Thank you Mike! That's was the problem. Thank you guys a lot for your great support!

Share this post


Link to post
Share on other sites
Great script, but I am having the same problem that Billy did, and the answer never seems to have been provided.

 

The ftp home directory permissions do not allow the backup file to be created there. As I do not wish to change the permissions on the home directory, how can the script be modified so as to upload the backup to a specific directory like /backup for instance?

 

Thank you.

have you tried creating a folder in the home directory like

/home/backup/

and change its permissions ?

 

 

Oh yes welcome to the forum all :)

 

I have created a /home/backup directory and changed the permissions, but the problem is getting the backup script to send the backup file to that directory. I don't know how to do that.

 

Rowan.

Share this post


Link to post
Share on other sites
Can't call method "login" on an undefined value at /usr/local/cpanel/bin/ftpput

 

Hi,

 

This thread is getting long, but I'm getting the above error when I try to use this script. Now to be precise, the script works one time and saves a backup to my ftp server running on a home pc. No errors, everything works. I get all the good confrimation e-mails.

 

But the second time the php file runs as sceduled, I get the above error. I get this on all subsiquent php runs until I reset my ftp server. I've tried two different servers with the exact same behaivior, so I don't believe that is the problem.

 

The router appears to be configured right because the FTP is going through correctly once. Could the php script not be closing something or reseting something on the server that leaves it in an abnormal state?

 

The log on the server looks 100% normal with a "GOOD-BYE" at the end of a successful transmission. I'm kind of at a loss here as this script almost works right.

 

Thanks for any help, and just browsing through the forum here I've learned alot. Very glad I chose Totalchoice to host...

Share this post


Link to post
Share on other sites

I get this error when I look at the dbsender.php script

"Warning: filetype(): open_basedir restriction in effect"

 

This is getting very complacated.

I did the chmod and still get the same error.

Cron job wont allways send e-mails but i dont get the fiel via e-mail eather.

Very lost here.

Share this post


Link to post
Share on other sites

Welcome to the forums brockhosting. ;)

 

Have you checked your default email with webmail? Sometimes the errors get sent there.

Share this post


Link to post
Share on other sites
Can't call method "login" on an undefined value at /usr/local/cpanel/bin/ftpput

 

Hi,

 

This thread is getting long, but I'm getting the above error when I try to use this script. Now to be precise, the script works one time and saves a backup to my ftp server running on a home pc. No errors, everything works. I get all the good confrimation e-mails.

 

But the second time the php file runs as sceduled, I get the above error. I get this on all subsiquent php runs until I reset my ftp server. I've tried two different servers with the exact same behaivior, so I don't believe that is the problem.

 

The router appears to be configured right because the FTP is going through correctly once. Could the php script not be closing something or reseting something on the server that leaves it in an abnormal state?

 

The log on the server looks 100% normal with a "GOOD-BYE" at the end of a successful transmission. I'm kind of at a loss here as this script almost works right.

 

Thanks for any help, and just browsing through the forum here I've learned alot. Very glad I chose Totalchoice to host...

 

 

All right. I don't have a ton of info to post here, but in the interest of trying to be helpful to everyone, just a quick update. I did nothing to resolve the problem, but the script started working correctly. Then it stopped. Basically, i'd say 75% of the time it works, which isn't great really, but is better than nothing. The other 75% of the time I get the error.

 

I haven't had a chance to really go through the code, and I'm no expert, but my thought is that 1 of 2 things may be going on.

 

1- my belkin router is the problem. This is clearly not a professional high quality router, but it is new and seems to work properly. Perhaps occasionally the router doesn't handle the FTP correctly, or fast enough, or something.

 

2- The script may be at fault. Perhaps it's written properly and should work "on paper" but in practice there is a better way to write the FTP portion that is more universally used.

 

I'm not trying to knock the script for no reason! It really is a huge help and for all I know it isn't the problem at all, I'm just conjecturing here. If someone who knows the code better than I do tells me I'm totally off base, excellent, we'll have ruled out one of my ideas!

Share this post


Link to post
Share on other sites
I have created a /home/backup directory and changed the permissions, but the problem is getting the backup script to send the backup file to that directory. I don't know how to do that.

 

I never added a variable for remote directory. I'll try to add that tonight and repost the script.

 

1- my belkin router is the problem. This is clearly not a professional high quality router, but it is new and seems to work properly. Perhaps occasionally the router doesn't handle the FTP correctly, or fast enough, or something.

 

2- The script may be at fault. Perhaps it's written properly and should work "on paper" but in practice there is a better way to write the FTP portion that is more universally used.

 

Try running backups manually via cPanel and see if it sometimes fails. The above script itself doesn't do the backup nor the FTP process. All it does it automate calling the cPanel process that does (basically does a form submit). If you are still having the problem running them manually from cPanel, that will help isolate where they problem might lie.

 

However, cPanel added a "Download Today's Backup" function since I wrote this script, so a simpler way to get your daily backups if you have something like wget on the remote machine is simply to wget the file every day in cron.

 

An example cron entry (all one line):

>0 3 * * * wget -q --http-user="youraccount" --http-password="yourpassword" -O /home/backup/dailybackup.tar.gz https://******:2083/getbackup/

 

Just change "/home/backup/dailybackup.tar.gz" to the directory and filename you want.

 

Note that this would be put as a cron on the remote machine, not under your cPanel account, as the machine running the wget will be receiving the file.

 

You can replace wget with any program (using it's appropriate parameters) that would retrieve and save a URL target.

Share this post


Link to post
Share on other sites

I got this to work from one server to another. So it should be fine for off site backups.

Now could some one tell me how I can get this script to "go off" every 6 hours with the cron system?

I keep getting a 404 error.

><?
$user_name = "you_user_name_here";
$user_pass = "your_pass_word_here";
$user_website = "www.your_full_domain_name_here.com";

$db_list = array("phpbb1","rface");
// "Backthisup", "backthisup2"
$looper = 0;

// Do not edit below this line
$dc = time();
while($db_list[$looper] != null) {
$db_name = $db_list[$looper];
$get_this = "http://".$user_name.":".$user_pass."@".$user_website.":2082/getsqlbackup/".$db_name.".gz";
echo "<br>Atempting to downlaod $db_name";
$handle = fopen($get_this, "r");
$save_as = "backup_".$db_name."_".$dc."_.gz";
if ($handle != null) {
$local_handle = fopen($save_as,"x");
echo "<br>Saveing $db_name as $save_as <br>";
while(!feof($handle))
{
  fwrite($local_handle,fgets($handle));
}
}
fclose($handle);
fclose($local_handle);
$looper++;
}
?>

Share this post


Link to post
Share on other sites

Where are you getting a 404 error? From the cron job? If so, you do not have the correct path to the script in the cron job.

 

It should be something like this

>/usr/bin/php -q /home/cpanelname/path-to-script

Share this post


Link to post
Share on other sites

php /home/brockho/auto_backup/my_backup_system.php

 

Thats what I have now and it works. :)

Now to try to get it to mail it to me every night.

This fourm took a long time to read but it helped a LOT thanks.

Share this post


Link to post
Share on other sites

By default cron jobs do not send an email unless there is an error so you will have to write it into your backup script to send you email.

Share this post


Link to post
Share on other sites

This is fast and easy.

I'll post it even though i'm not a TCH client any more because this thread got me to write this script.

Should work great for server to server backups because its FTP.

This can be set up as a one touch backup system or threw a cron job.

><?
// A script produced by BrockHosting.com
// For more help visit http://www.brockhosting.com/bb/viewtopic.php?p=13
$user_name = "not_root"; // YOUR cpanel user name

$user_pass = "password_here"; // YOUR cpanel password
$user_website = "www.brockhosting.com"; // YOUR website do NOT include http://

$ftp_server_ip = "1.2.3.4"; // Server IP here
$ftp_server_user = $user_name."_ftp"; // FTP login name here
$ftp_server_pass = "Hd3Z6wpf"; // FTP password here


$db_list = array("phpbb1","ptp","rcash"); // List of your databases
// Must be "database1","Database2"
// DO NOT ENTER "username_database1", "username_database2"


// Do not edit below this line
$dc = time();
$looper = 0;
while($db_list[$looper] != null) {
 $db_name = $db_list[$looper];
 $get_this = "http://".$user_name.":".$user_pass."@".$user_website.":2082/getsqlbackup/".$db_name.".gz";
 echo "\n<br><br>\nAtempting to downlaod $db_name";
 $handle = fopen($get_this, "r");
  $save_as = "backup_".$db_name."_".$dc."_.gz";
 if ($handle != null) {
   $local_handle = fopen($save_as,"x");
    echo "\n<br>Saveing \"$db_name\" as \"$save_as\" ";
 while(!feof($handle)){ fwrite($local_handle,fgets($handle)); }
 }
 fclose($handle);
 fclose($local_handle);
 // Got file need ftp home
 $ftp_conn_id = ftp_connect($ftp_server_ip) or die("Couldn't connect to $ftp_server_ip");
 ftp_login($ftp_conn_id,$ftp_server_user,$ftp_server_pass);
 $upload = ftp_put($ftp_conn_id, $save_as, $save_as, FTP_BINARY);
 if ($upload != null) {
 echo "\n<br>File Sent!\n";
 unlink($save_as);
 } else { 
 echo "\n<br>File NOT SENT!\n";
 }

 $looper++;
}
?>

Share this post


Link to post
Share on other sites

Hello,

 

I'm new to the forum and actually I found here and TCH through google when searching for an automated backup. My question is will that be possible to write a similar code to the one discussed in the topic for simulating the act of uploading a gziped backup? I mean a backup which is already made by cpanel is stored somewhere on the web and the php script reads that and simulates the uploading of it to cpanel for restoration.

Edited by johny_gjx

Share this post


Link to post
Share on other sites

Welcome to the forums, johny_gjx :huh:

 

Sorry, I don't know if that's possible or not.

Share this post


Link to post
Share on other sites

Welcome to the forums, johny_gjx! :thumbup1:

 

Is it possible? Sort of. 'Simulating' the upload isn't really useful, as the script would have to upload the entire file anyway in order to read it. Such a script should just retrieve the backup file from the remote server and save it locally so it can then be restored.

Share this post


Link to post
Share on other sites

Thank you David and Thomas,

 

David is right about the fact that it would be better if the script could retrieve the backup from the remote machine, but just in order to make things simpler we can do the transfer by SST:

 

http://www.jazarsoft.com/product.php?pid=0090040006

 

so perhaps if a php coder is going to help, things will be simpler for him, or maybe someone can combine the two pieces of code and make a perfect solution that does the transfer as well ;)

Share this post


Link to post
Share on other sites
Welcome to the forums, johny_gjx ;)

 

Great script, now I have to get it to work,

 

this is the error which i get,

>/bin/sh: line 1: /usr/local/bin/php/home/klei0404/fullbackup.php: Not a directory

 

if i take out the first part, and have only /home/klei0404/fullbackup.php, which is the absolute path to the this script.

 

i get (only part of the error)

>home/klei0404/fullbackup.php: line 1: ?php
: No such file or directory
/home/klei0404/fullbackup.php: line 2: 
: command not found
/home/klei0404/fullbackup.php: line 3: //: is a directory
/home/klei0404/fullbackup.php: line 4: //: is a directory
/home/klei0404/fullbackup.php: line 5: //: is a directory

 

any suggestions?

Thanks

Jasmon

Share this post


Link to post
Share on other sites

Welcome to the forums Jasmon ;)

 

Try:

>/usr/bin/php -q /home/klei0404/fullbackup.php

 

This is assuming that the fullbackup.php file resides above your public_html folder. Otherwise enter the full path to the script.

 

 

Edit: changed path to php

Share this post


Link to post
Share on other sites
Hello jasmon,

 

send me a message with the script and I will be happy to look at it for you.

 

great,

 

actually, i'm using the script mentioned in this thread (reply # 4)

 

I just copied and pasted into a new document which I uploaded to the server.

 

First I will give it a new shot!

Share this post


Link to post
Share on other sites

I've been using this script for some time now. What I've found is that the files generated by this script are evidently not owned by my account. When they're created they have permissions of 600 and I'm not able to chmod them or to ftp them down to my pc.

 

Anyone have any idea why this would be so? (I'm using my cpanel id/pwd in the script of course).

 

Thanks,

John

Share this post


Link to post
Share on other sites

From what I understand, it should not be happening. I'd be curious to know what user owns the files, what user owns the directory containing the files, and the permissions on that directory.

Share this post


Link to post
Share on other sites
From what I understand, it should not be happening. I'd be curious to know what user owns the files, what user owns the directory containing the files, and the permissions on that directory.

 

Well, here's what my FTP tool's dirinfo cmd reveals:

 

drwxr-xr-x 2 stblogs stblogs 4096 Jul 26 19:06 albums

-rwx------ 1 stblogs stblogs 21598 Jul 18 17:28 automysqlbackup.sh

-rw------- 1 root wheel 28865662 Sep 18 05:01 backup-9.18.2005_01-00-07_stblogs.tar.gz

-rw------- 1 root wheel 30227104 Sep 25 05:02 backup-9.25.2005_01-00-24_stblogs.tar.gz

drwxrwxrwx 5 stblogs stblogs 4096 Sep 26 05:00 backups

-rw-r--r-- 1 stblogs stblogs 119 Jul 29 21:03 error_log

drwxr-x--- 3 stblogs (?) 4096 Jul 17 15:33 etc

-rwx------ 1 stblogs stblogs 2284 Sep 13 19:09 fullbackup.php

drwx------ 2 stblogs stblogs 4096 Aug 30 13:17 logs

drwxrwx--- 3 stblogs (?) 4096 Sep 27 01:10 mail

drwxr-x--- 3 stblogs stblogs 4096 Jan 24 2005 public_ftp

drwxr-x--- 13 stblogs (?) 4096 Sep 26 01:32 public_html

-rwxr--r-- 1 stblogs stblogs 27 Feb 16 2005 robots.txt

drwx------ 6 stblogs stblogs 4096 Jan 26 2005 tmp

lrwxrwxrwx 1 root (?) 11 Jan 24 2005 www -> public_html

 

here's the script:

 

<?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!

 

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

 

// Info required for cPanel access

$cpuser = "********"; // Username used to login to CPanel

$cppass = "********"; // Password used to login to CPanel

$domain = "stblogsparish.com"; // Domain name where CPanel is run

$skin = "bluelagoon"; // Set to cPanel skin you use (script won't work if it doesn't match)

 

// Info required for saving locally

$savedir = "/home/stblogs/"; //Save to this directory

 

// Info required for FTP host

$ftpuser = "********"; // Username for FTP account

$ftppass = "********"; // Password for FTP account

$ftphost = "xxx.xxx.xxx.xxx"; // Full hostname or IP address for FTP host

$ftpmode = "ftp"; // FTP mode ("ftp" for active, "passiveftp" for passive)

 

// Notification information

$notifyemail = "myemailaddresshere"; // Email address to send results

 

// Secure or non-secure mode

$secure = 0; // 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... Bailing out!\n"; exit; }

 

// Encode authentication string

$authstr = $cpuser.":".$cppass;

$pass = base64_encode($authstr);

 

//$params = "dest=$ftpmode&email=$notifyemail&server=$ftphost&user=$ftpuser&pass=$ftppass&submit=Generate Backup";

$params = "dest=$savedir&email=$notifyemail&user=$ftpuser&pass=$ftppass&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);

 

?>

 

and here's the cron command line:

 

0 01 * * 7 /usr/bin/php /home/stblogs/fullbackup.php

 

 

Think I need to ticket this or could it be something I'm doing?

Share this post


Link to post
Share on other sites
// Info required for saving locally

$savedir = "/home/stblogs/"; //Save to this directory

 

This should be (I believe):

 

// Info required for saving locally

$savedir = "/home/your-TCH-cpanel-name/stblogs/"; //Save to this directory

Share this post


Link to post
Share on other sites

'stblogs' is the cpanel name. I believe the backups should be going into the 'backups' subdirectory, which has been set up with 0777 permissions presumably for this purpose:

>// Info required for saving locally
$savedir = "/home/stblogs/backups/"; //Save to this directory

Share this post


Link to post
Share on other sites
$skin = "bluelagoon"; // Set to cPanel skin you use (script won't work if it doesn't match)

I don't think anyone has the bluelagoon theme anymore

I think it should be x2 ?

Share this post


Link to post
Share on other sites
'stblogs' is the cpanel name. I believe the backups should be going into the 'backups' subdirectory, which has been set up with 0777 permissions presumably for this purpose:

>// Info required for saving locally
$savedir = "/home/stblogs/backups/"; //Save to this directory

 

Thanks David, I knew I was incorrect. :thumbup1:

Share this post


Link to post
Share on other sites