PHP script for backing up FTP data: How to set up the script

Step 1:

In order to make a backup of your FTP data, you can use the PEAR package Archive_Tar within a PHP script. This PEAR package is installed on our servers by default. Here you can see an example of use:

1. Create a folder (e.g. "backup") inside your account.

2. Create a subfolder named "archive" and determine 0777 as access privileges (CHMOD).

3. In the "backup" folder, please also create a PHP file named "ftp-backup.php" containing the following code:

<?
    // Optimize PHP configuration
    @error_reporting(E_ALL ^ E_WARNING);
    @ini_set("max_execution_time", 300);
    @ini_set("memory_limit", "256M");
    
    // Subfolder to be backed up. Remains empty in case the entire account should be backed up.
    $verzeichnis = "";
    
    // Resources that must be excluded
    $ignorieren = array("*.sql.gz", "*.tar.gz", "*.tgz", "kas_backup", "usage", "logs");

    // Invclude PEAR class and create archive
    $pfad = preg_replace('/(\/www\/htdocs\/\w+\/).*/', '$1', realpath(__FILE__));
    include "Archive/Tar.php";
    $archivname = "archiv/".preg_replace('/.+\/(.+)\/$/', '$1', $pfad).date('_Y-m-d_His').".tar.gz";
    $archiv = new Archive_Tar($archivname, true);
    $archiv->setIgnoreList($ignorieren);
    $archiv->createModify($pfad.$verzeichnis, "", $pfad);
    echo "Backup finished";
?>


4. In case of backing up only a single folder, please enter the path to that folder in the following variable:

    $verzeichnis = "path/to/folder";

5. In order to execute the backup, call up the script in your web browser: https://your-domain.com/backup/ftp-backup.php .


The script will be initiated and stores a backup file named KAS-Login_YYYY-MM.DD_TIME.tar.gz in the "archive" folder (/backup/archive).

Depending on the size of the data to be backed up, the process takes several minutes. We recommend data of a size of more than 5 GB to split into multiple backup files.

This script can also be used for an automated backup process over night which you can realize with the help of a cronjob that can be set up inside your KAS (Technical Administration) panel. A weekly execution of the script is entirely sufficient. The backed up data can be regularly downloaded using an FTP client which is installed on your local computer.

Hint:
In order to prevent the script from being publicly accessible, please set up a directory protection for the "backup" folder. Afterwards, the script can only be reached by entering username and password. A tutorial can be found here.

How helpful was this tutorial?

Search in support and FAQ

Seitensuche