From 9ca968c0394d17e0308129a01cbc142210703784 Mon Sep 17 00:00:00 2001 From: Chad Davis Date: Fri, 14 Jun 2024 17:31:29 -0500 Subject: [PATCH 1/7] Initial Commit Adding first draft of Simple Backup Utility and its automated deploy script --- .github/workflows/deploy.yml | 44 +++++++ index.php | 216 +++++++++++++++++++++++++++++++++++ 2 files changed, 260 insertions(+) create mode 100644 .github/workflows/deploy.yml create mode 100644 index.php diff --git a/.github/workflows/deploy.yml b/.github/workflows/deploy.yml new file mode 100644 index 0000000..1b3557b --- /dev/null +++ b/.github/workflows/deploy.yml @@ -0,0 +1,44 @@ +on: + pull_request: + branches: + - main + - dev + types: + - closed + push: + branches: + - dev + workflow_dispatch: +name: 🚀 Deploy website based on branch +jobs: + web-deploy: + name: 🎉 Deploy + runs-on: ubuntu-latest + if: | + github.event_name == 'workflow_dispatch' || + (github.event_name == 'pull_request' && github.event.pull_request.merged == true) || + (github.event_name == 'push' && contains(github.ref, 'refs/heads/dev')) + steps: + - name: 🚚 Get latest code + uses: actions/checkout@v4 + + - name: 📂 Sync files to PROD + if: contains(github.ref, 'refs/heads/main') || (github.event_name == 'pull_request' && contains(github.base_ref, 'main')) + uses: SamKirkland/FTP-Deploy-Action@v4.3.5 + with: + server: ${{ secrets.PROD_FTP_SERVER }} + username: ${{ secrets.PROD_FTP_USERNAME }} + password: ${{ secrets.PROD_FTP_PASSWORD }} + server-dir: / + + - name: 📂 Sync files to DEV + if: contains(github.ref, 'refs/heads/dev') + uses: SamKirkland/FTP-Deploy-Action@v4.3.5 + with: + server: ${{ secrets.DEV_FTP_SERVER }} + username: ${{ secrets.DEV_FTP_USERNAME }} + password: ${{ secrets.DEV_FTP_PASSWORD }} + server-dir: / + exclude: | + LICENSE + README.md diff --git a/index.php b/index.php new file mode 100644 index 0000000..ffef3e9 --- /dev/null +++ b/index.php @@ -0,0 +1,216 @@ + $folder_name, + 'created_date' => $created_date, + 'created_timestamp' => $created_timestamp // Add timestamp for sorting + ]; + } + // Sort by created_timestamp in descending order + usort($backup_folders, function($a, $b) { + return $b['created_timestamp'] - $a['created_timestamp']; + }); + return $backup_folders; +} + +$message = ''; +$messageColor = "#dc3545"; +$current_dir = getcwd(); // Get current directory +$folders = get_sibling_folders($current_dir); // Get sibling folder names excluding current directory + +if ($_SERVER['REQUEST_METHOD'] === 'POST') { + $input_name = preg_replace('/\s+/', '-', trim($_POST['folder_name'])); // Replace spaces with dashes + + if (isset($_POST['backup']) && in_array($_POST['backup'], $folders)) { + $source = "../{$_POST['backup']}"; + } else { + $message = 'Invalid backup folder selected.'; + } + + if (empty($message)) { + $folder_name = $_POST['backup'] . '_' . $input_name; + $destination = "../" . basename($current_dir) . "/" . $folder_name; + + if (is_dir($destination)) { + $message = "The folder '$folder_name' already exists. Backup cannot be completed."; + } else { + $file_count = backup_folder($source, $destination); + if ($file_count > 0) { + $message = "The folder '$folder_name' has been created with $file_count files."; + $messageColor = "#28a745"; + } else { + $message = "Failed to create the folder '$folder_name'."; + } + } + } + +} + +$backup_folders = get_backup_folders($current_dir); +?> + + + + + + Backup Utility + + + +
+

Backup Utility

+
+ +
+ + + +
+
+ +
+ +
+ +
+

Existing Backups

