 |
|
|
03-12-2005, 02:01 PM
|
MySQL backup
|
Posts: 20
|
Hello, I've been searching all over the place for a script that I can cron to backup my database. It also needs to have the ability to email me the archived file. I have found a few, but cannot use them as my current host does not have DBI, MIME Lite or MUTT modules installed.
Does anyone know of a free script that will email me the file without needing any of those 3 modules? Perhaps one that can just POP me the attached file??
|
|
|
|
03-12-2005, 02:10 PM
|
|
Posts: 3,189
|
Well if you know much about php this would not be hard to implement. You can use a DUMP sql statement to dump the database to a file then email the file as an attachment.
|
|
|
|
03-12-2005, 02:38 PM
|
|
Posts: 20
|
It's probably a pretty simple thing, but I have no clue how to do it as I don't know PHP at all. I'm using a script right now, and it dumps the database to a file with no problems. But I can't get it to mail me the results (as the script is designed to do) because of the missing modules. Basically, I'm using half the script.
From your reply, there IS a way to have the file emailed to me even without MIME Lite, MUTT, etc????
|
|
|
|
03-12-2005, 03:29 PM
|
|
Posts: 3,189
|
Yep, PHP has some built in MIME features that can be used. I'll put something together and post it here later for the whole community to use.
|
|
|
|
03-12-2005, 03:40 PM
|
|
Posts: 20
|
OK, thanks for the good news! You don't have to go through all that trouble for me, but it might be good for the community to see it
I did find a php script that looks like it might work to mail the attachment. But I CAN'T get the script to run!
I keep getting this error:
/usr/bin/php: bad interpreter: No such file or directory
I'm sure it's simple, but I have tried every path I can think of, and it still won't recognize it. Anybody know what I'm doing wrong?
|
|
|
|
03-12-2005, 04:32 PM
|
|
Posts: 20
|
Okay, did some research. The PHP on my host is installed as Apache, and not CGI. From what I gather, I need access to Lynx, so I can edit my /etc/crontab file??? I don't have access to Lynx, nor can I even get into my /etc directory. Do I need to just find another host???????????
|
|
|
|
03-12-2005, 05:46 PM
|
|
Posts: 3,189
|
How are you calling the script in your crontab? Are you calling it directly or are you prepending php to it?
Here is what I came up with regardless. However, perhaps the community can help me out on this one I keep getting an error yet it sends the file fine.
Here is MyDump.php:
PHP Code:
<?php
// Database Settings
$user = 'root';
$pass = '';
$db = 'test';
// Email Settings
$from = '';
$fromName = '';
$to = '';
$toName = '';
$subject = 'MySQL dump file ' . date("M-d-Y");
$message = 'You\'ve got a new backup';
// Backup File Settings
$save_path = '/home/civilis/public_html'; // without trailing slash
$file_name = 'mybackup.sql';
// Stop editing here
exec("mysqldump -u $user -p$pass $db > $save_path/$file_name");
$data = implode("", file("$save_path/$file_name"));
$gzdata = gzencode($data, 9);
$fp = fopen("$save_path/$file_name.gz", "w");
fwrite($fp, $gzdata);
fclose($fp);
exec("rm $save_path/$file_name");
include 'class.phpmailer.php';
$mail = new PHPMailer();
$mail->From = $from;
$mail->FromName = $fromName;
$mail->AddAddress($to, $toName);
$mail->AddAttachment($file_name.'.gz');
$mail->IsHTML(false);
$mail->Subject = $subject;
$mail->Body = $message;
$mail->Send();
?>
This script requires PHPMailer which is very easy to use, just drop it into the same directory as this file.
However, I get this error by email along with a seperate email with the proper backed up file...
Code:
Content-type: text/html
X-Powered-By: PHP/4.3.10
<br />
<b>Warning</b>: fopen(/home/civilis/public_html/path/changed/MyDump/mybackup.sql.gz): failed to open stream: Permission denied in <b>/home/civilis/public_html/path/changed/MyDump/MyDump.php</b> on line <b>23</b><br />
<br />
<b>Warning</b>: fwrite(): supplied argument is not a valid stream resource in <b>/home/civilis/public_html/path/changed/MyDump/MyDump.php</b> on line <b>24</b><br />
<br />
<b>Warning</b>: fclose(): supplied argument is not a valid stream resource in <b>/home/civilis/public_html/path/changed/MyDump/MyDump.php</b> on line <b>25</b><br />
I don't get this because the very next email is the correct email with the attachment...Anyone else have a clue?
|
|
|
|
03-12-2005, 06:37 PM
|
|
Posts: 20
|
Ok, I just finished toying with that script. I haven't tried with cron yet, just ran it from the browser. I got it to where I don't get a screen full of errrors, and it's mailing me a mail with "You've got a new backup"
I don't get 2 mails like you are. No error mail at all. It is sending me an attachment, but it's only 1K in size. My database is like 3MB when gzipped. So something's wrong there. Did you get an actual DB backup mailed to you in the correct size?
|
|
|
|
03-12-2005, 07:17 PM
|
|
Posts: 3,189
|
Yeah, I got the full backup. What errors were you getting on the screen? As far as my other email error that would only happen when I cronned it.
|
|
|
|
03-12-2005, 07:39 PM
|
|
Posts: 20
|
I was getting the usual permission and path errors. Nothing to do with your script, just me. This thing still won't work. It's not a PHPMailer problem, as it's not even backing up my database onto the server. Well, it does, but it's 1K as well.
I'll just switch hosting companies and get MUTT. All these scripts I've been trying is driving me nuts. Thanks for the help, cptn. 
|
|
|
|
03-12-2005, 07:51 PM
|
|
Posts: 110
|
A very quick and easy way is to run a cron job consisting of a script:
shutdown MySQL
tarzip the data directories
restart mysql
move the tarzip to an ftp'able directory and chmod it to the ftp user
on a windows PC run a schedule task to ftp download the file
This is what I do and it requires no user intervention. The script runs at 4:00am so there is hardly any traffic. If there was, the database is only down for about 30seconds.
When I boot my windows PC in the morning a schedule task waits until 9:00am and downloads the tarzip.
I don't have to do anything - except switch the PC on before 9:00am
I have a second server on the net which also grabs the file at 5:00am. Really, this is idiot proof!
|
|
|
|
03-12-2005, 09:11 PM
|
|
Posts: 2,099
Name: Adam
Location: Colchester CT
|
If your host uses cPanel, then you'll most likely have the ability to schedule automatic backups through that.
|
|
|
|
03-12-2005, 09:35 PM
|
|
Posts: 3,189
|
I'm not sure whether the exec function will work on windows, is your hosting windows based?
|
|
|
|
03-12-2005, 09:52 PM
|
|
Posts: 20
|
Anacrusis, no I don't have cpanel. My host offers h-sphere, but there is a cron function that allows me to set up these jobs. I already have the auto backups running, but would like the files sent to an email adde like gmail or yahoo, since I have a slow dial-up that takes forever.
Cptnwinky, yes, I'm running on Linux. I don't know why it's not working. It's okay though. I just signed up for another host and waiting activation. I will get this all settled in a jiffy after that.
I couldn't get the script to work, but others probably will. It's a very good script to have, so thanks for taking the time to write it up. I will try it on the new server as well 
|
|
|
|
|
« Reply to MySQL backup
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|