Jump to content

Recommended Posts

Posted

I found a php script to email me a backup of my mysql database. When I uploaded it and tried to run it, I got the following error

 

Warning: system() has been disabled for security reasons

 

Is there anyway to enable this?

 

Thanks,

Alex

Posted

Sorry, system() is disabled for security reasons. If we allowed system(), anybody could hack our servers and do something bad.

 

Try looking for a workaround. ;)

Guest Geoff
Posted

Why don't you backup via cpanel?

Posted

Thanks for the reply!

 

Ya, I will probably end up just backing it up through cpanel. I was looking for some way to make scheduled automatic backups though, and I don't believe you can do automatic backups through cpanel.

 

Posted

Yes auto backups has been disabled at the cpanel.

 

Why don't you post your script and I'm sure somebody can show how to get around the system() function. If not I can show you how to get the source of the database gz file through curl and you can do whatever with the source you want.

 

 

Posted
Yes auto backups has been disabled at the cpanel.

 

Why don't you post your script and I'm sure somebody can show how to get around the system() function. If not I can show you how to get the source of the database gz file through curl and you can do whatever with the source you want.

 

Here is the script. It was taken straight from http://www.theblog.ca/mysql-email-backup.

 

I posted it here on my website.

 

<?php
// Create the mysql backup file
// edit this section
$dbhost = "yourhost"; // usually localhost
$dbuser = "yourusername";
$dbpass = "yourpassword";
$dbname = "yourdb";
$sendto = "Webmaster <webmaster@yourdomain.com>";
$sendfrom = "Automated Backup <backup@yourdomain.com>";
$sendsubject = "Daily Mysql Backup";
$bodyofemail = "Here is the daily backup.";
// don't need to edit below this section

$backupfile = $dbname . date("Y-m-d") . '.sql';
system("mysqldump -h $dbhost -u $dbuser -p$dbpass $dbname > $backupfile");

// Mail the file

    include('Mail.php');
    include('Mail/mime.php');

    $message = new Mail_mime();
    $text = "$bodyofemail";
    $message->setTXTBody($text);
    $message->AddAttachment($backupfile);
        $body = $message->get();
        $extraheaders = array("From"=>"$sendfrom", "Subject"=>"$sendsubject");
        $headers = $message->headers($extraheaders);
    $mail = Mail::factory("mail");
    $mail->send("$sendto", $headers, $body);

// Delete the file from your server
unlink($backupfile);
?>

 

Thanks in advance,

Alex

Posted

I can't say that this works for certain because I don't have a large database to try it with. Give it a try on your site and when it emails the attachment to you make sure you open it up to see that everything is there. Als I'm sure some of the curl options aren't needed but they're not hurting anything either. :)

 

Either download the text file or copy the script below:

 

http://heliohost.uni.cc/auto_mysql_backup.txt

 

<?php
# Edit email info
$to = "Byron <byron@gmail.com>";
$from = "Automated Backup <byron@byron.heliohost.org>";
$subject = "Mysql Backup";
$message = "Your Mysql GZIP Backup";

# Edit cpanel backup info 
# login is cpanel login username and password
$user_name = "byron";
$user_pass = "********";
$database = "byron_test";
$domain = "byron.heliohost.org";
$url = "http://$domain:2082/getsqlbackup/$database.sql.gz";
$agent = "Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)";
$cook_file = "curl_login_cookie.txt";

# get file extension
$extract = pathinfo($url);
$fname = ($extract['basename']);

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_REFERER, "http://$domain");
curl_setopt($ch, CURLOPT_USERAGENT, $agent); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_UNRESTRICTED_AUTH, true);
curl_setopt($ch, CURLOPT_USERPWD, "$user_name:$user_pass");
curl_setopt($ch, CURLOPT_COOKIEFILE, $cook_file);
curl_setopt($ch, CURLOPT_COOKIEJAR, $cook_file);

$result = curl_exec($ch);
$extract = curl_getinfo($ch);
$httpcode = $extract['http_code'];
curl_close($ch);
if ($httpcode >= 200 && $httpcode < 303) {

    ####### Email gzip attachment #######
    $content = chunk_split(base64_encode($result));
    $uid = md5(uniqid(time()));
    $header = "From: $from\r\n";
    $header .= "MIME-Version: 1.0\r\n";
    $header .= "Content-Type: multipart/mixed; boundary=\"".$uid."\"\r\n\r\n";
    $header .= "This is a multi-part message in MIME format.\r\n";
    $header .= "--".$uid."\r\n";
    $header .= "Content-type:text/plain; charset=iso-8859-1\r\n";
    $header .= "Content-Transfer-Encoding: 7bit\r\n\r\n";
    $header .= $message."\r\n\r\n";
    $header .= "--".$uid."\r\n";
    $header .= "Content-Type: application/x-gzip; name=\"".$fname."\"\r\n";
    $header .= "Content-Transfer-Encoding: base64\r\n";
    $header .= "Content-Disposition: attachment; filename=\"".$fname."\"\r\n\r\n";
    $header .= $content."\r\n\r\n";
    $header .= "--".$uid."--";
    mail($to, $subject, $message, $header);
    ####### End email attachment ########

} else {
echo "cPanel Login Failed!";
}

# delete cookie file
if (file_exists($cook_file)) {
unlink($cook_file);
}
?>

Posted

Thanks for posting that. Everything appears to be working when I run it (It doesn't return an errors and I receive the email with the attached .sql.gz file), but it seems like something is going wrong. When i tried to open it (with jzip and 7zip on 2 different PCs), I received an error. The .sql.gz file outputted by manually going to myPhp admin and clicking export opens fine, so I don't think it is a software problem. I tried importing the .sql.gz file using myPHP admin, but I got an error

 

Interestingly, if I change the .sql.gz to .php and open with the browser I get a fully readable Log in to cpanel page. See attachment below

 

Thanks again,

Alex

Posted

I'm sorry I should have stated that the:

 

$user_name

$user_pass

 

variables were cPanel username and password and not your database username and password. Your getting that login page because login failed. I should have added a check to verify login. I've edited my code to check for login now. Copy the code again and edit for your site and try it again.

 

 

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...