Created
August 12, 2025 14:54
-
-
Save maaduukaar/2f4564e01ccd81c4d909fce966af89f1 to your computer and use it in GitHub Desktop.
PHP script to create a ZIP archive of the entire website, excluding the script itself and the backup folder. ⚠️ Delete after use!
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* ⚠️ IMPORTANT! SECURITY WARNING! ⚠️ | |
* DELETE THIS FILE FROM THE SERVER AFTER USE! | |
* Leaving this script on the server poses a security risk, | |
* as attackers can use it to create archives | |
* containing sensitive data from your website. | |
*/ | |
// Check if ZipArchive class is available | |
if (!class_exists('ZipArchive')) { | |
die('Error: ZipArchive class is not available. Make sure the zip extension is enabled in PHP.'); | |
} | |
// Get the current date for the file name | |
$currentDate = date('Y-m-d'); | |
$backupFileName = "backup_{$currentDate}.zip"; | |
// Create backup directory if it does not exist | |
$backupDir = 'backup'; | |
if (!is_dir($backupDir)) { | |
if (!mkdir($backupDir, 0755, true)) { | |
die('Error: Unable to create the backup directory'); | |
} | |
} | |
// Full path to the archive | |
$backupPath = $backupDir . DIRECTORY_SEPARATOR . $backupFileName; | |
// Get the current script name to exclude it | |
$currentScript = basename(__FILE__); | |
// Create a new ZIP archive | |
$zip = new ZipArchive(); | |
$result = $zip->open($backupPath, ZipArchive::CREATE | ZipArchive::OVERWRITE); | |
if ($result !== TRUE) { | |
die("Error creating archive: {$result}"); | |
} | |
/** | |
* Recursive function to add files to the archive | |
*/ | |
function addFilesToZip($zip, $dir, $baseDir, $excludeFiles = []) { | |
$files = scandir($dir); | |
foreach ($files as $file) { | |
if ($file === '.' || $file === '..') { | |
continue; | |
} | |
$filePath = $dir . DIRECTORY_SEPARATOR . $file; | |
$relativePath = str_replace($baseDir . DIRECTORY_SEPARATOR, '', $filePath); | |
// Exclude specified files and the backup folder | |
if (in_array($file, $excludeFiles) || $file === 'backup') { | |
continue; | |
} | |
if (is_dir($filePath)) { | |
// Add folder to the archive | |
$zip->addEmptyDir($relativePath); | |
// Recursively add the folder contents | |
addFilesToZip($zip, $filePath, $baseDir, $excludeFiles); | |
} else { | |
// Add file to the archive | |
$zip->addFile($filePath, $relativePath); | |
} | |
} | |
} | |
// Get the current directory | |
$currentDir = dirname(__FILE__); | |
// Files to exclude from the archive | |
$excludeFiles = [$currentScript, $backupFileName]; | |
try { | |
// Add files to the archive | |
addFilesToZip($zip, $currentDir, $currentDir, $excludeFiles); | |
// Close the archive | |
$closeResult = $zip->close(); | |
if ($closeResult) { | |
$archiveSize = filesize($backupPath); | |
$archiveSizeMB = round($archiveSize / 1024 / 1024, 2); | |
echo "<h2>✅ Archive created successfully!</h2>"; | |
echo "<p><strong>File name:</strong> {$backupFileName}</p>"; | |
echo "<p><strong>Location:</strong> {$backupPath}</p>"; | |
echo "<p><strong>Archive size:</strong> {$archiveSizeMB} MB</p>"; | |
echo "<p><strong>Number of files:</strong> {$zip->numFiles}</p>"; | |
echo "<hr>"; | |
echo "<p style='color: red; font-weight: bold;'>⚠️ DO NOT FORGET TO DELETE THIS SCRIPT FROM THE SERVER!</p>"; | |
} else { | |
echo "<h2>❌ Error closing the archive</h2>"; | |
} | |
} catch (Exception $e) { | |
echo "<h2>❌ An error occurred:</h2>"; | |
echo "<p>" . htmlspecialchars($e->getMessage()) . "</p>"; | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
PHP Backup Script
This PHP script creates a ZIP archive of all files and folders in the website directory, excluding:
backup
folder (where the archive will be stored)The archive is automatically saved in the
backup
folder with a name in the format:Usage
https://example.com/backup.php
).Requirements
zip
extension enabled (ZipArchive
class available).backup
directory (it will be created automatically if missing).Warning