mySQL backup w/ php

Discussion in 'OT Technology' started by Slid., Apr 21, 2005.

  1. Slid.

    Slid. I'm a guy.

    Joined:
    Oct 25, 2001
    Messages:
    1,928
    Likes Received:
    0
    Location:
    NH
    I have a database that I would like to backup daily but I haven't found a good script or a command for it. I want the backup to happen like the phpmyadmin export where all the insert, create statements are saved.

    Anyone have a good script that they use?

    I need something that doesn't require user intervention.
     
  2. intrktevo

    intrktevo New Member

    Joined:
    Oct 18, 2004
    Messages:
    5,781
    Likes Received:
    0
    Location:
    UCF
    http://www.mosix.nl/mysqldumper/

    -here is an example i got from devshed forums on how to use that class to save as a txt file
    -as soon as you can get this working the only thing left to do is set this script to run as a cron job via your control panel
    PHP:
    //msql dumper class set up
    $dumper = new MysqlDumper("localhost""root""""");
    $dumpstring $dumper->createDump();

    //get the export content from the dump class
    $export_content $dumpstring;

    //Creat a text file to download
    $filedate date(dmy);
    $filename "db_$filedate.sql";
    $fp fopen("../_backup/$filename""w") or die("couldn't write to $filename");
    fwrite($fp"$export_content");
    fclose($fp);
     
    Last edited: Apr 22, 2005
  3. PocoDiablo

    PocoDiablo New Member

    Joined:
    Jul 11, 2004
    Messages:
    4,413
    Likes Received:
    0
    Location:
    NOVA
    Just make a cron job like this:

    mysqldump --opt --user=username --password=password database_name > /home/username/db_backup.sql

    If you want a script that backs up your entire site, then FTPs it to another server, AND you are using cPanel, use this:

    Code:
    <?php
    
    // PHP script to allow periodic cPanel backups automatically.
    // Based on script posted by max.hedroom in cpanel.net forums
    //   This script contains passwords.  KEEP ACCESS TO THIS FILE SECURE!
    
    // ********* THE FOLLOWING ITEMS NEED TO BE CONFIGURED *********
    
    // Info required for cPanel access
    $cpuser = "name"; // Username used to login to CPanel
    $cppass = "pass"; // Password used to login to CPanel
    $domain = "site.com"; // Domain name where CPanel is run
    $skin = "x"; // Set to cPanel skin you use (script won't work if it doesn't match)
    
    // Info required for FTP host
    $ftpuser = "anonymous"; // Username for FTP account
    $ftppass = "none"; // Password for FTP account
    $ftphost = "ftp.site.com"; // Full hostname or IP address for FTP host
    $ftpmode = "ftp"; // FTP mode ("ftp" for active, "passiveftp" for passive)
    
    // Notification information
    $notifyemail = "[email protected]"; // Email address to send results
    
    // Secure or non-secure mode
    $secure = 0; // Set to 1 for SSL (requires SSL support), otherwise will use standard HTTP
    
    // Set to 1 to have web page result appear in your cron log
    $debug = 1;
    
    // *********** NO CONFIGURATION ITEMS BELOW THIS LINE *********
    
    if ($secure) {
       $url = "ssl://".$domain;
       $port = 2083;
    } else {
       $url = $domain;
       $port = 2082;
    }
    
    $socket = fsockopen($url,$port);
    if (!$socket) { echo "Failed to open socket connection... Bailing out!\n"; exit; }
    
    // Encode authentication string
    $authstr = $cpuser.":".$cppass;
    $pass = base64_encode($authstr);
    
    $params = "dest=$ftpmode&email=$notifyemail&server=$ftphost&user=$ftpuser&pass=$ftppass&submit=Generate Backup";
    
    // Make POST to cPanel
    fputs($socket,"POST /frontend/".$skin."/backup/dofullbackup.html?".$params." HTTP/1.0\r\n");
    fputs($socket,"Host: $domain\r\n");
    fputs($socket,"Authorization: Basic $pass\r\n");
    fputs($socket,"Connection: Close\r\n");
    fputs($socket,"\r\n");
    
    // Grab response even if we don't do anything with it.
    while (!feof($socket)) {
      $response = fgets($socket,4096);
      if ($debug) echo $response;
    }
    echo 'Done.';
    fclose($socket);
    
    ?>
    
    Just make a cron job to run that, like:

    /usr/bin/GET http://www.site.com/files/backup.php > /dev/null

    This code MUST be run from a DIFFERENT server than the one you are backing up. I.e., put it on web server one, then have it backup web server two, and vice versa. The FTP server doesn't matter.

    Enjoy.
     
  4. Slid.

    Slid. I'm a guy.

    Joined:
    Oct 25, 2001
    Messages:
    1,928
    Likes Received:
    0
    Location:
    NH
    Excellent, thank you both.

    I tried the mysqldump after posting and it kept bombing on me - mysqldumper worked great though!
     

Share This Page