NetBuilders

Members in Chat:

You are welcome to look around. You will have to register before you can post a message, create a blog, chat live with our members, or add a site to our directory.



Reply Mishre Directory
Old 3 July, 2009, 19:57 PM   #1 (permalink)
Great Proxy Hosting - MyProxyHosting.com
 
Zash's Avatar
 
Location: New York, USA
Blog Entries: 1
Thanked 109 Times in 85 Posts
Posts: 1,078
$NetBucks: 928
Join Date: Feb 2009
Last Online: Yesterday 20:48 PM
Send a message via MSN to Zash
Default Database Backup (to Email and FTP) via Cron

I did not write this tutorial. I found it very useful and am re-posting it for your convenience. Original: Database Backup via Cron

This tutorial allows you to backup your MySQL database, and then send it to you either via email or via FTP. You will need to set up a cron for the files below, and specify a time/date (IE Mondays at 4pm) and the file will run a backup at the time/day you specify.

SQL Backup - Email To You
Code:
<?
$datestamp = date("Y-m-d");      // Current date to append to filename of backup file in format of YYYY-MM-DD

/* CONFIGURE THE FOLLOWING SEVEN VARIABLES TO MATCH YOUR SETUP */
$dbuser = "";            // Database username
$dbpwd = "";            // Database password
$dbname = "";            // Database name. Use --all-databases if you have more than one
$filename= "backup-$datestamp.sql.gz";   // The name (and optionally path) of the dump file
$to = "you@remotesite.com";      // Email address to send dump file to
$from = "you@yourhost.com";      // Email address message will show as coming from.
$subject = "MySQL backup file";      // Subject of email

$command = "mysqldump -u $dbuser --password=$dbpwd $dbname | gzip > $filename";
$result = passthru($command);

$attachmentname = array_pop(explode("/", $filename));   // If a path was included, strip it out for the attachment name

$message = "Compressed database backup file $attachmentname attached.";
$mime_boundary = "<<<:" . md5(time());
$data = chunk_split(base64_encode(implode("", file($filename))));

$headers = "From: $from\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-type: multipart/mixed;\r\n";
$headers .= " boundary=\"".$mime_boundary."\"\r\n";

$content = "This is a multi-part message in MIME format.\r\n\r\n";
$content.= "--".$mime_boundary."\r\n";
$content.= "Content-Type: text/plain; charset=\"iso-8859-1\"\r\n";
$content.= "Content-Transfer-Encoding: 7bit\r\n\r\n";
$content.= $message."\r\n";
$content.= "--".$mime_boundary."\r\n";
$content.= "Content-Disposition: attachment;\r\n";
$content.= "Content-Type: Application/Octet-Stream; name=\"$attachmentname\"\r\n";
$content.= "Content-Transfer-Encoding: base64\r\n\r\n";
$content.= $data."\r\n";
$content.= "--" . $mime_boundary . "\r\n";

mail($to, $subject, $content, $headers);

unlink($filename);   //delete the backup file from the server
?>
SQL Backup - FTP'ed To You
Code:
<?
$datestamp = date("Y-m-d");      // Current date to append to filename of backup file in format of YYYY-MM-DD

/* CONFIGURE THE FOLLOWING THREE VARIABLES TO MATCH YOUR SETUP */
$dbuser = "";      // Database username
$dbpwd = "";         // Database password
$dbname = "";      // Database name. Use --all-databases if you have more than one
$filename= "backup-$datestamp.sql.gz";   // The name (and optionally path) of the dump file

$command = "mysqldump -u $dbuser --password=$dbpwd $dbname | gzip > $filename";
$result = passthru($command);

/* CONFIGURE THE FOLLOWING FOUR VARIABLES TO MATCH YOUR FTP SETUP */
$ftp_server = "";   // Shouldn't have any trailing slashes and shouldn't be prefixed with ftp://
$ftp_port = "21";            // FTP port - blank defaults to port 21
$ftp_username = "anonymous";         // FTP account username
$ftp_password = "";         // FTP account password - blank for anonymous

// set up basic connection
$ftp_conn = ftp_connect($ftp_server);

// Turn PASV mode on or off
ftp_pasv($ftp_conn, false);

// login with username and password
$login_result = ftp_login($ftp_conn, $ftp_username, $ftp_password);

// check connection
if ((!$ftp_conn) || (!$login_result))
{
   echo "FTP connection has failed.";
   echo "Attempted to connect to $ftp_server for user $ftp_username";
   exit;
}
else
{
   echo "Connected to $ftp_server, for user $ftp_username";
}

// upload the file
$upload = ftp_put($ftp_conn, $filename, $filename, FTP_BINARY);

// check upload status
if (!$upload)
{
   echo "FTP upload has failed.";
}
else
{
   echo "Uploaded $filename to $ftp_server.";
}

// close the FTP stream
ftp_close($ftp_conn);

unlink($filename);   //delete the backup file from the server
?>
You need to fill in all the variables and then save the script as .php and upload it to your site via FTP. You need to give it 755 permissions.

Then in cron add this code for the cron job:
Code:
php -q /home/username/public_html/folder/name.php
You need to edit this so it points to where you uploaded the script.
__________________
â–ˆ InfinitumHost - High Quality Unlimited Hosting
â–ˆ Unlimited Web Hosting with cPanel and Softaculous
â–ˆ Unlimited Reseller Hosting with FREE WHMCS!
â–ˆ www.infinitumhost.com
  Reply With Quote
Thanked by:
Mr.Bill (3 July, 2009), Nick (3 July, 2009)
Old 3 July, 2009, 20:04 PM   #2 (permalink)
Great Proxy Hosting - MyProxyHosting.com
 
Zash's Avatar
 
Location: New York, USA
Blog Entries: 1
Thanked 109 Times in 85 Posts
Posts: 1,078
$NetBucks: 928
Join Date: Feb 2009
Last Online: Yesterday 20:48 PM
Send a message via MSN to Zash
Default

Hmm, actually I just tried the via email thing, and it is sending me empty databases only. Can anybody here help me out?
__________________
â–ˆ InfinitumHost - High Quality Unlimited Hosting
â–ˆ Unlimited Web Hosting with cPanel and Softaculous
â–ˆ Unlimited Reseller Hosting with FREE WHMCS!
â–ˆ www.infinitumhost.com
  Reply With Quote
Old 3 July, 2009, 23:55 PM   #3 (permalink)
Newbie Net Builder
 
Nick's Avatar
 
Location: Tampa, FL
Thanked 16 Times in 14 Posts
Posts: 77
Recent Blog: advertising
$NetBucks: 102
Join Date: Jul 2009
Last Online: 11 April, 2010 20:26 PM
Default

This seems very useful. I will definitely try it out.
  Reply With Quote
Old 4 July, 2009, 01:07 AM   #4 (permalink)
Catch Me If you Can....
 
deluxdon's Avatar
 
Location: Deluxdon.In
Blog Entries: 1
Thanked 52 Times in 47 Posts
Posts: 459
$NetBucks: 987
Join Date: Jun 2009
Last Online: Today 06:25 AM
Default

Quote:
Originally Posted by Nick View Post
This seems very useful. I will definitely try it out.
Check just above post of OP that its not working (empty backup through email).

I am using same thing through plugin at my wp driven blogs (mysql backup direct to your email using schedule backup option).

DON.
  Reply With Quote
Old 4 July, 2009, 01:13 AM   #5 (permalink)
Newbie Net Builder
 
Nick's Avatar
 
Location: Tampa, FL
Thanked 16 Times in 14 Posts
Posts: 77
Recent Blog: advertising
$NetBucks: 102
Join Date: Jul 2009
Last Online: 11 April, 2010 20:26 PM
Default

Quote:
Originally Posted by deluxdon View Post
Check just above post of OP that its not working (empty backup through email).

I am using same thing through plugin at my wp driven blogs (mysql backup direct to your email using schedule backup option).

DON.
Until I try it for myself, I don't know. Although highly doubtful, he may not have set it up properly.
  Reply With Quote
Old 4 July, 2009, 01:22 AM   #6 (permalink)
Great Proxy Hosting - MyProxyHosting.com
 
Zash's Avatar
 
Location: New York, USA
Blog Entries: 1
Thanked 109 Times in 85 Posts
Posts: 1,078
$NetBucks: 928
Join Date: Feb 2009
Last Online: Yesterday 20:48 PM
Send a message via MSN to Zash
Default

