Automating Backups for AppGini Applications Using Cron and Mysqldump
If you’re using AppGini to build and deploy PHP applications backed by MySQL or MariaDB, automating database backups is a must to protect your data from accidental loss or corruption. This blog post will guide you through setting up automated backups using cron
and mysqldump
, specifically tailored for AppGini apps.
Why Automate Backups?
Your AppGini app’s database holds critical data, such as user records, custom tables, and application configurations. Regular backups ensure you can quickly restore your app in case of:
- Hardware failures
- Software bugs
- Human errors
- Security breaches
With AppGini, database credentials are stored in the config.php
file, making it even easier to integrate backups into your workflow.
Step 1: Locate Your Database Credentials
AppGini stores database connection details in the config.php
file, typically located in your app’s root directory. Here’s an example:
|
|
Take note of the following values:
- Database server:
$dbServer
- Database username:
$dbUsername
- Database password:
$dbPassword
- Database name:
$dbDatabase
These credentials will be used in the backup script.
Step 2: Test the mysqldump
Command
Before automating the process, test the mysqldump
command to ensure it works correctly with your AppGini database. Open your terminal and run the following command:
|
|
Replace:
northwind
with the value of$dbDatabase
fromconfig.php
Password123$
with the value of$dbPassword
localhost
with the value of$dbServer
3306
with the value of$dbPort
/path/to/backup/northwind_backup.sql
with your desired backup location, for example,/home/johndoe/backups/northwind_backup.sql
If the command runs successfully, you’ll find a .sql
file at the specified location. This file contains the entire database dump.
Step 3: Create a Backup Script
Let’s create a shell script to automate the backup process. This script will:
- Use the credentials from
config.php
. - Save backups with timestamps.
- Optionally, delete old backups to save disk space.
Example Backup Script
Create a new file named appgini_backup.sh
. You can place it anywhere on your server, but /usr/local/bin
is a common location for scripts.
|
|
Save and Make the Script Executable
-
Save the script in a secure location, e.g.,
/usr/local/bin/appgini_backup.sh
. -
Make it executable:
1
chmod +x /usr/local/bin/appgini_backup.sh
Step 4: Automate with Cron
To schedule the backup script, use cron
. Open the crontab editor:
|
|
Add a line to schedule the script, for example:
-
Daily at 2 AM:
1
0 2 * * * /usr/local/bin/appgini_backup.sh
-
Every 6 hours:
1
0 */6 * * * /usr/local/bin/appgini_backup.sh
Save and exit the editor.
Step 5: Verify the Backup
-
Wait for the cron job to run (or execute the script manually with
./appgini_backup.sh
). -
Check the backup directory for new
.sql
files. -
Test restoring a backup to ensure it works:
1
mysql -u northwind -pPassword123$ -h localhost -P 3306 northwind < /home/johndoe/backups/northwind_backup.sql
Replace the paths and credentials in the above command with your own.
Optional: Secure and Enhance Your Backups
1. Secure Credentials
Hardcoding credentials in the script can be a security risk. Since AppGini already stores them in config.php
, this script extracts them dynamically. Ensure your config.php
file has restricted permissions, but can still be read by the script and the web server:
|
|
2. Encrypt Backups
For sensitive data, encrypt backups using gpg
:
|
|
3. Remote Backups
To store backups offsite, use scp
or rsync
:
|
|
Conclusion
Automating backups for your AppGini application ensures your data is safe and restorable in case of emergencies. By leveraging mysqldump
and cron
, you can create a reliable, hands-off backup system tailored to your app. Just remember to regularly monitor your backups and test your restore process to avoid surprises when you need it most!