config.php
<?php
/***************************************************
learner coded it hehehe
****************************************************
Database settings
****************************************************/
$db_name = ''; // Database name
$db_username = ''; // Database username
$db_password = ''; // Database password
/***************************************************
Datestamp settings (uncomment one)
- this will be a part of your backup filename
****************************************************/
$date_format = 'd.m.Y H:i'; // 01.01.2005 19:05
# $date_format = 'H-i d-m-Y'; // 19-05 01-01-2005
# $date_format = 'Y-m-d h-i'; // 2005-01-01 07-05
/***************************************************
Backup file settings
****************************************************/
$backup_basename = 'backup'; // filename example: backup-01-01-2005_19-05.sql.gz, where 'backup' part is customizable
$delete_backup = true; // true = delete after sending
// false = leave at the server
/***************************************************
E-mail settings
****************************************************/
$from_email = 'any email id'; // this is 'from' e-mail address
$to_email = 'your email id'; // backup file will be sent to this e-mail address
$email_subject = 'MySQL backup'; // backup e-mail subject
$custom_message = false; // write here anything you want
$send_log = false; // true = send log file to an e-mail after each backup transfer
// false = don't send log file, leave it at the server
/***************************************************
Error reporting
(if you have any trouble, please
uncomment this and send me your error log)
****************************************************/
// error_reporting(E_ALL);
?>
index.php
<?php
/***************************************************
learner coded it hehehe
****************************************************/
require('config.php');
function backup_file() {
global $backup_basename;
$find = array(' ', ':', '/');
$replace = array('_', '-', '.');
$backup_filename = $backup_basename . '_' . str_replace($find, $replace, date_stamp()) . '.sql.gz';
return $backup_filename;
}
function date_stamp() {
global $date_format;
$date_stamp = date($date_format);
return $date_stamp;
}
function db_dump($multiple_db = false) {
global $db_username, $db_password, $db_name;
$db_name = ($multiple_db) ? '--all-databases' : $db_name;
$command = 'mysqldump -u ' . $db_username . ' --password=' . $db_password . ' ' . $db_name . ' | gzip > ' . backup_file();
$dump_status = (passthru($command) === false) ? 'N' : 'Y';
return $dump_status;
}
function send_att($attach_file, $isdb = true) {
global $to_email, $from_email, $email_subject, $custom_message, $send_log_weekday;
$mail_status = '';
$email_subject = $isdb ? $email_subject . ' [db file]' : $email_subject . ' [log file]';
$mailer = 'Sent by learner.';
$message = ($custom_message) ? $custom_message . "\n\n" : '';
$message .= 'Database backup file:' . "\n" . ' - ' . $attach_file . "\n\n";
$message .= '---' . "\n" . $mailer;
$boundary = md5(uniqid(time()));
$attachment = chunk_split(base64_encode(implode('', file($attach_file))));
$headers = 'From: ' . $from_email . "\n";
$headers .= 'MIME-Version: 1.0' . "\n";
$headers .= 'Content-type: multipart/mixed; boundary="' . $boundary . '";' . "\n";
$headers .= 'This is a multi-part message in MIME format. If you are reading this, then your e-mail client probably doesn\'t support MIME.' . "\n\n";
$headers .= $mailer . "\n";
$headers .= '--' . $boundary . "\n";
$headers .= 'Content-Type: text/plain; charset="iso-8859-1"' . "\n";
$headers .= 'Content-Transfer-Encoding: 7bit' . "\n\n";
$headers .= $message . "\n";
$headers .= '--' . $boundary . "\n";
$headers .= 'Content-Disposition: attachment;' . "\n";
$headers .= 'Content-Type: Application/Octet-Stream; name="' . $attach_file . "\"\n";
$headers .= 'Content-Transfer-Encoding: base64' . "\n\n";
$headers .= $attachment . "\n";
$headers .= '--' . $boundary . '--' . "\n";
if (mail($your_email, $email_subject, $message, $headers)) {
if ($isdb && $delete_backup) {
unlink(backup_file());
}
$mail_status = 'Y';
} else {
$mail_status = 'N';
}
return $mail_status;
}
function write_log($log_file = 'backup_log.txt') {
$log_content = "\n" . date_stamp() . "\t\t\t" . db_dump() . "\t\t\t" . send_att(backup_file());
if (!$handle = fopen($log_file, 'a+')) exit;
if (chmod($log_file, 0644) && is_writable($log_file)) {
if (filesize($log_file) == '0') {
$log_header = '/***************************************************' . "\n\n\t";
$log_header .= 'learner the great' . "\n\t";
$log_header .= 'learner rules' . "\n\n";
$log_header .= '****************************************************/' . "\n\n";
$log_header .= 'Backup log' . "\n";
$log_header .= '----------------------------------------------' . "\n";
$log_header .= 'DATESTAMP: DUMPED MAILED' . "\n";
$log_header .= '----------------------------------------------';
if (fwrite($handle, $log_header) === false) exit;
}
if (fwrite($handle, $log_content) === false) exit;
}
fclose($handle);
send_att($log_file, false);
}
write_log();
?>
any error or bug post here
Failure is not when a girls leaves you, its only when you let her go virgin. heheh!!