1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

PHP script to ZIP a folder and transfer via FTP using cron

Discussion in 'PHP' started by CandyCottonLover, Aug 10, 2009.

Thread Status:
Not open for further replies.
  1. #1
    I need help with php script that can zip a folder inside Server A to be transfered to Server B via ftp. I already know how to transfer a sql dump from A to B using cron using the script that can be found here:

    http://www.netbuilders.org/programming/database-backup-email-ftp-via-cron-5088.html

    Just for reference:
    <?
    $datestamp = date("Y-m-d");      // Current date to append to filename of backup file in format of YYYY-MM-DD
    
    /* CONFIGURE THE FOLLOWING THREE VARIABLES TO MATCH YOUR SETUP */
    $dbuser = "";      // Database username
    $dbpwd = "";         // Database password
    $dbname = "";      // Database name. Use --all-databases if you have more than one
    $filename= "backup-$datestamp.sql.gz";   // The name (and optionally path) of the dump file
    
    $command = "mysqldump -u $dbuser --password=$dbpwd $dbname | gzip > $filename";
    $result = passthru($command);
    
    /* CONFIGURE THE FOLLOWING FOUR VARIABLES TO MATCH YOUR FTP SETUP */
    $ftp_server = "";   // Shouldn't have any trailing slashes and shouldn't be prefixed with ftp://
    $ftp_port = "21";            // FTP port - blank defaults to port 21
    $ftp_username = "anonymous";         // FTP account username
    $ftp_password = "";         // FTP account password - blank for anonymous
    
    // set up basic connection
    $ftp_conn = ftp_connect($ftp_server);
    
    // Turn PASV mode on or off
    ftp_pasv($ftp_conn, false);
    
    // login with username and password
    $login_result = ftp_login($ftp_conn, $ftp_username, $ftp_password);
    
    // check connection
    if ((!$ftp_conn) || (!$login_result))
    {
       echo "FTP connection has failed.";
       echo "Attempted to connect to $ftp_server for user $ftp_username";
       exit;
    }
    else
    {
       echo "Connected to $ftp_server, for user $ftp_username";
    }
    
    // upload the file
    $upload = ftp_put($ftp_conn, $filename, $filename, FTP_BINARY);
    
    // check upload status
    if (!$upload)
    {
       echo "FTP upload has failed.";
    }
    else
    {
       echo "Uploaded $filename to $ftp_server.";
    }
    
    // close the FTP stream
    ftp_close($ftp_conn);
    
    unlink($filename);   //delete the backup file from the server
    ?>
    PHP:
    Now to perfect this I need a script that zip a single folder let say a "thumb" folder which contain thumbnails for images. It will zip the folder with random file name or a static file name(perefered) and it will and once the zip/tar/gzip process is done it will transfer it via ftp and notified via email or just display information saying the process is done.

    The reason for this script is to make a remote backup system which can also be used to have another copy of website with a diff domain but have the same content sync on from main server.

    RSYNC is good but im trying alternative for it using php and cron :)
    SEMrush
     
    CandyCottonLover, Aug 10, 2009 IP
    SEMrush
  2. jestep

    jestep Prominent Member Premium Member

    Messages:
    3,661
    Likes Received:
    215
    Best Answers:
    19
    Trophy Points:
    330
    #2
    This script should do exactly what you need, just replace the mysql dump, with the path to the directory you want to compress.

    Something like this:
    
    <?php
    $datestamp = date("Y-m-d");      // Current date to append to filename of backup file in format of YYYY-MM-DD
    
    /* CONFIGURE THE FOLLOWING THREE VARIABLES TO MATCH YOUR SETUP */
    $directory_path = "/path/to/my/dir";
    $dump_file_name = '/path/to/backup/My_backuo_dir_'.$datestamp.'.tgz';
    
    $command = "tar -zcvf {$dump_file_name} {$directory_path}";
    $result = exec($command,$output);
    
    /* CONFIGURE THE FOLLOWING FOUR VARIABLES TO MATCH YOUR FTP SETUP */
    $ftp_server = "";   // Shouldn't have any trailing slashes and shouldn't be prefixed with ftp://
    $ftp_port = "21";            // FTP port - blank defaults to port 21
    $ftp_username = "anonymous";         // FTP account username
    $ftp_password = "";         // FTP account password - blank for anonymous
    
    // set up basic connection
    $ftp_conn = ftp_connect($ftp_server);
    
    // Turn PASV mode on or off
    ftp_pasv($ftp_conn, false);
    
    // login with username and password
    $login_result = ftp_login($ftp_conn, $ftp_username, $ftp_password);
    
    // check connection
    if ((!$ftp_conn) || (!$login_result))
    {
       echo "FTP connection has failed.";
       echo "Attempted to connect to $ftp_server for user $ftp_username";
       exit;
    }
    else
    {
       echo "Connected to $ftp_server, for user $ftp_username";
    }
    
    // upload the file
    $upload = ftp_put($ftp_conn, $dump_file_name, $dump_file_name, FTP_BINARY);
    
    // check upload status
    if (!$upload)
    {
       echo "FTP upload has failed.";
    }
    else
    {
       echo "Uploaded $filename to $ftp_server.";
    }
    
    // close the FTP stream
    ftp_close($ftp_conn);
    
    unlink($dump_file_name);   //delete the backup file from the server
    ?>
    
    PHP:
     
    jestep, Aug 10, 2009 IP
    CandyCottonLover likes this.
  3. CandyCottonLover

    CandyCottonLover Banned

    Messages:
    93
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Owh well I should try this :) I'll give out more info once I'm done with this :)

    ================= Result = Success with some fix ================================

    Just a few changed need to be done on the $dump_file_name as it doesnt need a path to store the backup as it will automatically store the file inside the same folder of where you call the script.

    $directory_path = "/path/to/my/dir/"; // This will result the tar file will be compile with in tree dir

    So to avoid your file will be placed inside nested folder you can use this:
    $directory_path = "dir/";

    Place this script inside the the folder where the folder you want to backup is in eg:

    You wanna backup thumb folder:
    /home/username/public_html/thumb/

    So place the script at
    /home/username/public_html/backup-script.php

    I wonder if there is another command that can tar a folder path without including the path inside the gzip file. If you know please help me make this perfect :)

    Fix for filename:
    $dump_file_name = "backup-$datestamp.tgz";

    <?php
    $datestamp = date("Y-m-d");      // Current date to append to filename of backup file in format of YYYY-MM-DD
    
    /* CONFIGURE THE FOLLOWING THREE VARIABLES TO MATCH YOUR SETUP */
    $directory_path = "/path/to/my/dir/";
    $dump_file_name = "backup-$datestamp.tgz";
    
    $command = "tar -zcvf {$dump_file_name} {$directory_path}";
    $result = exec($command,$output);
    
    /* CONFIGURE THE FOLLOWING FOUR VARIABLES TO MATCH YOUR FTP SETUP */
    $ftp_server = "";   // Shouldn't have any trailing slashes and shouldn't be prefixed with ftp://
    $ftp_port = "21";            // FTP port - blank defaults to port 21
    $ftp_username = "anonymous";         // FTP account username
    $ftp_password = "";         // FTP account password - blank for anonymous
    
    // set up basic connection
    $ftp_conn = ftp_connect($ftp_server);
    
    // Turn PASV mode on or off
    ftp_pasv($ftp_conn, false);
    
    // login with username and password
    $login_result = ftp_login($ftp_conn, $ftp_username, $ftp_password);
    
    // check connection
    if ((!$ftp_conn) || (!$login_result))
    {
       echo "FTP connection has failed.";
       echo "Attempted to connect to $ftp_server for user $ftp_username";
       exit;
    }
    else
    {
       echo "Connected to $ftp_server, for user $ftp_username";
    }
    
    // upload the file
    $upload = ftp_put($ftp_conn, $dump_file_name, $dump_file_name, FTP_BINARY);
    
    // check upload status
    if (!$upload)
    {
       echo "FTP upload has failed.";
    }
    else
    {
       echo "Uploaded $filename to $ftp_server.";
    }
    
    // close the FTP stream
    ftp_close($ftp_conn);
    
    unlink($dump_file_name);   //delete the backup file from the server
    ?>
    PHP:
    Thank you jestep for making this happen ^^ ++ rep
     
    Last edited: Aug 10, 2009
    CandyCottonLover, Aug 10, 2009 IP
Thread Status:
Not open for further replies.