Views: 3 visits
Flat-style illustration of servers, a cloud icon, and an FTP folder connected by circular arrows and a gear, with text ‘Automate FTP Backup Management’

Automate FTP Backup Management using our robust PHP script to effortlessly archive old backups, keep only the most recent files, and delete redundant copies. In this guide, you’ll learn how to set up daily cron jobs, configure your FTP credentials, customize retention rules (daily, weekly, monthly), monitor disk usage, and ensure your backup host stays lean—without any manual intervention.

Why You Should Automate FTP Backup Management

Manual backup cleanup is error-prone and time-consuming. Over weeks and months, hundreds of backup files accumulate:

  • Wasted disk space: Old or duplicate backups clog your server volume.
  • Maintenance burden: Manually logging in to delete files takes hours each week.
  • Risk of data loss: You might accidentally delete the wrong file—or miss a full disk error.

By choosing to automate FTP backup management, you:

  • Optimize server space with precise retention rules.
  • Save valuable time eliminating manual cleanup tasks.
  • Gain reliability through daily checks and email alerts.

Key Features of Our PHP Backup Script

  • Daily Retention: Keep the last 7 daily backups, regardless of date gaps.
  • Weekly Retention: Archive the most recent 3 Friday backups automatically.
  • Monthly Retention: Archive the first-of-month backups for the last 3 months.
  • Automated Cleanup: Delete any files not included in your retention policy.
  • Email Alerts: Receive notifications if today’s backup is missing or incomplete.
  • Easy Configuration: Simply set FTP credentials, directory paths, and retention counts.
  • Cron-Ready: Designed to execute daily via Cron with zero human intervention.

How to Automate FTP Backup Management with PHP

1. Download the Script

Download the ZIP package containing ftp_backup_cleanup.php from your product page on p30script.com:

Download FTP Backup Cleanup Script

2. Configure FTP Settings

Open ftp_backup_cleanup.php in your editor and update the connection details:

// FTP credentials
$ftp_server  = "ftp.yourdomain.com";
$ftp_user    = "ftp_username";
$ftp_pass    = "ftp_password";

3. Set Backup & Archive Paths

Adjust these to match your server layout and permissions:

// Folder for incoming backups
$backup_dir  = "/home/username/backups/";
// Folder for archived files
$archive_dir = "/home/username/archive/";

4. Install on Your Server

Upload ftp_backup_cleanup.php to a secure folder on your cPanel account, for example:

/home/username/scripts/ftp_backup_cleanup.php

5. Schedule a Daily Cron Job

In cPanel → Cron Jobs, add this command to run daily:

/usr/local/bin/php /home/username/scripts/ftp_backup_cleanup.php >/dev/null 2>&1

This ensures the script runs at midnight and suppresses emails unless an alert is triggered.

Behind the Scenes: What the Script Does

  1. Connects to FTP using ftp_connect & ftp_login.
  2. Fetches the file list via ftp_nlist($backup_dir).
  3. Parses dates out of filenames like backup-10-06-2025_13-00-00.tar.gz.
  4. Verifies today’s backup exists—sends email if missing.
  5. Sorts all files by date descending.
  6. Keeps the 7 most recent daily backups.
  7. Archives up to 3 recent Friday files & 3 first-of-month files into $archive_dir.
  8. Deletes any remaining files not covered by those rules.

For example:

usort($file_dates, function($a,$b){ return $b['date'] - $a['date']; });
$daily_to_keep  = array_slice($file_dates, 0, 7);
// Archive & delete logic…

Example Directory Layout on Jan 5, 2026

After six months of daily runs, your backup host will contain:

  • /home/username/backups/: 7 recent daily backups (Jan 5 → Dec 30, 2025).
  • /home/username/archive/:
    • Friday archives: Dec 26, Dec 19, Dec 12, 2025
    • Month-start archives: Jan 1, Dec 1, Nov 1, 2025

Frequently Asked Questions

Q: What if a Friday or 1st-of-month backup is missing?
A: The script skips missing dates and archives the next valid backups until it reaches your set count.

Q: Can I change retention counts?
A: Yes—just edit these variables:

$daily_keep   = 7;
$weekly_keep  = 3;
$monthly_keep = 3;

Q: How are email alerts configured?
A: If today’s backup is absent, the script calls PHP’s mail() function to your configured address.

Conclusion

With this guide, you can automate FTP backup management in minutes—no more manual purges or forgotten snapshots. Install the script, set your paths, schedule the cron job, and let it run. Your backup host will stay optimized, with only the files you need.

Download the FTP Backup Cleanup Script and streamline your backup workflow now:
Download Now

Read more