SNAP AUTO BACKUP APPLICATION. (it's an snapshot backup that backup website to cloud)
Certainly! Here’s the detailed explanation formatted in Markdown:
SABA (Snapshot Auto Backup Application) is a web-based application designed to create backups of a website by archiving its files and uploading them to Google Drive for storage. This system includes several components:
- Backup Shell Script (
drive-backup.sh): Archives the website directory and uploads it to Google Drive. - Web Interface (
saba.php): A front-end web page for initiating backups or potential restorations. - PHP Execution Script (
run-backup.php): Executes the shell script from the web interface when a user clicks the "Backup" button.
This shell script is the core of the backup process. Here’s a breakdown of its structure:
-
Variables:
TMPBACKUP: Defines a temporary directory (/root/tmp/) where the backup file will be temporarily stored.SITE: Sets a site identifier (saba.com) used for naming the archive file.BACKUPDIR: Specifies the path to the website directory (/home/site/public_html) that will be archived.ID: The ID of the Google Drive folder where the backup file will be uploaded.
-
Directory Check:
- The script checks if the
TMPBACKUPdirectory exists. If it doesn’t, the script creates it, ensuring that backups won’t fail due to a missing directory.
- The script checks if the
-
Archiving the Directory:
- The command
tar -zcfcreates a.tar.gzarchive ofBACKUPDIR. The file is named with the formatsaba.com-YYYY-MM-DD.tar.gzto include the date, making it easier to identify when each backup was created.
- The command
-
Uploading to Google Drive:
- The
drivecommand uploads the archive to a specific Google Drive folder (using the folder ID provided inID). After uploading, the local copy of the archive is deleted.
- The
Note: The drive command-line tool (from GitHub’s drive repository) must be installed, configured, and authenticated on your server.
The saba.php file provides a user interface to initiate backups or restorations. Here’s a breakdown of its structure:
-
Structure:
- The HTML file includes a header and two buttons.
- Backup Button: Clicking this button calls
run-backup.php, triggering thedrive-backup.shscript to perform the backup. - Restore Button: Currently redirects to the same page (
?). This may be updated to call a restore script or page.
-
Switch Control:
- A toggle switch labeled "DAILY|WEEK" is on the page. It currently has no functionality but appears to be for toggling between daily and weekly backups. JavaScript could be added to handle this functionality in the future.
-
Styling:
- Uses Bootstrap for general styling and a few custom styles for the toggle switch, buttons, and background color.
The run-backup.php file is responsible for executing the drive-backup.sh script when the "Backup" button is clicked on the web interface.
-
Script Execution:
- The PHP
shell_execfunction callsdrive-backup.sh. When the "Backup" button is clicked, this PHP file runs, triggering the shell script to perform the backup.
- The PHP
-
Permissions:
- Ensure that the
run-backup.phpfile and the web server user have execution permissions fordrive-backup.sh.
- Ensure that the
-
Security Note:
- Since this setup allows web-based backup initiation, it’s recommended to secure the page with access controls to prevent unauthorized access.
Here’s a high-level overview of how everything works together:
-
User Initiates a Backup:
- A user opens
saba.phpand clicks the "Backup" button.
- A user opens
-
Backup Script Execution:
- The "Backup" button triggers
run-backup.php, which usesshell_execto rundrive-backup.sh.
- The "Backup" button triggers
-
Backup Process:
drive-backup.shchecks for the temporary directory (TMPBACKUP), creates it if necessary, then archives the website directory.- The archive is named with the site identifier and the current date.
- After archiving, the script uploads the backup to Google Drive, deletes the local copy, and completes.
-
Future Potential:
- You could extend the functionality by adding scheduled backups (using CRON), integrating restore functionality, and securing the web interface.
-
Functionality Enhancements:
- Consider adding a cron job to automate the backup process.
- Implement "Restore" functionality if you plan to add restoration from Google Drive.
- Make the toggle switch functional to switch between daily and weekly backup schedules.
-
Security:
- Secure
saba.phpto prevent unauthorized access. - Ensure that sensitive information, like folder IDs or authentication tokens, is securely stored and handled.
- Secure
This setup creates a foundational backup application with room for future improvements. Let me know if you’d like to dive deeper into any specific part or need assistance with further enhancements!