Current Location: Home> Latest Articles> How to Use PHP and FTP to Automate Website Data Backup

How to Use PHP and FTP to Automate Website Data Backup

M66 2025-06-30

PHP and FTP: How to Automate Website Data Backup

In website development, data backup is an essential task. Regular backups help prevent data loss caused by server failures or cyberattacks, ensuring that the website can quickly recover in any emergency situation. This article will show you how to automate the backup process using PHP and FTP protocols.

Requirements

First, make sure that your website environment has PHP and the necessary FTP extensions installed. Then, we will create a PHP script that uses FTP to connect and perform automatic backups.

FTP Connection Configuration

We need to configure the FTP server connection information. Here's an example PHP script:

<?php
// FTP server configuration
$ftp_server = "ftp.example.com";
$ftp_username = "your_username";
$ftp_password = "your_password";

// Connect to FTP server
$conn_id = ftp_connect($ftp_server);

// Log in to FTP server
$login_result = ftp_login($conn_id, $ftp_username, $ftp_password);

// Check connection and login result
if ((!$conn_id) || (!$login_result)) {
    die("FTP connection or login failed!");
}

Specifying Backup Directories

Next, we need to specify the directory to be backed up and the target directory where backups will be stored. Ensure that the target directory exists on the FTP server. Here is how to set the backup directory and target directory:

<?php
// Directory to back up
$backup_dir = "/path/to/backup/directory";

// Target backup directory
$target_dir = "/path/to/target/directory";

// Connect to FTP server
$conn_id = ftp_connect($ftp_server);

// Log in to FTP server
$login_result = ftp_login($conn_id, $ftp_username, $ftp_password);

// Check connection and login result
if ((!$conn_id) || (!$login_result)) {
    die("FTP connection or login failed!");
}

// Recursively back up the directory to the FTP server
backup_directory($backup_dir, $target_dir);

// Close FTP connection
ftp_close($conn_id);

// Recursive backup function
function backup_directory($backup_dir, $target_dir) {
    global $conn_id;

    // Create target directory
    if (!ftp_mkdir($conn_id, $target_dir)) {
        die("Failed to create target directory!");
    }

    // List all files and subdirectories in the backup directory
    $contents = ftp_nlist($conn_id, $backup_dir);

    // Loop through all files and subdirectories for backup
    foreach ($contents as $file) {
        // Check file type
        $is_dir = ftp_size($conn_id, $file) == -1;

        if ($is_dir) {
            // If it is a subdirectory, back it up recursively
            backup_directory($backup_dir . "/" . $file, $target_dir . "/" . $file);
        } else {
            // If it is a file, download and save it to the target directory
            if (!ftp_get($conn_id, $target_dir . "/" . $file, $backup_dir . "/" . $file, FTP_BINARY)) {
                die("Backup file failed!");
            }
        }
    }
}

Setting Up Scheduled Tasks

Once you have set up the above, you can create a scheduled task to automatically execute the backup script. For example, you can use Cron to configure it to run the backup script at 3 AM every day:

0 3 * * * php /path/to/backup/script.php

Conclusion

By combining PHP and FTP protocols, you can easily automate the backup of your website's data, preventing accidental loss. Depending on your specific needs, you can adjust the backup strategy accordingly. We hope this article helps you better manage the security of your website's data.