Auto backup MySQL dbase to FTP server?

Discussion in 'PHP' started by Amsterdam, Mar 4, 2009.

  1. #1
    Hi,

    I am trying to create a small script that will backup a MySQL database. I can successfully create the backup file at the root of my Website using:

    <?php
    
      // This PHP Script will backup the content of a MySQL database in a GZip file
    
      // Enter Database access details
      $host= 'HOSTNAME';
      $user= 'USERNAME';
      $pass= 'PASSWRD';
      $db=   'DBNAME';
      
      // Instructing the system to zip and store the database
      system(sprintf(
    
        'mysqldump --opt -h%s -u%s -p%s %s | gzip > $s/dumpDB.sql.gz',
        $host,
        $user,
        $pass,
        $db,
        getenv('DOCUMENT_ROOT')
      ));
      echo '+DONE';
    ?>
    Code (markup):
    However, I would like the script to save the file to a FTP server elsewhere. I have added the variables for the FTP server, like so:

    <?php
    
      // This PHP Script will backup the content of a MySQL database in a GZip file
    
      // Enter Database access details
      $host= 'HOSTNAME';
      $user= 'USERNAME';
      $pass= 'PASSWRD';
      $db=   'DBNAME';
      
      // FTP Server access details
      $ftphost= 'FTPHOSTHAME';
      $ftpdir= 'FTPDIR';
      $ftpuser= 'FTPUSERNAME';
      $ftppass= 'FTPPASSWRD';
    
      // Instructing the system to zip and store the database
      system(sprintf(
    
        'mysqldump --opt -h%s -u%s -p%s %s | gzip > $s/dumpDB.sql.gz',
        $host,
        $user,
        $pass,
        $db,
        getenv('DOCUMENT_ROOT')
      ));
      echo '+DONE';
    ?>
    Code (markup):
    But what do I need to add to mysqldump for the file to be sent to the FTP server?

    I've been searching for hours and can't find a solution that works. Any help will be much appreciated.

    Many thanks,

    T
     
    Amsterdam, Mar 4, 2009 IP
  2. SmallPotatoes

    SmallPotatoes Peon

    Messages:
    1,321
    Likes Received:
    41
    Best Answers:
    0
    Trophy Points:
    0
    #2
    With any luck your server has a scriptable FTP client on it. ncftpput is an easy one to work with. But it's hard to guess what software is installed on your server.
     
    SmallPotatoes, Mar 4, 2009 IP
  3. LaCReMeL

    LaCReMeL Member

    Messages:
    3
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    36
    #3
    	
    $file = "dumpDB.sql.gz";
    $remotefile = "dumpDB.sql.gz";
    //ftp connection
    $conn_id = ftp_connect($ftphost);
    $login_result = ftp_login($conn_id, $ftpuser, $ftppass);
    if ((!$conn_id) || (!$login_result))
    {
        echo "Cannot Connect To FTP!!<br>";
        exit;
    } else
    {
        echo "FTP Connection Established; ".$ftp_server.", User : ".$ftp_user_name." <br>";
    }
    //change dir
    if (ftp_chdir($conn_id, $ftpdir))
    { 
        echo "Dir Changed To: " . ftp_pwd($conn_id) . "\n";
    } else
    {
        echo "Dir Cannot Changed\n";
    }
    //upload file
    if (ftp_put($conn_id, $remote_file, $file, FTP_ASCII))
    {
        echo "File Uploaded! $file\n <br>";
    } else
    {
        echo "File Cannot Uploaded! : $file\n <br>";
    }
    //close connection
    ftp_close($conn_id);
    
    PHP:
    i write a sample for you. put codes before echo '+DONE';
     
    LaCReMeL, Mar 4, 2009 IP
  4. Amsterdam

    Amsterdam Well-Known Member

    Messages:
    361
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    128
    #4
    Many thanks LaCReMel,

    I added the code, but unfortunately it isn't working properly. It saves a local copy, but not to the FTP server.

    It appears to connect and change directory ok, but not transfer the actual file. The message 'File Cannot Uploaded!' is displayed and when I check the FTP server the file isn't there.

    Any ideas?

    Thanks again in advance,

    T
     
    Amsterdam, Mar 5, 2009 IP
  5. LaCReMeL

    LaCReMeL Member

    Messages:
    3
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    36
    #5
    change FTP_ASCII to FTP_BINARY and try again please
     
    LaCReMeL, Mar 5, 2009 IP
  6. Amsterdam

    Amsterdam Well-Known Member

    Messages:
    361
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    128
    #6
    I tried that. I also changed the $remote_file variable to $remotefile but still no luck unfortunately.

    T
     
    Amsterdam, Mar 5, 2009 IP
  7. ez-designs

    ez-designs Well-Known Member

    Messages:
    230
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    105
    #7
    Are you changing his variables to match your variables?
     
    ez-designs, Mar 5, 2009 IP
  8. Amsterdam

    Amsterdam Well-Known Member

    Messages:
    361
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    128
    #8
    Yes. I now have:

    <?php
    
      // Enter Database access details
      $host= 'HOSTNAME';
      $user= 'USERNAME';
      $pass= 'PASSWRD';
      $db=   'DBNAME';
      
      // FTP Server access details
      $ftphost= 'FTPHOSTNAME';
      $ftpdir= 'FTPDIR';
      $ftpuser= 'FTPUSERNAME';
      $ftppass= 'FTPPASSWRD';
    
      // Instructing the system to zip and store the database
      system(sprintf(
        'mysqldump --opt -h%s -u%s -p%s %s | gzip > %s/dumpDB.sql.gz',
        $host,
        $user,
        $pass,
        $db,
        getenv('DOCUMENT_ROOT')
      ));
      
      $file = "dumpDB.sql.gz";
      $remotefile = "dumpDB.sql.gz";
      
      //ftp connection
      $conn_id = ftp_connect($ftphost);
      $login_result = ftp_login($conn_id, $ftpuser, $ftppass);
      if ((!$conn_id) || (!$login_result))
      {
          echo "Cannot Connect To FTP!!<br>";
          exit;
      }
      else{
          echo "FTP Connection Established; ".$ftphost.", User: &nbsp;".$ftpuser." <br>";
           }
    
      //change dir
      if (ftp_chdir($conn_id, $ftpdir))
      {
           echo "Dir Changed To: " . ftp_pwd($conn_id) . " <br>";
      }
      else{
          echo "Dir Cannot Be Changed\n";
          }
    
      //upload file
      if (ftp_put($conn_id, $remotefile, $file, FTP_BINARY))
      {
          echo "File $file\n Uploaded! <br>";
      }
      else{
          echo "File $file\n Cannot Be Uploaded! <br>";
          }
    
      //close connection
      ftp_close($conn_id);
      
      echo '+DONE' ;
    ?>
    Code (markup):
    But unfortunately it doesn't work :(

    T
     
    Amsterdam, Mar 6, 2009 IP