in Search
     
Latest post 09-27-2005 4:32 PM by keithrull. 3 replies.
Page 1 of 1 (4 items)
Sort Posts: Previous Next
  • 09-25-2005 11:39 PM

    • fedora
    • Top 200 Contributor
    • Joined on 08-31-2005
    • Posts 3
    • Points 60

    MYSQL BACKUP using PHP Codes

    Guys! help me naman in backing-up mysql db using a php codes..lalagyan ko sana ng backup tool un aking online inventory system..tnx alot..
    • Post Points: 20
  • 09-26-2005 6:23 PM In reply to

    • keithrull
    • Top 10 Contributor
    • Joined on 08-08-2005
    • San Diego, CA
    • Posts 1,956
    • Points 39,255

    Re: MYSQL BACKUP using PHP Codes

    What type of backup are you thinking of doing? database schema? or database values?

    devpinoy sig

    • Post Points: 20
  • 09-27-2005 3:28 AM In reply to

    • fedora
    • Top 200 Contributor
    • Joined on 08-31-2005
    • Posts 3
    • Points 60

    Re: MYSQL BACKUP using PHP Codes

    actually as in buong database eh-export nya as <dbname><timestamp>.sql format...parang sa VB..back-up..gets po?Thanks
    • Post Points: 20
  • 09-27-2005 4:32 PM In reply to

    • keithrull
    • Top 10 Contributor
    • Joined on 08-08-2005
    • San Diego, CA
    • Posts 1,956
    • Points 39,255

    Re: MYSQL BACKUP using PHP Codes

    Try this:

    There are at least three ways to backup your MySQL Database :

    1.) Execute a database backup query from PHP file.
    2.) Run mysqldump using system() function.
    3.) Use phpMyAdmin to do the backup.
     
     
    Execute a database backup query from PHP file
    Below is an example of using SELECT INTO OUTFILE query for creating table backup :


    <?php
    include 'config.php';
    include 'opendb.php';

    $tableName  = 'mypet';
    $backupFile = 'backup/mypet.sql';
    $query      = "SELECT * INTO OUTFILE '$backupFile' FROM $tableName";
    $result = mysql_query($query);


    include 'closedb.php';
    ?>


    To restore the backup you just need to run LOAD DATA INFILE query like this :


    <?php
    include 'config.php';
    include 'opendb.php';

    $tableName  = 'mypet';
    $backupFile = 'mypet.sql';
    $query      = "LOAD DATA INFILE 'backupFile' INTO TABLE $tableName";
    $result = mysql_query($query);


    include 'closedb.php';
    ?>


    It's a good idea to name the backup file as tablename.sql so you'll know from which table the backup file is


    Run mysqldump using system() function
    The system() function is used to execute an external program. Because MySQL already have built in tool for creating MySQL database backup (mysqldump) let's use it from our PHP script


    <?php
    include 'config.php';
    include 'opendb.php';

    $backupFile = $dbname . date("Y-m-d-H-i-s") . '.gz';
    $command = "mysqldump --opt -h $dbhost -u $dbuser -p $dbpass $dbname | gzip > $backupFile";
    system($command);

    include 'closedb.php';
    ?>


     
    Use phpMyAdmin to do the backup
    This option as you may guessed doesn't involve any programming on your part. However I think i mention it anyway so you know more options to backup your database.

    To backup your MySQL database using phpMyAdmin click on the "export" link on phpMyAdmin main page. Choose the database you wish to backup, check the appropriate SQL options and enter the name for the backup file.


     

    devpinoy sig

    • Post Points: 5
Page 1 of 1 (4 items)