The AppGini Blog
A few tips and tricks to make your coding life a tiny bit better.

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:

1
2
3
4
5
6
<?php
	$dbServer = 'localhost';
	$dbUsername = 'northwind';
	$dbPassword = 'Password123$';
	$dbDatabase = 'northwind';
	$dbPort = '3306';

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:

1
mysqldump -u northwind -pPassword123$ -h localhost -P 3306 northwind > /path/to/backup/northwind_backup.sql

Replace:

  • northwind with the value of $dbDatabase from config.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:

  1. Use the credentials from config.php.
  2. Save backups with timestamps.
  3. 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.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#!/bin/bash

# Path to AppGini's config.php -- change this to match your app's path
CONFIG_FILE="/var/www/html/your-app/config.php"

# Backup directory -- change this to your desired location
BACKUP_DIR="/home/johndoe/backups"

# Extract database credentials from config.php
DB_SERVER=$(grep -oP "\$dbServer\s*=\s*'\K[^']+" $CONFIG_FILE)
DB_USERNAME=$(grep -oP "\$dbUsername\s*=\s*'\K[^']+" $CONFIG_FILE)
DB_PASSWORD=$(grep -oP "\$dbPassword\s*=\s*'\K[^']+" $CONFIG_FILE)
DB_DATABASE=$(grep -oP "\$dbDatabase\s*=\s*'\K[^']+" $CONFIG_FILE)
DB_PORT=$(grep -oP "\$dbPort\s*=\s*'\K[^']+" $CONFIG_FILE)

DATE=$(date +"%Y-%m-%d_%H-%M-%S")
BACKUP_FILE="$BACKUP_DIR/${DB_DATABASE}_backup_$DATE.sql"

# Create backup directory if it doesn't exist
mkdir -p $BACKUP_DIR

# Perform the database backup
mysqldump -u $DB_USERNAME -p$DB_PASSWORD -h $DB_SERVER -P $DB_PORT $DB_DATABASE > $BACKUP_FILE

# Optional: Remove backups older than 7 days
find $BACKUP_DIR -type f -name "*.sql" -mtime +7 -exec rm {} \;

# Exit
exit 0

Save and Make the Script Executable

  1. Save the script in a secure location, e.g., /usr/local/bin/appgini_backup.sh.

  2. 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:

1
crontab -e

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

  1. Wait for the cron job to run (or execute the script manually with ./appgini_backup.sh).

  2. Check the backup directory for new .sql files.

  3. 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:

1
chmod 640 /var/www/html/your-app/config.php

2. Encrypt Backups

For sensitive data, encrypt backups using gpg:

1
gpg --encrypt --recipient "[email protected]" $BACKUP_FILE

3. Remote Backups

To store backups offsite, use scp or rsync:

1
scp $BACKUP_FILE user@remote_server:/remote/backup/directory/

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!