Database Code

  Home arrow Database Code arrow mysqlphpbak
DATABASE CODE

mysqlphpbak
By: Codewalkers
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 2
    2002-01-18

    Table of Contents:

     
     

    SEARCH CODEWALKERS

    TOOLS YOU CAN USE

    advertisement
    Simple mysql utility that dumps data from all databases on a server into a date stamped directory - can either be called either through a web site or automated with a PHP binary and entry into your crontab.

    By : ov_m

    <?php
    //Full Source at http://www.ov-m.com/mysqlphpbak/

    $MYSQL_SERVER="localhost"; // server name
    $MYSQL_USER="foo"; // authorized mysql user
    $MYSQL_PASSWD="bar"; // authorized user password
    $PATHTOMYSQLDUMP="/www/bin/"; // path to mysqldump utility

    $DIRNAME="/path/to/database/backup/dir/"; // where to do the backups
    $DIRNAME.=date("Ymd"); // add date identifier
    $DIRNAME.="/"; // add trailing slash

    echo "Creating New Back Up Directory\n\n";

    echo "Using Directory : $DIRNAME<br>\n";


    if (@mkdir ($DIRNAME,0700)) {
    echo "New Directory Created<br>\n";
    } else {
    echo "Directory already exists!\n";
    }

    echo "Backing Up all Databases on $HOSTNAME\n\n";

    //***************************************************************************

    mysql_connect($MYSQL_SERVER,$MYSQL_USER,$MYSQL_PASSWD);

    $result = mysql_list_dbs();
    $i = 0;
    while ($i < mysql_num_rows ($result)) {
    $tb_names[$i] = mysql_tablename ($result, $i);
    $i++;
    }
    //***************************************************************************
    // Loop through the table names, and do the dump.
    //***************************************************************************
    for ($i=0; $i<count($tb_names); $i++) {

    $COMMAND_DO=$PATHTOMYSQLDUMP. "mysqldump -h".$MYSQL_SERVER." -u".$MYSQL_USER." -p".$MYSQL_PASSWD." ".$tb_names[$i]." > ".$DIRNAME.$tb_names[$i].".sql";

    //echo $COMMAND_DO."\n"; // uncomment these lines if you want to see all
    //flush(); // back-ups made on the server

    exec($COMMAND_DO); // execute each backup

    }

    echo "\n\nDONE!\n";



    ?>
    DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

    More Database Code Articles
    More By Codewalkers

    blog comments powered by Disqus

    DATABASE CODE ARTICLES

    - Converting CSV Files to MySQL Insert Queries...
    - Examples and Tools for Database Design
    - Relationships, Entities and Database Design
    - Modeling and Designing Databases
    - Data extract to Excel
    - Oracle database class 0.76
    - The opposite of mysql_fetch_assoc
    - On line Thermal Transmitance Calculation
    - pjjTextBase
    - PHP Object Generator
    - FastMySQL
    - RC4PHP
    - SQL function with integrated sprintf()
    - DB Interaction Classes v1.1
    - deeMySQLParser


    © 2003-2012 by Developer Shed. All rights reserved. DS Cluster 7 - Follow our Sitemap