Jump to content

BackUp and email DB using PHP


willpower

Recommended Posts

Hi

 

I have a DB which needs to be backed up regularly.  It is on a shared server.  I need to write a php script that a)backs up the DB and b) kindly emails me the backed up file.  I would be happy with a non compressed .sql (text) file.

 

Kinda thought this bit would be easy...but so far I cant even find out where to start.  Any help, guidance or similar scripts greatfully appreciated.

 

Thanks

 

Will

Link to comment
Share on other sites

A very curt remark....I believe that the link to the tutorial would have been acceptable without the comment.  Perhaps you are a school teacher?

 

I thank you sincerely for the resource but not for your opinion...opinions are like your arse holes...everyone has one.

Link to comment
Share on other sites

A very curt remark....I believe that the link to the tutorial would have been acceptable without the comment.  Perhaps you are a school teacher?

 

I thank you sincerely for the resource but not for your opinion...opinions are like your arse holes...everyone has one.

 

I am 16, and no my comment was nothing just some basic facts of the community. What you asked for had no script along with it, meaning we have to write the whole thing for you then post it.

 

 

your welcome btw.

 

Link to comment
Share on other sites

Well at 16 perhaps you have some social skills left to develop.

 

"Kinda thought this bit would be easy...but so far I cant even find out where to start.  Any help, guidance or similar scripts greatfully appreciated."

 

At what point did I ask you to write the script for me?

 

I believe that I requested what you kindly provided.

 

 

Link to comment
Share on other sites

He was asking for a starting point, just a url to a tutorial on that area would of been enough, I'm sorry I found that comment was a little harsh myself.

 

Though there was absolutely no need to start slinging insults back either if someone says something you do not like, remember he did you you the url, he could of easily left that out, if you found his comment insulting you could of messaged a moderator. This is not the place to sling insult.

 

and you also have social skills to develop so just leave it before a moderator bans you

 

Yes this forum is to help people learn, and that person needs to at least put an effort into trying to write something, but just asking for a start point isn't so bad is it?

 

 

Link to comment
Share on other sites

OK..so i can grow up a little myself.  I merely take exception to the fact that I specifically did not ask for a script to be written.

 

I have been a member for some time now and i am very well aware of the 'basic facts of this community'

 

I respect this community and enjoy its services and benefits wholeheartedly and help as much as I can with my limited knowledge.

 

 

I apologise if I offend with my comments or indeed in the defence of my posts.

 

NOW

 

Back to the issue in hand.

 

The tutorial which was suggested has been useful.  I indeed have now created a file which should create a backup, however the following occurs.

 

All tables are present, the number of lines are present, the data is missing.  Can anyone shed some light as to the accuracy or issues surrounding this piece of code.

 

$query = mysql_query("select * from $table");

    $num_fields = @mysql_num_fields($query);

    $numrow = mysql_num_rows($query);



    for ($i =0; $i<$numrow; $i++) {

      $result .= "INSERT INTO ".$table." VALUES(";

          for($j=0; $j<$num_fields; $j++) {

          $row[$j] = addslashes($row[$j]);

          $row[$j] = ereg_replace("\n","\\n",$row[$j]);

          if (isset($row[$j])) $result .= "\"$row[$j]\"" ; else $result .= "\"\"";

          if ($j<($num_fields-1)) $result .= ",";

         }   

      $result .= ");\n";

     }

     return $result . "\n\n\n";

 

As all values are '' (NULL)

 

Link to comment
Share on other sites

Well at 16 perhaps you have some social skills left to develop.

 

"Kinda thought this bit would be easy...but so far I cant even find out where to start.  Any help, guidance or similar scripts greatfully appreciated."

 

At what point did I ask you to write the script for me?

 

I believe that I requested what you kindly provided.

 

 

 

If thats the case then I appoligize, but i really did not point that out in a rude manner....

 

:P

Link to comment
Share on other sites

http://sourceforge.net/projects/automysqlbackup/

 

Very robust solution. I've used it for years on my dedicated servers. It is written in PERL. If you intended on writing such a robust app from scratch as a project, then this might not be of much use. If you wanted a solution that has already been written, this script is very good, very reliable, and feature rich.

 

The comment was rude, but I sympathize with Sensei on the other hand. We are getting a LOT of requests lately where it's obvious the poster doesn't have the php knowledge to code what he wants, and then the community ends up writing the script for him. Not really what the spirit of this help forum is about, but it goes on nonetheless because of the 'competition' factor between people who give the answers. I've tried many times to direct a poster to the information so they can learn for themselves, only to have another just give the direct code. While that's cool and all, there also is no comment provided with the code which will teach the original requester why it works. It just 'works'.

 

So I'm sorry to say, I'm on the fence, and believe both some of the requesters and some of the respondents bear equal responsibility for this problem. A lot of requesters ask questions that they didn't for one second try to solve themselves, and in the rush to provide the 'first' response, responders throw together a quick snippet of code and post it without much direction or comment (because, if they took the time to do so, some other hot gun would beat them to 'first' response). It goes on...

 

I agree, Will, that you adequately verbalized you wanted 'direction', and I believe you are owed an apology from Sensei.  But I do understand where Sensei's coming from, and his comment would have been 100% appropriate in about 20 other posts made just today alone.

 

PhREEEk

Link to comment
Share on other sites

An extremely well verbalised response.  Thanks for your comments and direction.

 

To all

 

Again I apologise for my response, but having read PHP_PhREEEk's comments I have identified my course of anger.  I too accept Sensai's response in light of the said comments. I can identify with his sentiments however I felt as though I was being 'tarred' with the same brush as the 'please do it for me' brigade. This  is what cause offence.

 

Whilst none of this solves my issue...lol...yet...(yet to look at your suggestion Phreeek) I do believe that this post has served to discuss some ongoing issues with this forum.

 

To those who help those who wish to learn...I salute you.

 

For the others....well now I would get banned.

Link to comment
Share on other sites

Will, from where I am standing I was getting slapped right in the face. I do understand you that you were requesting some sort of guidance or help, which I provided earlier with a tutorial. You have to try things on your own first, and never give up until you have the answer. You should have done some research, because google would have given you the results in a matter of seconds. I felt a little pissed since you just wanted us to show you how its done, but you should have been ready with a script of your own. It could have just been a little mysql connection, it wouldnt have mattered.

 

Then again, I feel like an ass saying so, we are getting alot of people wanting us to code for them ( your not one of them, and i am 100% sure).

 

I am sorry...

 

Accept my apologie please :P

Link to comment
Share on other sites

$query = mysql_query("select * from $table") or die(mysql_error());

    $num_fields = @mysql_num_fields($query);

    $numrow = mysql_num_rows($query);



    for ($i =0; $i<$numrow; $i++) {

      $result .= "INSERT INTO ".$table." VALUES(";

          for($j=0; $j<$num_fields; $j++) {

          $row[$j] = addslashes($row[$j]);

          $row[$j] = ereg_replace("\n","\\n",$row[$j]);

          if (isset($row[$j])) $result .= "\"$row[$j]\"" ; else $result .= "\"\"";

          if ($j<($num_fields-1)) $result .= ",";

         }   

      $result .= ");\n";

     }

     return $result . "\n\n\n";

 

Whats the error spit out?

Link to comment
Share on other sites

Here is one that does exactly what you want, with the email results.

 

< ?php

/*

Quickly and easily backup your MySQL database and have the tgz emailed to you.  You need PEAR installed with the Mail and Mail_Mime packages installed. Read more about PEAR here: http://pear.php.net. This will work in any *nix enviornment. Make sure you have write access to your /tmp directory.

*/

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

// mysql & minor details..
$tmpDir = "/tmp/";
$user = "root";
$password = "pass";
$dbName = "db";
$prefix = "db_";

// email settings...
$to = "someone@gmail.com";
$from = "another@gmail.com";
$subject = "db - backup";
$sqlFile = $tmpDir.$prefix.date('Y_m_d').".sql";
$attachment = $tmpDir.$prefix.date('Y_m_d').".tgz";

$creatBackup = "mysqldump -u ".$user." --password=".$password." ".$dbName." > ".$sqlFile;
$createZip = "tar cvzf $attachment $sqlFile";
exec($creatBackup);
exec($createZip);

$headers = array('From' => $from, 'Subject' => $subject);
$textMessage = $attachment;
$htmlMessage = "";

$mime = new Mail_Mime("n");
$mime->setTxtBody($textMessage);
$mime->setHtmlBody($htmlMessage);
$mime->addAttachment($attachment, 'text/plain');
$body = $mime->get();
$hdrs = $mime->headers($headers);
$mail = &Mail::factory('mail');
$mail->send($to, $hdrs, $body);

unlink($sqlFile);
unlink($attachment);

?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.