Schedule backups with ISPConfig?

Discussion in 'General' started by hmfireball, Jun 3, 2008.

  1. hmfireball

    hmfireball New Member

    ISPConfig allow to manually backup sites to a FTP server.
    But is there a way to automate/schedule the backups and have them sent to a FTP server automatically ?
    Thanks.
    Henri
     
    Last edited: Jun 3, 2008
  2. gjcomputer

    gjcomputer New Member

    i have been looking for this for a while now as well. i like the way the ispconfig web interface backs up everything into a zip file, and you can unzip everything and use what you need. good for migration, and moving servers. very interested in thsi as well :)
     
  3. hmfireball

    hmfireball New Member

    Yes I love the zip thing too!
    There must be an easy way to automate the sending to a FTP server but I'm not enough expert in Linux to know about it. :)
     
  4. falko

    falko Super Moderator Howtoforge Staff

  5. hmfireball

    hmfireball New Member

    Thank you Falco but I would like to automate the sending of the backup files made by ISPConfig.
     
  6. falko

    falko Super Moderator Howtoforge Staff

    You can do this by creating a cron job.
     
  7. hmfireball

    hmfireball New Member

    I'm not sure that it can work as I need to be sure that ISPConfig has finished to write the backup files before trying to send them to the FTP server.

    Wouldn't it be better to include some code after the PHP script of ISPConfig that creates sites backups?
    Do you know the location of this script and where I could append some code? I don't want to break the script and the comments are in German :)

    Thanks!

    Henri
     
  8. falko

    falko Super Moderator Howtoforge Staff

    It's the /root/ispconfig/scripts/shell/backup.php script. :)
     
  9. gjcomputer

    gjcomputer New Member

    hmm, gonna have to figure out how to add this as a cron job to run backup to an ftp server. thanks falko
     
  10. till

    till Super Moderator Staff Member ISPConfig Developer

    This script is not made to run backups on a FTP server. It might be easoier if you write a custom shell script that simply makes a tar.gz file of the /var/www directory (or the place were your webs are stored) and then copy the tar.gz file to the ftp server.
     
  11. gjcomputer

    gjcomputer New Member

    that would work. do you have any tips on doing this?
     
  12. falko

    falko Super Moderator Howtoforge Staff

    You can use the tutorial I posted earlier in this thread.
     
  13. hmfireball

    hmfireball New Member

    My code to automate sending backups to FTP Server

    This the first version of the code I wrote to send backups to an FTP Server.
    It's really simple!
    You just have to add the codes between // HENRI and //~HENRI to the right places (near line 133) of the /root/ispconfig/scripts/shell/backup.php script.
    Beware that this script will send a new backup everyday and thus fill the FTP Server (it's a first version)!
    Henri


    PHP:
    // [...]

                    // Delete temp file
                    
    exec("rm -rf $tmp_dir");
                    
                    
    // HENRI Send file to FTP server
                    
    send_backup($web_id$backup_dir$backup_file_name);
                    
    //~HENRI

    }

    // All web site
    $webs $mod->db->queryAllRecords("SELECT * FROM isp_isp_web");
    if(!empty(
    $webs)){
      foreach(
    $webs as $web){
        
    do_backup($web['doc_id']);
      }
    }

    // HENRI
    function send_backup($web_id$backup_dir$backup_file_name) {

        
    // Replace by your own parameters
        
    $ftp_server 'XXXXXXXX';
        
    $ftp_user_name 'XXXXXXXX';
        
    $ftp_user_pass 'XXXXXXXXX';
        
        
    // Connect and login to FTP server
        
    $conn_id = @ftp_connect($ftp_server);
        
    $login_result = @ftp_login($conn_id$ftp_user_name$ftp_user_pass);
        if (!
    $conn_id || !$login_result) {
            echo 
    "FTP connection failed for $ftp_user_name!\n";
            return 
    false;
        }
        else {
            echo 
    "$ftp_user_name connected\n";
        }
        
        
    // Create web_dir if necessary
        
    $web_dir '/web'.$web_id;
        if (@
    ftp_mkdir($conn_id$web_dir)) {
            echo 
    "Created $web_dir\n";
        }
        
        
    $destination_file $web_dir.'/'.$backup_file_name;
        
    $source_file $backup_dir.'/'.$backup_file_name;
        
        
    // Upload backup
        
    $upload = @ftp_put($conn_id$destination_file$source_fileFTP_BINARY);
        
        
    // Display result
        
    if (!$upload) {
            echo 
    "Failed sending $destination_file\n";
        }
        else {
            echo 
    "Successed sending $destination_file\n";
        }
        
        
    // Close FTP connection
        
    @ftp_close($conn_id);
        
        return 
    $upload;
    }
    //~HENRI
     
    Last edited: Jun 18, 2008
  14. gjcomputer

    gjcomputer New Member

    thanks bud im gonna give this a try. maybe 2nd version can overwrite after a set amount of time? :cool:

     
  15. vaio1

    vaio1 Member

  16. Jorem

    Jorem Member

    I use the filesystem backup function in Webmin.

    Very easy. You can shedule backups. Use SSH or FTP server. And you get an email on succes.
     
    Last edited: Jun 19, 2008
  17. hmfireball

    hmfireball New Member

    My code to automate sending backups to FTP Server (version 2)

    This version removes old backup from FTP server after a given number of days.
    Add the codes between // HENRI and //~HENRI to the right places in the /root/ispconfig/scripts/shell/backup.php script.

    PHP:
    set_time_limit(0);

    include(
    "/root/ispconfig/scripts/lib/config.inc.php");
    include(
    "/root/ispconfig/scripts/lib/server.inc.php");

    if(
    $go_info["server"]["do_automated_backups"] != 1) die();

    // Erstelle Namen für Backup Datei
    $backup_file_name "backup_".date("Y_m_d",time()).".zip";

    // HENRI
    // Replace with your own parameters
    define('FTP_SERVER''XXXXXXX');
    define('FTP_USER_NAME''XXXXXXXXX');
    define('FTP_USER_PASS''XXXXXXXXX');
    define('FTP_DAYS'20); // the script will keep FTP backups this number of days. Min value is 1.

    // set old_backup_file_name
    $old_time mktime(000date('m'), date('d') - FTP_DAYSdate('Y'));
    $old_backup_file_name 'backup_'.date('Y_m_d'$old_time).'.zip';
    //~HENRI



    // [...]



                    // Delete temp file
                    
    exec("rm -rf $tmp_dir");
                    
                    
    // HENRI Send file to FTP server
                    
    send_backup($web_id$backup_dir);
                    
    //~HENRI

    }

    // All web site
    $webs $mod->db->queryAllRecords("SELECT * FROM isp_isp_web");
    if(!empty(
    $webs)){
      foreach(
    $webs as $web){
        
    do_backup($web['doc_id']);
      }
    }

    // HENRI
    function send_backup($web_id$backup_dir) {
        global 
    $backup_file_name$old_backup_file_name;
        
        
    // Connect and login to FTP server
        
    $conn_id = @ftp_connect(FTP_SERVER);
        
    $login_result = @ftp_login($conn_idFTP_USER_NAMEFTP_USER_PASS);
        if (!
    $conn_id || !$login_result) {
            echo 
    "FTP connection failed for ".FTP_USER_NAME."!\n";
            return 
    false;
        }
        else {
            echo 
    FTP_USER_NAME." connected\n";
        }
        
        
    // Create web_dir if necessary
        
    $web_dir '/web'.$web_id;
        if (@
    ftp_mkdir($conn_id$web_dir)) {
            echo 
    "Created $web_dir\n";
        }
        
        
    // Upload backup
        
    $destination_file $web_dir.'/'.$backup_file_name;
        
    $source_file $backup_dir.'/'.$backup_file_name;
        
    $uploaded = @ftp_put($conn_id$destination_file$source_fileFTP_BINARY);
        
        
    // Display result
        
    if (!$uploaded) {
            echo 
    "Failed sending $destination_file\n";
        }
        else {
            echo 
    "Successed sending $destination_file\n";
            
            
    // Remove old backup
            
    $destination_file $web_dir.'/'.$old_backup_file_name;
            if (@
    ftp_delete($conn_id$destination_file)) {
                echo 
    "Removed old backup: $destination_file\n";
            }
        }
        
        
    // Close FTP connection
        
    @ftp_close($conn_id);
        
        return 
    $uploaded;
    }
    //~HENRI



    Henri
    http://www.absyx.fr
     
  18. gjcomputer

    gjcomputer New Member

    thanks once again, this one is easier to follow. what is the best way to test and make sure this works? i tried running the php manually, and it doesnt do anything
     
  19. hmfireball

    hmfireball New Member

    Before you run the script, you must enable automated backup in ISPConfig's config file.

    In /home/admispconfig/ispconfig/lib/config.inc.php, find the line:
    PHP:
    $go_info["server"]["do_automated_backups"] = 0// 0 = no, 1 = yes; PLEASE NOTE: automated backups might fill up your HDD fast!


    and set it to 1:
    PHP:
    $go_info["server"]["do_automated_backups"] = 1// 0 = no, 1 = yes; PLEASE NOTE: automated backups might fill up your HDD fast!


    Save and restart ISPConfig server (not sure it's needed though):
    Code:
    /etc/init.d/ispconfig_server restart
    
    Then try to run the script manually:
    Code:
    /root/ispconfig/php/php /root/ispconfig/scripts/shell/backup.php
    Backup should start and you should see logs about backup files being sent to the FTP Server.
    Once it's done, connect to the FTP Server and check if the files are where they're expected to be :)

    Henri
    http://www.absyx.fr
     
    Last edited: Jun 20, 2008
  20. gjcomputer

    gjcomputer New Member

    got it, thanks. this script looks like what i wanted too.

    under ftp server, the correct syntax is
    Code:
    define('FTP_SERVER', '192.168.1.105/array1/web/');
    correct?
     
    Last edited: Jun 20, 2008

Share This Page