Jump to content

backup your DB


westman
 Share

Recommended Posts

i have a script to be ran by a cron job to backup a db weekly here is the script....

// configure your database variables below:$dbhost = 'localhost'; // Server address of your MySQL Server$dbuser = 'yourcpanelname_dbuser'; // Username to access MySQL database$dbpass = 'dbuserpassword'; // Password to access MySQL database$dbname = 'yourcpanelname_dbname'; // Database Name// Optional Options You May Optionally Configure$use_gzip = "yes"; // Set to No if you don't want the files sent in .gz format$remove_sql_file = "yes"; // Set this to yes if you want to remove the .sql file after gzipping. Yes is recommended.$remove_gzip_file = "yes"; // Set this to yes if you want to delete the gzip file also. I recommend leaving it to "no"// Configure the path that this script resides on your server.$savepath = "/home/yourcpanelname/dbsender"; // Full path to this directory. Do not use trailing slash!$send_email = "yes"; // Do you want this database backup sent to your email? Fill out the next 2 lines$to = "you@a-valid-domain.com"; // Who to send the emails to - you can also use a GMail address$from = "you@a-valid-domain.com"; // Who should the emails be sent from?$senddate = date("j F Y");$subject = "MySQL Database Backup - $senddate"; // Subject in the email to be sent. - you can change this$message = "Your MySQL database has been backed up and is attached to this email"; // Brief Message. - you can change this

my question is...how do i get this to just send the DB backup to my email and not save it on the server?

  • Like 1
Link to comment
Share on other sites

It probably has to become a file in order for it to be sent as attachment, or else you can get the raw SQL somehow and add it to the body of the message instead. If you don't want the file on your server, you can unlink the file after you have it sent to your email.

Link to comment
Share on other sites

+ the zip did not save on my server
I cant see anything in your provided code which will cause to query the database and save it and compress it to zip. is that your complete code?
but how do i get the zip in the email?
Set the appropiate content-type header for zip file in email header
Link to comment
Share on other sites

You need to actually tell MySQL to export the database. MySQL comes with a program called mysqldump which exports databases, so you need to use a function like exec to send a system command to execute the mysqldump program with the options you want. http://dev.mysql.com/doc/refman/5.1/en/mysqldump.html

Link to comment
Share on other sites

i found a new script thanks to google ;)....

<?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 serverunlink($backupfile);?>

will this work to send the DB in an email?not tested by me yet it dose not look like php5 but i could be wrong

Link to comment
Share on other sites

ok i tested it and it seemed to work but way does the file that gets sent to my email look difrent than if i was to EXPORT my DB?
Because of the options you're (not) using with mysqldump. Check the link I posted.
and this is not in the email, way?
What do you mean? The file is not attached to the email?
Link to comment
Share on other sites

  • 3 months later...

back agian am lost i had script working and then i changed my host provider to godaddy and can not get it working here is what i have...

 <?php// Create the mysql backup file// edit this section$dbhost = "my host info"; // usually localhost$dbuser = "my user name";$dbpass = "my password";$dbname = "my DB name";$sendto = "my email";$sendfrom = "email from";$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');	          ////// not shore what this is for    include('Mail/mime.php');     ////// not shore what this is for    $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 serverunlink($backupfile);?>

can any 1 see in my code any problems?

Link to comment
Share on other sites

It's your DBhost. Godaddy doesn't use local host. To get the host name go to your mysql control panel, open up the database preference for the database you want (NOT PHPMYADMIN) and the host name will be in there. It will be a long-ish link. How to find it:1) Go to the Hosting Control Center for your website2) Go to Database -> MySQL3) Under "Databases Name" find your database4) In the row you see your Database name look to the right and click on the pencil Icon, "Edit/View Details"5) Look at the "Host Name" and you have your dbhost variable!

Edited by Krewe
Link to comment
Share on other sites

what is not working? is there any errors? stating the error would be helpfull for us to help you i belive system() could be blocked by your host.

Link to comment
Share on other sites

the 2 includes were on my old server and did not have the file but the script worked. i do not have any errors to show. i now it does not work coz i have no email please tell me more about system() can it be replace with anything?

Link to comment
Share on other sites

There you have it: The files that contain the code that sends the mail is not there => you have no email sent.Get PEAR_Mail and PEAR_Mail_Mime, and upload them to your host in a way that makes them accessible to your script.

Link to comment
Share on other sites

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
 Share

×
×
  • Create New...