Page 1 of 2

Easy Database Backup Script

Posted: Sat Jan 20, 2007 3:23 am
by Elijah Lofgren
Here is a database backup script that I use a "cron" job to automatically run ever day so my website's databases are automatically zipped up and emailed to me:

Code: Select all

<?php
/**
 * Generates a backup of the database and sends it in an email
 *
 * @author Elijah Lofgren < elijahlofgren [at] elijahlofgren.com>
 * @version $Id: backup-database-cron.php 2135 2005-08-07 13:43:29Z elijahlofgren $
 */
require_once dirname(__FILE__).'/../config.php';
$output = 'Creating Database backup.';
$email = 'youremail@example.com';
$output .= backup($config, $email);
echo $output;
// $output .= $Page->showFooter();
// echo $output;

    /* Database Backup Utility 1.0 By Eric Rosebrock, http://www.phpfreaks.com
    Written: July 7th, 2002 12:59 AM

    If running from shell, put this above the <?php  "#! /usr/bin/php -q"  without the quotes!!!

    This script is dedicated to "Salk". You know who you are :)

    This script runs a backup of your database that you define below. It then gzips the .sql file
    and emails it to you or ftp's the file to a location of your choice.

    It is highly recommended that you leave gzip on to reduce the file size.

    You must chown the directory this script resides in to the same user or group your webserver runs
    in, or CHMOD it to writable. I do not recommend chmod 777 but it's a quick solution. If you can setup
    a cron, you can probably chown your directory!

    IMPORTANT!!! I recommend that you run this outside of your
    web directory, unless you manually want to run this script. If you do upload it inside your web
    directory source Tree($settings), I would at least apply Apache access control on that directory. You don't
    want people downloading your raw databases!

    This script is meant to be setup on a crontab and run on a weekly basis
    You will have to contact your system administrator to setup a cron tab for this script
    Here's an example crontab:

    0 0-23 * * * php /path/to/thisdirectory/dbsender.php > /dev/null

    */
    function backup($config, $email)
    {
        // Optional Options You May Optionally Configure

        $use_gzip = TRUE;  // Set to FALSE if you don't want the files sent in .gz format
        $remove_sql_file = TRUE; // Set this to TRUE if you want to remove the .sql file after gzipping. Yes is recommended.
        $remove_gzip_file = TRUE; // Set this to TRUE if you want to delete the gzip file also. I recommend leaving it to "no"

        // Full path to the backup directory. Do not use trailing slash!
        $savepath = dirname(__FILE__);
        $send_email = TRUE;  // Do you want this database backup sent to your email? Fill out the next 2 lines

        // $senddate = date("j F Y");
        $senddate = date('Y-m-d-Hi');

        // Subject in the email to be sent.
        $subject = 'MySQL DB: '.$config['db_name'].' Backup - '.$senddate;
        $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 = ""; // FTP hostname
        $ftp_user_name = ""; // FTP username
        $ftp_user_pass = ""; // FTP password
        $ftp_path = "/public_html/backups/"; // This is the path to upload on your ftp server!
*/
        // Do not Modify below this line! It will void your warranty!

        $date = date('Y-m-d-Hi');
        $filename = $savepath.'/'.$config['db_name'].'-'.$date.'.sql';
        // passthru("mysqldump --opt -h$dbhost -u$dbuser -p$dbpass $dbname >$filename");
        // passthru("mysqldump --opt -h$dbhost -u$dbuser -p$dbpass $dbname >$filename");
        // $passthru = "mysqldump -h $server -u $username -p$password --add-drop-table --all --quick --lock-tables --disable-keys --extended-insert -d $database >$filename";
        $passthru = 'mysqldump --opt'
                             .' -h '.$config['db_hostname']
                             .' -u '.$config['db_username']
                      // There must not be a space between -p and the password
                             .' -p'.$config['db_password']
                      // There MUST be a space between the password and DB name
                             .' '.$config['db_name'].' > '.$filename;
                             // echo $passthru;
        //  $passthru = "mysqldump -h $server -u $username -p$password --opt --tables -d $database >$filename";
        // echo $passthru;
        passthru($passthru);

        if (FALSE != $use_gzip) {
            $real_path = realpath($savepath);
            // echo '<br />df '.$real_path.'</br>';
            $zipline = "tar -czf ".$real_path.'/'.$config['db_name'].'-'.$date.'_sql.tar.gz '.$real_path.'/'.$config['db_name'].'-'.$date.'.sql';
            // $zipline = "tar -czf --directory=".$real_path.'  '.$database."-".$date."_sql.tar.gz "."$database-$date.sql";
            //   echo '<br />'.$zipline.'<br />';
            shell_exec($zipline);
        }
        // Remove the SQL file if needed
        if (FALSE != $remove_sql_file) {
            exec('rm -r -f '.$filename);
        }
        // If supposed to gzip the file
        if (FALSE != $use_gzip) {
            $filename2 = $savepath.'/'.$config['db_name'].'-'.$date.'_sql.tar.gz';
        } else {
            $filename2 = $savepath.'/'.$config['db_name'].'-'.$date.'.sql';
        }
        // If backing up to email address
        if (FALSE != $send_email) {
            $fileatt_type = filetype($filename2);
            //  echo $filename2;
            $fileatt_name = "".$config['db_name']."-".$date."_sql.tar.gz";
            $headers = 'From: '.$email;
            // 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($email, $subject, $message, $headers);
            if (FALSE != $ok) {
                $output  = '<p>Database backup created and sent! ';
                $output .= 'File name '.$filename2.'</p>';
            } else {
                $output = '<p>Mail could not be sent. Sorry!</p>';
            }
        }
        /*
        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 ('yes' == $remove_gzip_file) {
            exec("rm -r -f $filename2");
        }
        return $output;
    }

?>
Just put the above code in a .php file in a subfolder to your CMSMS install and chmod 777 the folder. Then you can set up a cron or just manually run it.

Hope this helps someone,

Elijah

Edit: added slash to /dev/null.

Re: Easy Database Backup Script

Posted: Fri Feb 23, 2007 10:35 pm
by fiddle
question...When you run a backup, does it back up the whole database.. meanint templates css files and entries.? The only thing I would manually have to back up would be pictures, video etc..    Is this correct.?
thanks

Re: Easy Database Backup Script

Posted: Fri Feb 23, 2007 10:46 pm
by Elijah Lofgren
fiddle wrote: question...When you run a backup, does it back up the whole database.. meanint templates css files and entries.? The only thing I would manually have to back up would be pictures, video etc..    Is this correct.?
thanks
Yes, it backs up the entire database.
If you just use the CMSMS upload method, everything else that you need to back up should be inside the "uploads" folder.

Have a great weekend,

Elijah

Re: Easy Database Backup Script

Posted: Fri Feb 23, 2007 11:01 pm
by fiddle
thank you very much

Re: Easy Database Backup Script

Posted: Wed Apr 25, 2007 12:17 pm
by chilsta
Just working on getting this working myself...

One problem I found: when setting up the cron, be sure to use:

0 0-23 * * * php /path/to/thisdirectory/dbsender.php > /dev/null

(note the leading slash before dev)

rather than:

0 0-23 * * * php /path/to/thisdirectory/dbsender.php > dev/null

as is written in the script.

-C-

Re: Easy Database Backup Script

Posted: Fri Apr 27, 2007 12:25 pm
by skypanther
Not all hosts permit exec() calls or access to mysqldump. I humbly suggest my backup app, which also provides you a way to back up a subset of tables as well as files. http://skypanther.com/spbackup.php

Re: Easy Database Backup Script

Posted: Fri Apr 27, 2007 12:37 pm
by cyberman
skypanther wrote: I humbly suggest my backup app
Sounds great - maybe you could write a module for integrating it into CMSms admin panel  ;) ...

Re: Easy Database Backup Script

Posted: Tue May 08, 2007 1:19 am
by slloyd
Elijah, can you explain the steps for running a cron job? I have no idea what this means or how to do it. I don't know much of anything about MySql or php. TIA.

Re: Easy Database Backup Script

Posted: Tue May 08, 2007 3:00 am
by Elijah Lofgren
slloyd wrote: Elijah, can you explain the steps for running a cron job? I have no idea what this means or how to do it. I don't know much of anything about MySql or php. TIA.
The only way that I know of is to use Cpanel (unless you have root file access, if so, you can create a script in /etc/cron.daily).
I just do the following:
1. Login to Cpanel.
2. Go to "Cron Jobs"
3. Click "Standard" experience level.
4. Then just choose a time and in the "Command to run:" entry put something like this:

Code: Select all

php /home/your_name/public_html/backups/backup-database-cron.php
If your host uses something other than Cpanel there is probably some other way to set this up.

Hope this helps,

Elijah

Re: Easy Database Backup Script

Posted: Fri May 11, 2007 5:09 pm
by jsmonzani
Great job, thanks for the tip  8)

Re: Easy Database Backup Script

Posted: Tue May 15, 2007 9:47 pm
by slloyd
Elijah, I'm not using CPanel. I have a VPS that I access through Plesk.

Re: Easy Database Backup Script

Posted: Wed May 16, 2007 1:02 pm
by skypanther
cyberman wrote:
skypanther wrote: I humbly suggest my backup app
Sounds great - maybe you could write a module for integrating it into CMSms admin panel  ;) ...
cyberman, it's on my to do list!

@slloyd - check out this Flash tutorial for setting up cron jobs via Plesk
http://www.swsoft.com/doc/tutorials/Ple ... rontab.htm

Re: Easy Database Backup Script

Posted: Wed May 23, 2007 12:34 am
by wannawebdesigns
I'm looking forward to skypanther's backup module ;D
Free software is goooood!

Re: Easy Database Backup Script

Posted: Sat May 26, 2007 10:01 am
by tigercat
Are there any plans to integrate this backup script into CMSMS 2.0?
Regarding the script: how can I restore my data afterwards? Is there even a possibility to copy data from one into another database (of course containing the same database structure ;-) )

Re: Easy Database Backup Script

Posted: Wed Jun 06, 2007 8:47 pm
by skypanther
Sorry, I've been tied up with other projects and haven't been to these forums in a while. The spBackup script creates a SQL file that you can restore using a tool like phpMyAdmin, HeidiSQL, mySQL-Front, etc. The SQL the file contains will re-create the tables (it has both DROP TABLE IF EXISTS and CREATE TABLE commands). You could edit the file to remove these if you wanted to restore to an existing db. It's designed to work for a web hosted environment where you create an empty db through your site's control panel. Then, you use phpMyAdmin or a remote admin tool (MySQL-Front) to process the SQL.

When I do get the chance to turn spBackup into a CMSMS module, it will be commercial not free. The price will be the same $20 USD that I charge for the standalone version. I make exceptions for hardships / good causes and I'll need beta testers, who would get a free copy. But I'm afraid it's going to be a couple weeks more before I can even start.

Tim