Links
|
| Thread:
PHP Snippet on how to setup automated MYSQL Backup
|
|
Web Development & Designing Got any web designing skills? Or maybe a question about a script that doesn't work? Use this forum to talk about this type of thing. |
 |
|
 |
|
 |
|
|
Join Date: 24 Jan 2005
Location: :.Middle Earth.:
Age: 21
Posts: 7,113
|

30-08-2006, 11:44
|
|
AgeRage™ Founder
|
|
|
PHP Snippet on how to setup automated MYSQL Backup
PHP Code:
<?php
/*
Attention, you will propably need to contact your hosting company to setup a cronjob for you, if you dont know
*/
// configure your database variables below
$dbhost = 'localhost'; // Server address of your MySQL Server
$dbuser = 'your_username'; // Username to access MySQL database
$dbpass = 'yourpass'; // Password to access MySQL database
$dbname = 'database_name'; // 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 = "no"; // 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 = "/www/htdocs/dbsender"; // Full path to this directory. Do not use trailing slash!
$send_email = "no"; /* Do you want this database backup sent to your email? Yes/No? If Yes, Fill out the next 2 lines */
$to = "you@yourdomain.com"; // Who to send the emails to
$from = "email@yourdomain.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.
$message = "Your MySQL database has been backed up and is attached to this email"; // Brief Message.
$use_ftp = "no"; // Do you want this database backup uploaded to an ftp server? Fill out the next 4 lines
$ftp_server = "192.168.0.1"; // FTP hostname
$ftp_user_name = "ftp_username"; // FTP username
$ftp_user_pass = "ftp_password"; // FTP password
$ftp_path = "/"; // This is the path to upload on your ftp server!
// Do not Modify below this line! It will void your warranty :-D!
$date = date("mdy-hia");
$filename = "$savepath/$dbname-$date.sql";
passthru("mysqldump --opt -h$dbhost -u$dbuser -p$dbpass $dbname >$filename");
if($use_gzip=="yes"){
$zipline = "tar -czf ".$dbname."-".$date."_sql.tar.gz $dbname-$date.sql";
shell_exec($zipline);
}
if($remove_sql_file=="yes"){
exec("rm -r -f $filename");
}
if($use_gzip=="yes"){
$filename2 = "$savepath/".$dbname."-".$date."_sql.tar.gz";
} else {
$filename2 = "$savepath/$dbname-$date.sql";
}
if($send_email == "yes" ){
$fileatt_type = filetype($filename2);
$fileatt_name = "".$dbname."-".$date."_sql.tar.gz";
$headers = "From: $from";
// Read the file to be attached ('rb' = read binary)
$file = fopen($filename2,'rb');
$data = fread($file,filesize($filename2));
fclose($file);
// Generate a boundary string
$semi_rand = md5(time());
$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
// Add the headers for a file attachment
$headers .= "\nMIME-Version: 1.0\n" ."Content-Type: multipart/mixed;\n" ." boundary=\"{$mime_boundary}\"";
// Add a multipart boundary above the plain message
$message = "This is a multi-part message in MIME format.\n\n" ."--{$mime_boundary}\n" ."Content-Type: text/plain; charset=\"iso-8859-1\"\n" ."Content-Transfer-Encoding: 7bit\n\n" .
$message . "\n\n";
// Base64 encode the file data
$data = chunk_split(base64_encode($data));
// Add file attachment to the message
$message .= "--{$mime_boundary}\n" ."Content-Type: {$fileatt_type};\n" ." name=\"{$fileatt_name}\"\n" ."Content-Disposition: attachment;\n" ." filename=\"{$fileatt_name}\"\n" ."Content-Transfer-Encoding: base64\n\n" .
$data . "\n\n" ."--{$mime_boundary}--\n";
// Send the message
$ok = @mail($to, $subject, $message, $headers);
if ($ok) {
echo "<h4><center>Database backup created and sent! File name $filename2</center></h4>";
} else {
echo "<h4><center>Mail could not be sent. Sorry!</center></h4>";
}
}
if($use_ftp == "yes"){
$ftpconnect = "ncftpput -u $ftp_user_name -p $ftp_user_pass -d debsender_ftplog.log -e dbsender_ftplog2.log -a -E -V $ftp_server $ftp_path $filename2";
shell_exec($ftpconnect);
echo "<h4><center>$filename2 Was created and uploaded to your FTP server!</center></h4>";
}
if($remove_gzip_file=="yes"){
exec("rm -r -f $filename2");
}
?>
I hope it helps you a bit  .
|

|
|
Join Date: 14 Oct 2005
Location: Hell
Posts: 1,615
|

30-08-2006, 12:52
|
|
Will Rape Noobs
|
|
|
|
*scratch head.* I wish i knew what PHP was and what a MYSQL backup was..
|
MYSPACE
Itz Bucky
Quote:
|
Great thing I love about this site is the kick-ass view of the moderator, some sites just let noobs ask stupid questions.
|
|
|
|
Join Date: 24 Jan 2005
Location: :.Middle Earth.:
Age: 21
Posts: 7,113
|

30-08-2006, 13:17
|
|
AgeRage™ Founder
|
|
|
PHP is web's most powerfull programming language. mySQL on the other hand is the world's most powerfull database.
If you use both of them ,you can do miracles  . Forums data is stored in mySQL tables.
|
|
|
Join Date: 09 Mar 2005
Location: www.agerage.net/forums
Posts: 3,897
|

30-08-2006, 14:14
|
|
The Adminorator
|
|
|
|
Very useful, thanks a lot VF. I presume you have set this up for AR already?
|
|
|
Join Date: 07 May 2006
Location: Tech-fusion.net :O
Posts: 1,299
|

30-08-2006, 14:28
|
|
Golden Dragon
|
|
|
|
That's cool =.= Php seems complicated :o
|
|
|
Join Date: 29 Sep 2005
Posts: 928
|

30-08-2006, 16:24
|
|
Bronze Dragon
|
|
|
|
How would you get this to run say every week, once a week?
Put a check for a date on every page and when someone loads that page run the script if its the preset date?
or is there another way?
|
|
|
Join Date: 24 Jan 2005
Location: :.Middle Earth.:
Age: 21
Posts: 7,113
|

30-08-2006, 16:55
|
|
AgeRage™ Founder
|
|
|
Quote:
|
Originally Posted by Skyline_R34
Very useful, thanks a lot VF. I presume you have set this up for AR already?
|
yeah, however i suspect something is wrong with servage's cronjob.
By the way, by mistake i deleted T-F cronjob, kidly add it back. Sorry.
Quote:
|
Originally Posted by 2nd~0v3R|r!de
Put a check for a date on every page and when someone loads that page run the script if its the preset date?
or is there another way?
|
No, there is a faster way. You simply setup a cronjob, in your control panel, and you point it to run the script every day on 12am or any other time you wish. Cronjobs in control panels, have unlimited options, set it daily/weekly/monthly etc.
|
|
|
Join Date: 29 Sep 2005
Posts: 928
|

30-08-2006, 16:58
|
|
Bronze Dragon
|
|
|
Quote:
|
Originally Posted by Virtuosofriend
No, there is a faster way. You simply setup a cronjob, in your control panel, and you point it to run the script every day on 12am or any other time you wish. Cronjobs in control panels, have unlimited options, set it daily/weekly/monthly etc.
|
Awww ok, thanks. I've got my apache server sitting next to me so i'l take a look at that. Nice one!
|
|
|
Join Date: 24 Jan 2005
Location: :.Middle Earth.:
Age: 21
Posts: 7,113
|

30-08-2006, 17:59
|
|
AgeRage™ Founder
|
|
|
|
let me know if you try it out, because it works on my local but not on agerage. I would very much like to know.
|
|
|
Join Date: 09 Mar 2005
Location: www.agerage.net/forums
Posts: 3,897
|

30-08-2006, 18:30
|
|
The Adminorator
|
|
|
Quote:
|
Originally Posted by Virtuosofriend
yeah, however i suspect something is wrong with servage's cronjob.
By the way, by mistake i deleted T-F cronjob, kidly add it back. Sorry.
|
It wasn't working anyway, so no problems. I get MySQL backups manualy, but this will work a lot better. Thanks.
EDIT:
Added to Tech-Fusion, will tell you if it works tomorrow  .
|
|
Last edited by Skyline_R34; 30-08-2006 at 20:56..
|
|
|
Join Date: 24 Jan 2005
Location: :.Middle Earth.:
Age: 21
Posts: 7,113
|

30-08-2006, 21:57
|
|
AgeRage™ Founder
|
|
|
|
i doubt, but let me know. I spent the whole evening figuring out why. gonna let you know guys, if it is a bug in the code or something
|
|
|
Join Date: 07 May 2006
Location: Tech-fusion.net :O
Posts: 1,299
|

30-08-2006, 22:49
|
|
Golden Dragon
|
|
|
Quote:
|
Originally Posted by Virtuosofriend
i doubt, but let me know. I spent the whole evening figuring out why. gonna let you know guys, if it is a bug in the code or something
|
Did you fix the skin yet =\
|
|
|
Join Date: 13 Feb 2005
Location: ]EUA[ Leader Throne
Age: 22
Posts: 447
|

30-08-2006, 23:04
|
|
MPC™ Co-Founder
|
|
|
Now that is handy, nice one VF
|
|
|
Join Date: 24 Jan 2005
Location: :.Middle Earth.:
Age: 21
Posts: 7,113
|

30-08-2006, 23:08
|
|
AgeRage™ Founder
|
|
|
Quote:
|
Originally Posted by GFX Nation
Did you fix the skin yet =\
|
negative. I worked on that shit today...erm i guess i have to do it but dont expect any results for the next 2-3 weeks.
|
|