+ + + + + + $folder): ?> + + + + + +
BackupCreated Date (CST)
+
+ + From b9b7763ddaebc3365d3dcf277cd2623da2647402 Mon Sep 17 00:00:00 2001 From: Chad Davis Date: Sun, 16 Jun 2024 10:18:34 -0500 Subject: [PATCH 2/7] Added delete backup feature Added the ability to delete a backup using a Font Awesome trash icon --- index.php | 87 ++++++++++++++++++++++++++++++++++++++++++------------- 1 file changed, 67 insertions(+), 20 deletions(-) diff --git a/index.php b/index.php index ffef3e9..9e3a2bf 100644 --- a/index.php +++ b/index.php @@ -53,37 +53,58 @@ function get_backup_folders($dir) { return $backup_folders; } +function delete_backup_folder($folder) { + if (!is_dir($folder)) { + return false; + } + $files = array_diff(scandir($folder), array('.', '..')); + foreach ($files as $file) { + $path = "$folder/$file"; + is_dir($path) ? delete_backup_folder($path) : unlink($path); + } + return rmdir($folder); +} + $message = ''; $messageColor = "#dc3545"; $current_dir = getcwd(); // Get current directory $folders = get_sibling_folders($current_dir); // Get sibling folder names excluding current directory if ($_SERVER['REQUEST_METHOD'] === 'POST') { - $input_name = preg_replace('/\s+/', '-', trim($_POST['folder_name'])); // Replace spaces with dashes - - if (isset($_POST['backup']) && in_array($_POST['backup'], $folders)) { - $source = "../{$_POST['backup']}"; + if (isset($_POST['delete'])) { + $folder_to_delete = $current_dir . '/' . $_POST['delete']; + if (delete_backup_folder($folder_to_delete)) { + $message = "The folder '{$_POST['delete']}' has been deleted."; + $messageColor = "#28a745"; + } else { + $message = "Failed to delete the folder '{$_POST['delete']}'."; + } } else { - $message = 'Invalid backup folder selected.'; - } + $input_name = preg_replace('/\s+/', '-', trim($_POST['folder_name'])); // Replace spaces with dashes + + if (isset($_POST['backup']) && in_array($_POST['backup'], $folders)) { + $source = "../{$_POST['backup']}"; + } else { + $message = 'Invalid backup folder selected.'; + } - if (empty($message)) { - $folder_name = $_POST['backup'] . '_' . $input_name; - $destination = "../" . basename($current_dir) . "/" . $folder_name; + if (empty($message)) { + $folder_name = $_POST['backup'] . '_' . $input_name; + $destination = "../" . basename($current_dir) . "/" . $folder_name; - if (is_dir($destination)) { - $message = "The folder '$folder_name' already exists. Backup cannot be completed."; - } else { - $file_count = backup_folder($source, $destination); - if ($file_count > 0) { - $message = "The folder '$folder_name' has been created with $file_count files."; - $messageColor = "#28a745"; + if (is_dir($destination)) { + $message = "The folder '$folder_name' already exists. Backup cannot be completed."; } else { - $message = "Failed to create the folder '$folder_name'."; + $file_count = backup_folder($source, $destination); + if ($file_count > 0) { + $message = "The folder '$folder_name' has been created with $file_count files."; + $messageColor = "#28a745"; + } else { + $message = "Failed to create the folder '$folder_name'."; + } } } } - } $backup_folders = get_backup_folders($current_dir); @@ -94,6 +115,7 @@ function get_backup_folders($dir) { Backup Utility + @@ -203,11 +242,19 @@ function get_backup_folders($dir) { Backup Created Date (CST) + Delete $folder): ?> + +
+ +
+ From c0713a37777e5862857abc686f454ceead4f5a31 Mon Sep 17 00:00:00 2001 From: Chad Davis Date: Sun, 16 Jun 2024 10:30:21 -0500 Subject: [PATCH 3/7] Fix table text color Accidentally removed the table text coloring CSS for TD cells - this fixes that mistake --- index.php | 1 + 1 file changed, 1 insertion(+) diff --git a/index.php b/index.php index 9e3a2bf..81d9144 100644 --- a/index.php +++ b/index.php @@ -184,6 +184,7 @@ function delete_backup_folder($folder) { table th, table td { border: 1px solid #fff; padding: 10px; + color: #000; } table th { background-color: #007bff; From ee04481d99c027cd1802ea230b8df577b88d9c8a Mon Sep 17 00:00:00 2001 From: Chad Davis Date: Fri, 21 Jun 2024 08:33:09 -0500 Subject: [PATCH 4/7] Combine redundant CSS --- index.php | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/index.php b/index.php index 81d9144..bd50205 100644 --- a/index.php +++ b/index.php @@ -196,10 +196,7 @@ function delete_backup_folder($folder) { table tr:nth-child(odd) { background-color: #e9ecef; } - table th:nth-child(1), table td:nth-child(1) { - width: 45%; - } - table th:nth-child(2), table td:nth-child(2) { + table th:nth-child(1), table th:nth-child(2), table td:nth-child(1), table td:nth-child(2) { width: 45%; } table th:nth-child(3), table td:nth-child(3) { From dcc5df26c01d08b72a4d695657a07eb4e6301282 Mon Sep 17 00:00:00 2001 From: Chad Davis Date: Fri, 21 Jun 2024 08:41:40 -0500 Subject: [PATCH 5/7] Add random background color #3 Added random background color for main container object. Also updated the formatting of the previously combined CSS. --- index.php | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/index.php b/index.php index bd50205..de3e984 100644 --- a/index.php +++ b/index.php @@ -108,6 +108,20 @@ function delete_backup_folder($folder) { } $backup_folders = get_backup_folders($current_dir); + +// Array of colors +$colors = [ + "blue", "blueviolet", "brown", "cadetblue", "chocolate", "crimson", "darkblue", + "darkcyan", "darkgray", "darkgreen", "darkmagenta", "darkolivegreen", "darkorchid", + "darkred", "darkslateblue", "darkslategray", "darkviolet", "deeppink", "dimgray", + "firebrick", "forestgreen", "gray", "green", "indianred", "magenta", "maroon", + "mediumblue", "mediumvioletred", "midnightblue", "navy", "orangered", "palevioletred", + "peru", "purple", "rebeccapurple", "red", "seagreen", "sienna", "slategray", "steelblue", + "teal", "tomato" +]; + +// Randomly select a color +$random_color = $colors[array_rand($colors)]; ?> @@ -129,7 +143,7 @@ function delete_backup_folder($folder) { padding: 20px; border: 2px solid #fff; border-radius: 10px; - background-color: rgba(255, 255, 255, 0.1); + background-color: ; } h1, h2 { color: #fff; @@ -196,7 +210,8 @@ function delete_backup_folder($folder) { table tr:nth-child(odd) { background-color: #e9ecef; } - table th:nth-child(1), table th:nth-child(2), table td:nth-child(1), table td:nth-child(2) { + table th:nth-child(1), table th:nth-child(2), + table td:nth-child(1), table td:nth-child(2) { width: 45%; } table th:nth-child(3), table td:nth-child(3) { From 74c9a51c1c1e403be546736b14ff536fa9c0a9e3 Mon Sep 17 00:00:00 2001 From: Chad Davis Date: Fri, 21 Jun 2024 09:45:28 -0500 Subject: [PATCH 6/7] Add button shadow #4 Close #4 --- index.php | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/index.php b/index.php index de3e984..614fa59 100644 --- a/index.php +++ b/index.php @@ -170,6 +170,10 @@ function delete_backup_folder($folder) { gap: 10px; margin-bottom: 20px; } + .button-container button { + box-shadow: 0 8px 8px 1px rgba(0, 0, 0, .2); + font-weight: bold; + } button { padding: 10px 20px; font-size: 16px; From 70f09be2bdbe4bb0e68b0f0ca0646f72914638bb Mon Sep 17 00:00:00 2001 From: Chad Davis Date: Fri, 21 Jun 2024 13:18:19 -0500 Subject: [PATCH 7/7] Update README.md --- README.md | 70 +++++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 68 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 1398047..292370e 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,68 @@ -# Simple-Backup-Utility -Simple PHP backup utility to copy an entire directory into a backup folder. +# Simple Backup Utility +![GitHub](https://img.shields.io/github/license/dynamiccookies/Simple-Backup-Utility?style=for-the-badge "License") +![GitHub file size in bytes](https://img.shields.io/github/size/dynamiccookies/Simple-Backup-Utility/index.php?style=for-the-badge) +![GitHub Release Date](https://img.shields.io/github/release-date/dynamiccookies/Simple-Backup-Utility?style=for-the-badge "Release Date") +![GitHub release (latest SemVer including pre-releases)](https://img.shields.io/github/v/release/dynamiccookies/Simple-Backup-Utility?display_name=tag&include_prereleases&sort=semver&style=for-the-badge "Release Version") +[Deployed with FTP Deploy Action](https://github.com/SamKirkland/FTP-Deploy-Action) + +Simple Backup Utility is a PHP script that allows you to create and manage backups of sibling folders within the same directory. + +## Features + +- **Backup Creation**: Easily create backups of specific folders. +- **Backup Deletion**: Remove existing backup folders. +- **User-Friendly Interface**: Intuitive web interface for straightforward operation. + +## Requirements + +- PHP (version 5.6 or higher recommended) +- Web server (e.g., Apache, Nginx) + +## Installation + +1. Clone the repository or download the ZIP file. +2. Place the script (`index.php`) in the directory where you want to manage backups. +3. Ensure the directory has appropriate permissions for creating and deleting files and folders. + +## Usage + +### Creating a Backup + +1. Access the script via a web browser after installation. +2. Enter a name for your backup in the "Backup Name" field. +3. Click the "Backup" button for the environment folder you want to backup. + +### Deleting a Backup + +1. Click the trash icon next to the Existing Backups entry. + + +## Folder Structure + +The Simple Backup Utility expects the following directory structure: +- Project Directory + - prod + - staging + - test + - dev + - backups + - **index.php** (this script) + - prod_release-v1 + - staging_benchmark-tests + - test_data-fixtures + - dev_feature-branch-backup + +The script displays all of its parent directory's sibling folders. In this example, prod, staging, test, and dev. + +The backup process names the selected directory using the format `_` based on the selected directory to backup. + +**Note:** The names of all files and folders are arbitrary and can be customized as needed. + + +## License + +This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details. + +## Support + +For any issues, questions, or feature requests, please [open an issue](https://github.com/dynamiccookies/Simple-Backup-Utility/issues).