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

Automating Backups for AppGini Applications Using Task Scheduler and Mysqldump (Windows)

If you’re using AppGini to build and deploy PHP applications backed by MySQL or MariaDB, automating database backups is crucial to protect your data from accidental loss or corruption. This tutorial will walk you through automating backups on Windows using Task Scheduler and mysqldump, with steps tailored specifically for AppGini apps.


Why Automate Backups?

Your AppGini app’s database holds essential data — such as user records, custom tables, and application settings. Automating backups ensures you can quickly recover your app’s functionality in case of:

  • Hardware failures
  • Software bugs
  • Human errors
  • Security breaches

On Windows, mysqldump combined with Task Scheduler provides a reliable way to automate these backups.


Step 1: Locate Your Database Credentials

AppGini stores database connection details in the config.php file, typically located in your app’s root directory. Open the file in a text editor and locate the following variables:

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

Take note of:

  • Database server: $dbServer
  • Database username: $dbUsername
  • Database password: $dbPassword
  • Database name: $dbDatabase
  • Database port: $dbPort

These credentials will be used in the backup script.


Step 2: Locate and Test the mysqldump Command

Before automating backups, we need to identify where mysqldump.exe is located on your machine. Follow these steps:

Locate mysqldump

  1. Open PowerShell.

  2. Run the following command to search for mysqldump.exe:

    1
    
    Get-ChildItem -Path "C:\Program Files", "C:\Program Files (x86)" -Recurse -Filter "mysqldump.exe" -ErrorAction SilentlyContinue
    

    This will search common installation directories for MySQL and output the full path to mysqldump.exe.

  3. Note the full path of the executable (e.g., C:\Program Files\MySQL\MySQL Server 8.0\bin\mysqldump.exe).

    If no results are found, ensure you have MySQL or MariaDB installed, and mysqldump is included in your installation.

Test the mysqldump Command

Once you’ve located mysqldump.exe, test it to ensure it works correctly with your database. Run the following PowerShell command:

1
& "C:\Path\To\mysqldump.exe" -u northwind -pPassword123$ -h localhost -P 3306 northwind > C:\backups\northwind_backup.sql

Replace:

  • C:\Path\To\mysqldump.exe with the path to mysqldump you found earlier.
  • northwind with the value of $dbDatabase.
  • Password123$ with the value of $dbPassword.
  • localhost with the value of $dbServer.
  • 3306 with the value of $dbPort.
  • C:\backups\northwind_backup.sql with your desired backup file path.

If the command runs successfully, a .sql file will appear at the specified location. This file contains the database dump.


Step 3: Create a Backup Script

Now, let’s create a PowerShell script that automates 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 PowerShell Backup Script

  1. Open Notepad (or your favorite text editor) and paste the following script:
 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
30
31
32
# Path to AppGini's config.php -- Change this to match your app's path
$configFile = "C:\inetpub\wwwroot\your-app\config.php"

# Backup directory -- Change this to your desired location
$backupDir = "C:\backups"

# Path to mysqldump -- Change this to the location you found earlier
$mysqldumpPath = "C:\Program Files\MySQL\MySQL Server 8.0\bin\mysqldump.exe"

# Extract database credentials from config.php
$dbServer = (Get-Content $configFile | Select-String -Pattern "\$dbServer\s*=\s*'([^']+)'" | ForEach-Object { ($_ -split "'")[1] })
$dbUsername = (Get-Content $configFile | Select-String -Pattern "\$dbUsername\s*=\s*'([^']+)'" | ForEach-Object { ($_ -split "'")[1] })
$dbPassword = (Get-Content $configFile | Select-String -Pattern "\$dbPassword\s*=\s*'([^']+)'" | ForEach-Object { ($_ -split "'")[1] })
$dbDatabase = (Get-Content $configFile | Select-String -Pattern "\$dbDatabase\s*=\s*'([^']+)'" | ForEach-Object { ($_ -split "'")[1] })
$dbPort = (Get-Content $configFile | Select-String -Pattern "\$dbPort\s*=\s*'([^']+)'" | ForEach-Object { ($_ -split "'")[1] })

# Generate a timestamped backup file name
$date = Get-Date -Format "yyyy-MM-dd_HH-mm-ss"
$backupFile = "$backupDir\$($dbDatabase)_backup_$date.sql"

# Ensure the backup directory exists
if (!(Test-Path -Path $backupDir)) {
    New-Item -ItemType Directory -Path $backupDir | Out-Null
}

# Perform the database backup
& $mysqldumpPath -u $dbUsername -p$dbPassword -h $dbServer -P $dbPort $dbDatabase > $backupFile

# Optional: Remove backups older than 7 days
Get-ChildItem -Path $backupDir -Filter "*.sql" | Where-Object { $_.LastWriteTime -lt (Get-Date).AddDays(-7) } | Remove-Item

Write-Host "Backup completed: $backupFile"
  1. Save the file as appgini_backup.ps1 in a secure location, such as C:\scripts.

Step 4: Automate with Task Scheduler

To schedule the backup script to run automatically, use Task Scheduler:

  1. Open Task Scheduler (Win + R, type taskschd.msc, and press Enter).
  2. Click Create Task from the right-hand menu.
  3. In the General tab:
    • Name the task (e.g., AppGini Database Backup).
    • Select Run whether user is logged on or not.
    • Check Run with highest privileges.
  4. In the Triggers tab:
    • Click New to add a trigger.
    • Set the backup schedule (e.g., Daily at 2:00 AM).
  5. In the Actions tab:
    • Click New.
    • Set Action to Start a Program.
    • For Program/script, enter:
      1
      
      powershell.exe
      
    • For Add arguments, enter:
      1
      
      -File "C:\scripts\appgini_backup.ps1"
      
  6. In the Conditions and Settings tabs, adjust options as needed (e.g., stop the task if it runs longer than 1 hour).
  7. Click OK and enter your username/password when prompted.

Step 5: Verify the Backup

  1. Wait for the task to run at its scheduled time (or execute it manually by right-clicking the task and selecting Run).

  2. Check the backup directory (e.g., C:\backups) for new .sql files.

  3. Test restoring a backup to confirm it works:

    1
    
    & "C:\Path\To\mysql.exe" -u northwind -pPassword123$ -h localhost -P 3306 northwind < C:\backups\northwind_backup.sql
    

By ensuring mysqldump is correctly located and tested, as shown above, you can create a reliable backup process for your AppGini application!