Back Up Your Db And Send It To Your Email

Started on the old forum in May 2008 · view the original in the archive · old links redirect here

10 replies · 768 views · started 18 years ago · 1 min read

6 participants
learner Lead · 587 posts #1

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!!

BEST MOBILE ADVERTISER

External image (may be gone)

djdevil89 Senior · 287 posts #5

atleast u can be secured and could easily backup your db without logging into your cpanel etc.

But be carefull with it as it doesnt seems so secure did u use

 exit ();

????

foreach ($_SERVER as $server => $value)
{
echo "$server is $value<br />";
}
djlee Developer · 224 posts #6

i suggest you change the name of index.php and obfuscate it a LOT .. or add script protection to only allow authorised users (i.e. urself) to access it.

  1. rename the file maybe zzxxccvvbb.php
  2. if you dont wanna rename stick it in a new folder with a stupid name but u can remember and include an htaccess file to only allow access to it from your IP/IP range

i say this because wat happens if i come on your site and i decide im gonna see whether you have this code installed. i mess with the URL and finally find it.. great now i open up 10 tabs and have them reload once a second . thats 10 backups being compiled per second and then emailed .. i think your server is going to not like you very much External image (may be gone)

subzero Super moderator Fellow · 3,954 posts #7

lol i try what ya said it do like to use cpu but server still up thou power cpu lol

Visit: Chat4u.mobi - The New Lay Of being a site of your dreams! Visit: WapMasterz Coming Back Soon!


SCRIPTS FOR SALE BY SUBZERO Chat4u Script : coding-talk.com/f28/chat4u-mobi-script-only-150-a-17677/ - > Best Script for your site no other can be hacked by sql or uploaders. FileShare Script : coding-talk.com/f28/file-wap-share-6596/ -> Uploader you will never regret buying yeah it mite be old now but it still seems to own others... _______…

djlee Developer · 224 posts #8

ok so now i have the rest of my team do the same but this time i do it with 30 or 40 tabs .. u've already seen the hit on the CPU.. double the efforts i double the hit on your load.

Your webserver will most likely never completely shut down .. even during a DDOS attack it is possible to make periodic connections to the attacked server if you get your request in at the right time and wait for processing. The point is it can make your server sluggish and what is the point in giving someone ammunition to harm your site when fixing it or at least obfuscating it is a piece of cake

subzero Super moderator Fellow · 3,954 posts #9

No its the power of the quad cores

that script use 5% of cpu and didnt move abit other that

4 x (time's) 2.20 ghz

Visit: Chat4u.mobi - The New Lay Of being a site of your dreams! Visit: WapMasterz Coming Back Soon!


SCRIPTS FOR SALE BY SUBZERO Chat4u Script : coding-talk.com/f28/chat4u-mobi-script-only-150-a-17677/ - > Best Script for your site no other can be hacked by sql or uploaders. FileShare Script : coding-talk.com/f28/file-wap-share-6596/ -> Uploader you will never regret buying yeah it mite be old now but it still seems to own others... _______…

djlee Developer · 224 posts #10

ok so you have a quad core .. most people here either have shared hosting with fook knows how many sites on the same server or the specs arent that great considering you dont need a good server for a wap site ... but like i said why give someone ammunition.. it only takes someone who has a few good friends to take a disliking to you and they could lag your server .. even a quad core has limits although it would probably max out your RAM and the php allowed resources before your CPU took too much of a hit

alesh Lead · 604 posts #11

Is there maybe a script tht will backup my database nd save it on my server....? I mean above the public_html ....i found many scripts,but like this 1, it only sends backup 2 email....found 1 even with tht cron jobs or wathever, but niether 1 of them doesnt save backup 2 my server....

eeeeerrr....

Write your reply now; next you create a free account (or sign in) and it is posted right away. Sign in to reply.

Similar topics

Keyboard shortcuts

g f
Go to forums
g h
Go to your feed
g m
Go to messages
r
Reply (in a topic)
/
Search
j
Next post
k
Previous post
?
This list
Ctrl/⌘ + Enter
Send what you are writing
Ctrl/⌘ + B / I
Bold / italic while writing
Esc
Close