+ Reply to Thread
Results 1 to 4 of 4

Thread: Universal DB Backup

  1. #1
    Code Monkey
    Points: 4,025, Level: 42
    Level completed: 50%, Points required for next Level: 75
    Overall activity: 99.2%
    Achievements:
    Tagger First Class1000 Experience Points1 year registered
    Awards:
    User with most referrers
    lfhost's Avatar
    Join Date
    Jul 2010
    Posts
    313
    Points
    4,025
    Level
    42
    Downloads
    0
    Uploads
    0
    My Mood
    Bored

    Universal DB Backup

    Hey All,

    It was asked on the forum for a DB backup via cron.

    So here it is.

    Save this in a file called sql_backup.php for example.

    PHP Code:
    <?php
    ob_start
    ();
    set_time_limit(0);
    //enter your MySQL database host name, often it is not necessary to edit this line
    define('DBHOST''localhost');

    //enter your MySQL database username
    define('DBUSERNAME''');

    //enter your MySQL database password
    define('DBPASSWORD''');

    //enter your MySQL database name
    define('DBNAME''');

    //full path to the backup directory (this should be a writable directory i.e. chmodd to 777)
    $filelocation "/home/cpanel_username/bk/";

    $conn mysql_connect(DBHOSTDBUSERNAMEDBPASSWORD) or die(mysql_error());
    $db mysql_select_db(DBNAME$conn) or die(mysql_error());

    $traffic_time time();
    $traffic_reset mktime(0,0,0,date("n"),date("j"),date("Y"));
    $traffic_reset2 mktime(0,2,0,date("n"),date("j"),date("Y"));

    //////////////////////////////////////////////////////////////////////////////////////////////////////
    //////////////////////////////////////////////////////////////////////////////////////////////////////
    /////////////////////////                                                     /////////////////////////
    /////////////////////////                DO NOT EDIT BELOW THIS LINE !         /////////////////////////
    /////////////////////////                                                     /////////////////////////
    //////////////////////////////////////////////////////////////////////////////////////////////////////
    //////////////////////////////////////////////////////////////////////////////////////////////////////

    $today date("h.i.s_dMY");
    $backup $today "_dbdump.sql";

    if(
    $traffic_time >= $traffic_reset && $traffic_time <= $traffic_reset2)

        
    $data '';
        
    $tables_sql mysql_query("SHOW TABLES") or die(mysql_error());

        while(
    $tables mysql_fetch_row($tables_sql)) 
        {
            
    $tables_create_sql mysql_fetch_row(mysql_query("SHOW CREATE TABLE ".$tables[0])); 
             
    $data .="\r\n#\r\n# Table structure for table `".$tables_create_sql[0]."`\r\n#\r\n\r\n"
             
    $data .="DROP TABLE IF EXISTS `".$tables[0]."`;\r\n"
             
    $data .=str_replace(
        "
    "\r\n""$tables_create_sql[1]").";\r\n\r\n";

             
    $data .="#\r\n# Dumping data for table `".$tables[0]."`\r\n#\r\n\r\n"

             
    $sql mysql_query("SELECT * FROM ".$tables[0]." limit 2500") or die(mysql_error()); 
             
    $num_rows mysql_num_rows($sql); 
             if (
    $num_rows!=0
             { 
                while (
    $row mysql_fetch_assoc($sql)) 
                { 
                    
    $count 0
                    
    $data .="INSERT INTO `".$tables[0]."` VALUES (";
                    
                    foreach (
    $row as $inserts
                    {     
                        
    $data .="'".addslashes($inserts)."'";   
                        
                        
    $count=$count+1
                        if (
    count($row) > $count
                        { 
                           
    $data .=", "
                        } 
                        else 
                        { 
                           
    $data .=""
                        }                 
                    }     
                    
                          
    $data .=");\r\n";               
                } 
             } 
        }
        
    $fp fopen($filelocation.$backup,"w"); 
        
    $writtenfputs($fp$data); 
        
    fclose($fp);
        if(!
    $written)
        {
            echo 
    "backup failed";
        }
        else
        {
            echo 
    "backup successful";
        }
    }    
    exit(); 
    ob_end_flush();
    ?>
    This is non script dependent. It will read your db, all tables and content and back to a file on your server. DO NOT set it to be a public folder, as you are then allowing people access to your DB information freely. If you are on a cpanel based server, then add it before public_html/ like /home/CPANELUSERNAME/bk/ You would then need to login to FTP to back this up.

    If you dont have the option of not sending it to a non public folder, then lock the folder with .htpssd (password protected directory)

    You will need to modify the following to your server settings.

    PHP Code:
    //enter your MySQL database host name, often it is not necessary to edit this line
    define('DBHOST''localhost');

    //enter your MySQL database username
    define('DBUSERNAME''');

    //enter your MySQL database password
    define('DBPASSWORD''');

    //enter your MySQL database name
    define('DBNAME''');

    //full path to the backup directory (this should be a writable directory i.e. chmodd to 777)
    $filelocation "/home/cpanel_username/bk/"
    read the notes next to each field before editing.

    The file is sitelocked so bots, rogue users cannot run the file.
    If you want the file to be run at another time, then you can change

    $traffic_reset = mktime(0,0,0,date("n"),date("j"),date("Y"));
    $traffic_reset2 = mktime(0,2,0,date("n"),date("j"),date("Y"));

    mktime is set in the following order

    mktime(HOURS,MINUTES,SECONDS,MONTH,DAY,YEAR);

    So say you want to run it at 11pm you would change it to

    $traffic_reset = mktime(11,0,0,date("n"),date("j"),date("Y"));
    $traffic_reset2 = mktime(11,2,0,date("n"),date("j"),date("Y"));

    The reason it is set to 2 minutes, is just in case the file gets delayed by your server.

    Setup a cron to be at midnight at an interval of your choosing. The settings you need for the cron to run everyday at midnight are

    0 0 * * * php -q /home/cpanel_username/public_html/sql_backup.php

    you break it down as follows

    Minute: 0
    Hour: 0
    Day: *
    Month: *
    Weekday: *
    Command: php -q /home/cpanel_username/public_html/sql_backup.php

    You can modify this to how many times you want to run the importer.

    Remember to change cpanel_username above to your username.
    Or change the whole path to the correct path to the sql_backup.php file.

    If you wish to test the script without using a cron, then comment out

    PHP Code:
    if($traffic_time >= $traffic_reset && $traffic_time <= $traffic_reset2)

    like

    PHP Code:
    /*
    if($traffic_time >= $traffic_reset && $traffic_time <= $traffic_reset2)

    */ 
    and the last

    PHP Code:

    like

    PHP Code:
    /*
    }
    */ 
    Last but not least, I limited the DB records to be 2500 as my test site was timing out on large table sets. You can always remove this by changing

    PHP Code:
             $sql mysql_query("SELECT * FROM ".$tables[0]." limit 2500") or die(mysql_error()); 
    to

    PHP Code:
             $sql mysql_query("SELECT * FROM ".$tables[0]) or die(mysql_error()); 
    Entirely up to you,

    Enjoy.
    Thanks
    Paul
    I code for FOOOOOD
    Your coding errors make me Orgasm LOL

    Arcade Themes and Mods
    Sniperz Login with Fb and submit your GAMES.
    $5 CPM Popunders, banners and more.

  2. #2
    Senior Member
    Points: 3,536, Level: 39
    Level completed: 24%, Points required for next Level: 114
    Overall activity: 0%
    Achievements:
    Three FriendsTagger First Class1000 Experience PointsRecommendation Second Class1 year registered
    Awards:
    Community Award
    ZyPhiX's Avatar
    Join Date
    Jul 2010
    Location
    the netherlands
    Posts
    112
    Points
    3,536
    Level
    39
    Downloads
    1
    Uploads
    4
    My Mood
    Chatty
    Ok thanks a lot!!! cool :-)

  3. #3
    Mr. Mayor
    Points: 44,635, Level: 100
    Level completed: 0%, Points required for next Level: 0
    Overall activity: 0%
    Achievements:
    Three FriendsRecommendation First ClassCreated Album picturesCreated Blog entryTagger First Class
    Awards:
    Posting Award
    RickyG's Avatar
    Join Date
    Jul 2010
    Location
    To many to list!! Ask me where i am today :)
    Posts
    883
    Points
    44,635
    Level
    100
    Blog Entries
    4
    Downloads
    2
    Uploads
    4
    My Mood
    Confused
    code monkey at work!
    My name is RickyG, and I approved this message!


    http://www.mibbi.com

    http://www.farting.us/

  4. #4
    Code Monkey
    Points: 4,025, Level: 42
    Level completed: 50%, Points required for next Level: 75
    Overall activity: 99.2%
    Achievements:
    Tagger First Class1000 Experience Points1 year registered
    Awards:
    User with most referrers
    lfhost's Avatar
    Join Date
    Jul 2010
    Posts
    313
    Points
    4,025
    Level
    42
    Downloads
    0
    Uploads
    0
    My Mood
    Bored

    Your welcome :P

    Gotta live up to my name LOL.

    But im still hungry for moar lol.
    Thanks
    Paul
    I code for FOOOOOD
    Your coding errors make me Orgasm LOL

    Arcade Themes and Mods
    Sniperz Login with Fb and submit your GAMES.
    $5 CPM Popunders, banners and more.

+ Reply to Thread

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts