Mysql backup using php script

Discussion in 'PHP' started by technoguy, Jan 15, 2007.

Thread Status:
Not open for further replies.
  1. #1
    I want to make php script which will take backup of mysql database. I dont want to use phpmyadmin, I want using it php script. Is it possible to execute mysqldump command using php?

    Thanks in advance
     
    technoguy, Jan 15, 2007 IP
  2. agnivo007

    agnivo007 Peon

    Messages:
    4,290
    Likes Received:
    289
    Best Answers:
    0
    Trophy Points:
    0
    #2
    agnivo007, Jan 15, 2007 IP
  3. sundaybrew

    sundaybrew Numerati

    Messages:
    7,294
    Likes Received:
    1,260
    Best Answers:
    0
    Trophy Points:
    560
    #3
    sundaybrew, Jan 15, 2007 IP
  4. chovy

    chovy Peon

    Messages:
    181
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #4
    rsnapshot has one written in Perl - could easily convert it to php.
     
    chovy, Jan 15, 2007 IP
  5. EvilivE

    EvilivE Peon

    Messages:
    23
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #5
    This is what I have been using for years.
    
    <?
    
    function backupDBs(){
    
      $link = mysql_connect("localhost","root","PASSWORD");
      $result = mysql_query("show databases", $link);
    
      while($row = mysql_fetch_assoc($result)) {
        mysql_query("use ".$row["Database"], $link);
        $resultTables = mysql_query("show tables");
        while($rowTables = mysql_fetch_assoc($resultTables)) {
          system("mysqldump --user=root --password=PASSWORD ".$row["Database"]." ".$rowTables["Tables_in_".$row["Database"]]." > /var/www/dbBackups/".date("mdY")."_".$row["Database"]."_".$rowTables["Tables_in_".$row["Database"]].".sql");
        }
      }
    
      system("tar --create --gzip --file /var/www/dbBackups/".date("mdY").".tar.gz /var/www/dbBackups/*.sql");
      system("rm -f /var/www/dbBackups/*.sql");
      system("mv /var/www/dbBackups/*.gz /mnt/zip/db_tables/".date("m")."/");
    }
    
    function backupWWW(){
      system("cp -uR --reply=yes /var/www/html /mnt/zip/www");
      system("cp -uR --reply=yes /var/www/php-includes /mnt/zip/www");
    }
    
    if(date("d") == 01){
    
      // perform 1st of month mtce
      // rm -f current months directory
      system("rm -f /mnt/zip/db_tables/".date("d")."/*");
    
      // run backup routine
      backupDBs();
      backupWWW();
    } else {
    
      backupDBs();
      backupWWW();
    }
    
    ?>
    
    PHP:
     
    EvilivE, Jan 15, 2007 IP
  6. Ch3W

    Ch3W Member

    Messages:
    31
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    41
    #6
    Well technically, phpMyAdmin is a php script ;)
     
    Ch3W, Jan 16, 2007 IP
  7. sukantab

    sukantab Well-Known Member

    Messages:
    2,075
    Likes Received:
    49
    Best Answers:
    0
    Trophy Points:
    110
    #7
    Thanks a lot EviLive....
    Will try this script today itself....
    I was also looking for this....
     
    sukantab, Jan 16, 2007 IP
Thread Status:
Not open for further replies.