That's true, and I haven't tried the FTP one. I'm pretty sure the FTP one works, and maybe the email if I set it up wrong...
__________________
â–ˆ InfinitumHost - High Quality Unlimited Hosting
â–ˆ Unlimited Web Hosting with cPanel and Softaculous
â–ˆ Unlimited Reseller Hosting with FREE WHMCS!
â–ˆ www.infinitumhost.com
  Reply With Quote
Old 4 July, 2009, 04:41 AM   #7 (permalink)
Newbie Net Builder
 
Thanked 6 Times in 2 Posts
Posts: 32
$NetBucks: 18
Join Date: Jun 2009
Last Online: 12 April, 2010 00:14 AM
Default

Is there a similar script that will backup the website as well? Not just the MySql, but all files etc..?

Or maybe a desktop software?
  Reply With Quote
Old 4 July, 2009, 13:55 PM   #8 (permalink)
Über Moderator
 
TopDogger's Avatar
 
Thanked 339 Times in 222 Posts
Posts: 775
$NetBucks: 1,239
Join Date: Jan 2009
Last Online: Yesterday 23:24 PM
Default

This script and variations of it have been around for several years. I found that you will get a blank SQL file if the permissions are not set right for the database user.

Try this. You need the following permissions: SELECT, RELOAD, FILE, SHOW DATABASES, SUPER, LOCK TABLES, SHOW VIEW

It is a good idea to set up a separate DB user for backups. These are not standard settings.

One of the basic security rules for DB user permissions is that you never grant anything more than the minimal requirements to any DB user.

There may be other reasons for a blank file, but this is what fixed my backups when I first encountered the problem.

Another problem you might run into is with e-mailing large databases. Some ISPs place limits on e-mail file sizes. The --all-databases option may not work if the databases are large. I've also seen problem trying to e-mail WordPress backups if you have the wassup plugin installed and you do not set time limits on the data it retains. By default, wassup saves all the history and the tables can become very large very quickly.




__________________
You can have it fast, good or cheap. Pick any two.
Phoenix Managed Services :: Free Car Shipping Quotes
  Reply With Quote
Old 5 July, 2009, 06:19 AM   #9 (permalink)
Catch Me If you Can....
 
deluxdon's Avatar
 
Location: Deluxdon.In
Blog Entries: 1
Thanked 52 Times in 47 Posts
Posts: 459
$NetBucks: 987
Join Date: Jun 2009
Last Online: Today 06:25 AM
Default

Quote:
Originally Posted by ebizlaunch View Post
Is there a similar script that will backup the website as well? Not just the MySql, but all files etc..?

Or maybe a desktop software?
You can take full website backup (all files) through cpanel back up option.

DON.
  Reply With Quote
Old 6 July, 2009, 04:28 AM   #10 (permalink)
Newbie Net Builder
 
Thanked 6 Times in 2 Posts
Posts: 32
$NetBucks: 18
Join Date: Jun 2009
Last Online: 12 April, 2010 00:14 AM
Default

Quote:
Originally Posted by deluxdon View Post
You can take full website backup (all files) through cpanel back up option.

DON.
Agreed. The problem is that

1) I have to login to the cpanel
2) Click on the backup icons
3) save these in a separate directory.

Simple enough for one site.

When you have multiple sites, on different servers, need to take periodic backups (weekly) - this becomes a chore and a time-waster.

I'm looking to automate this; and no, I'm not a tech geek, so wouldn't be able to rsync or write a script to do this.

I'm the kind of guy who thinks that a con job is a cron job with the R silent

Still looking...
  Reply With Quote
Reply

Bookmarks

Tags
backup, cron, database, email, ftp

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Safe backup plug in thesyndicate Wordpress 10 1 September, 2009 19:32 PM
Always save a backup! sam Managing 7 10 August, 2009 08:51 AM
Get tables name from DataBase [vb] ghadeer Databases 7 16 May, 2009 14:54 PM
Backup of Mysql database using PhpMyAdmin mega Databases 2 14 May, 2009 09:06 AM
who you can get (daily , weekly, monthly) auto backup ghadeer Managing 1 14 May, 2009 09:03 AM


All times are GMT. The time now is 10:55 AM.
Powered by vBulletin® Version 3.8.6
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
SEO by vBSEO
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios