Real-Time Domain Availability Checker in PHP (AJAX + WHOIS + DNS)
A modern PHP-based domain search tool with AJAX, DNS checks, and WHOIS fallback. Get instant domain availability results and smart suggestions in a fast, elegant UI.
Suggested:
Learn how to automate MySQL database backups using a secure and efficient PHP script. Includes cron job setup, gzip compression, and security tips.
Backing up your database is one of the most critical aspects of maintaining a healthy and resilient web application. Automating this task can save you from catastrophic data loss, reduce human error, and simplify your disaster recovery strategy. In this post, we’ll build a robust automated MySQL database backup script in PHP, enhance it with security considerations, and integrate scheduling using cron jobs for true automation.
Table of contents [Show]
Before we dive into the script, ensure your environment has:
<?php
// Configuration
$host = 'localhost';
$username = 'db_user';
$password = 'db_password';
$database = 'db_name';
$backupDir = __DIR__ . '/backups/';
$timestamp = date('Ymd_His');
$backupFile = $backupDir . $database . "_backup_" . $timestamp . ".sql.gz";
// Create backup directory if it doesn't exist
if (!file_exists($backupDir)) {
mkdir($backupDir, 0755, true);
}
// Execute mysqldump
$command = sprintf(
'mysqldump --host=%s --user=%s --password=%s %s | gzip > %s',
escapeshellarg($host),
escapeshellarg($username),
escapeshellarg($password),
escapeshellarg($database),
escapeshellarg($backupFile)
);
exec($command, $output, $result);
if ($result === 0) {
echo "Backup successful: $backupFile\n";
} else {
echo "Backup failed with code $result\n";
}
?>
mysqldump for complete database export.env file parsed by vlucas/phpdotenv.Restrict access to backup files: Configure your server to deny HTTP access to the backup directory:
# .htaccess
Deny from all
openssl or similar tools.1. Open the crontab editor:
crontab -e2. Add the cron job to run the script daily at 2:00 AM:
0 2 * * * /usr/bin/php /path/to/backup_script.php >> /path/to/logs/backup.log 2>&1Add this snippet to delete backup files older than 30 days:
$files = glob($backupDir . '*.gz');
$days = 30;
foreach ($files as $file) {
if (filemtime($file) < time() - ($days * 86400)) {
unlink($file);
}
}
Send alerts on failure using email or a messaging API:
if ($result !== 0) {
mail('admin@example.com', 'Database Backup Failed', 'Check the backup logs for more details.');
}
A: mysqldump is faster and more reliable for large databases and supports full schema + data export.
A: Loop through an array of database names and run the script per database.
A: Yes, if the user has remote access privileges and the MySQL port is open.
gunzip < db_backup_20250512.sql.gz | mysql -u db_user -p db_nameA: Ensure mysqldump is installed and accessible to PHP. Use the full path to the command if needed (find it using which mysqldump).
Automating database backups in PHP is a smart and scalable way to safeguard your application’s data. With a well-structured script and cron integration, you’ll ensure consistent backups without manual oversight. Don’t forget to periodically test your restores—a backup is only useful if it works when needed.
A modern PHP-based domain search tool with AJAX, DNS checks, and WHOIS fallback. Get instant domain availability results and smart suggestions in a fast, elegant UI.
Learn how to build an advanced AJAX contact form with PHP that sends email without page reload. Includes modern UI design, form validation, and secure email handling using PHP's mail() function.
Learn how to remove .php, .html, and .htm extensions from URLs using .htaccess. Create clean, SEO-friendly URLs on Apache servers with our step-by-step guide.