{"id":5992,"date":"2025-12-27T10:30:30","date_gmt":"2025-12-27T10:30:30","guid":{"rendered":"https:\/\/www.codexworld.com\/?p=5992"},"modified":"2026-01-16T14:48:29","modified_gmt":"2026-01-16T14:48:29","slug":"auto-backup-mysql-database-using-php-cron","status":"publish","type":"post","link":"https:\/\/www.codexworld.com\/auto-backup-mysql-database-using-php-cron\/","title":{"rendered":"Auto Backup MySQL Database using PHP Cron (Linux &#038; Windows)"},"content":{"rendered":"<p>Automatically backing up your MySQL database is critical for production websites and applications. Manual backups are error-prone and often forgotten, while automated backups ensure your data is safe even if something goes wrong.<\/p>\n<p>In this tutorial, you\u2019ll learn <b>how to auto backup a MySQL database using PHP and Cron (Linux\/macOS) or Task Scheduler (Windows)<\/b>. We\u2019ll use a <b>CLI-based PHP script<\/b> that creates <b>compressed MySQL backups<\/b>, stores them in a secure folder, and automatically deletes old backups based on a retention policy.<\/p>\n<h2>\ud83d\udccc What You\u2019ll Learn<\/h2>\n<ul>\n<li>How to create a MySQL backup using PHP and mysqldump<\/li>\n<li>How to compress database backups automatically<\/li>\n<li>How to configure backup retention (auto delete old backups)<\/li>\n<li>How to schedule backups using Cron (Linux)<\/li>\n<li>How to schedule backups using Task Scheduler (Windows)<\/li>\n<\/ul>\n<h2>\ud83d\udee0\ufe0f Prerequisites<\/h2>\n<ul>\n<li>A server with PHP installed (CLI version)<\/li>\n<li>Access to MySQL database credentials<\/li>\n<li>mysqldump utility installed (usually comes with MySQL)<\/li>\n<li>CLI access (Terminal \/ Command Prompt)<\/li>\n<li>Cron access (Linux\/macOS) or Task Scheduler (Windows)<\/li>\n<\/ul>\n<h2>\ud83d\udcc1 Project Structure<\/h2>\n<p>Create a simple folder structure like below:<\/p>\n<pre class=\"file-struc\">mysql-auto-backup<span style=\"color:#794938\">\/<\/span>\r\n\u2502\u2500\u2500 backup.php\r\n\u2502\u2500\u2500 config.php\r\n\u2514\u2500\u2500 backups\/\r\n<\/pre>\n<p>&#8211; <b>backup.php<\/b>: The main PHP script that performs the backup.<br \/>\n&#8211; <b>config.php<\/b>: Configuration file for database credentials and backup settings.<br \/>\n&#8211; <b>backups\/<\/b>: Directory where backups will be stored.<br \/>\nMake sure the <b>backups\/<\/b> directory is writable by the user running the PHP script.<\/p>\n<h2>\u2699\ufe0f Step 1: Database Configuration (config.php)<\/h2>\n<p>Create a <code>config.php<\/code> file to store database credentials and backup settings in a clean and reusable way.<br \/>\n<u>Key Configuration Options:<\/u><\/p>\n<ul>\n<li><b>db_host<\/b>: Database host (usually localhost or 127.0.0.1)<\/li>\n<li><b>db_user<\/b>: Database username<\/li>\n<li><b>db_pass<\/b>: Database password<\/li>\n<li><b>db_name<\/b>: Name of the database to back up<\/li>\n<li><b>backup_dir<\/b>: Directory where backups will be stored<\/li>\n<li><b>retention_days<\/b>: Number of days to keep backups before deletion<\/li>\n<li><b>mysqldump_path<\/b>: Path to the mysqldump binary<\/li>\n<\/ul>\n<pre><span style=\"color: #0000BB\">&lt;?php <br \/><\/span><span style=\"color: #007700\">return&nbsp;[ <br \/>&nbsp;&nbsp;&nbsp;&nbsp;<\/span><span style=\"color: #DD0000\">'db_host'&nbsp;<\/span><span style=\"color: #007700\">=&gt;&nbsp;<\/span><span style=\"color: #DD0000\">'127.0.0.1'<\/span><span style=\"color: #007700\">, <br \/>&nbsp;&nbsp;&nbsp;&nbsp;<\/span><span style=\"color: #DD0000\">'db_user'&nbsp;<\/span><span style=\"color: #007700\">=&gt;&nbsp;<\/span><span style=\"color: #DD0000\">'root'<\/span><span style=\"color: #007700\">, <br \/>&nbsp;&nbsp;&nbsp;&nbsp;<\/span><span style=\"color: #DD0000\">'db_pass'&nbsp;<\/span><span style=\"color: #007700\">=&gt;&nbsp;<\/span><span style=\"color: #DD0000\">''<\/span><span style=\"color: #007700\">, <br \/>&nbsp;&nbsp;&nbsp;&nbsp;<\/span><span style=\"color: #DD0000\">'db_name'&nbsp;<\/span><span style=\"color: #007700\">=&gt;&nbsp;<\/span><span style=\"color: #DD0000\">'your_database'<\/span><span style=\"color: #007700\">, <br \/> <br \/>&nbsp;&nbsp;&nbsp;&nbsp;<\/span><span style=\"color: #FF8000\">\/\/&nbsp;Directory&nbsp;where&nbsp;backups&nbsp;will&nbsp;be&nbsp;stored <br \/>&nbsp;&nbsp;&nbsp;&nbsp;<\/span><span style=\"color: #DD0000\">'backup_dir'&nbsp;<\/span><span style=\"color: #007700\">=&gt;&nbsp;<\/span><span style=\"color: #0000BB\">__DIR__&nbsp;<\/span><span style=\"color: #007700\">.&nbsp;<\/span><span style=\"color: #DD0000\">'\/backups'<\/span><span style=\"color: #007700\">, <br \/> <br \/>&nbsp;&nbsp;&nbsp;&nbsp;<\/span><span style=\"color: #FF8000\">\/\/&nbsp;Automatically&nbsp;delete&nbsp;backups&nbsp;older&nbsp;than&nbsp;X&nbsp;days <br \/>&nbsp;&nbsp;&nbsp;&nbsp;<\/span><span style=\"color: #DD0000\">'retention_days'&nbsp;<\/span><span style=\"color: #007700\">=&gt;&nbsp;<\/span><span style=\"color: #0000BB\">7<\/span><span style=\"color: #007700\">, <br \/>&nbsp;&nbsp;&nbsp;&nbsp; <br \/>&nbsp;&nbsp;&nbsp;&nbsp;<\/span><span style=\"color: #FF8000\">\/\/&nbsp;Path&nbsp;to&nbsp;mysqldump&nbsp;binary&nbsp;(use&nbsp;'mysqldump'&nbsp;if&nbsp;it's&nbsp;on&nbsp;PATH&nbsp;or&nbsp;set&nbsp;full&nbsp;path&nbsp;on&nbsp;Windows) <br \/>&nbsp;&nbsp;&nbsp;&nbsp;<\/span><span style=\"color: #DD0000\">'mysqldump_path'&nbsp;<\/span><span style=\"color: #007700\">=&gt;&nbsp;<\/span><span style=\"color: #DD0000\">'mysqldump'<\/span><span style=\"color: #007700\">, <br \/>]; <br \/><\/span><span style=\"color: #0000BB\">?&gt;<\/span><\/pre>\n<h2>\ud83d\uddc4\ufe0f Step 2: Create Backup Script (backup.php)<\/h2>\n<p>Create the main backup script <code>backup.php<\/code> that will perform the database backup using the configuration from <code>config.php<\/code>. This script will:<\/p>\n<ul>\n<li>Load configuration settings<\/li>\n<li>Ensure the backup directory exists<\/li>\n<li>Generate a filename based on the current date and time<\/li>\n<li>Execute the <code>mysqldump<\/code> command to create a database dump<\/li>\n<li>Compress the dump using gzip<\/li>\n<li>Handle errors gracefully<\/li>\n<li>Delete old backups automatically based on the retention policy<\/li>\n<\/ul>\n<pre><span style=\"color: #0000BB\">&lt;?php <br \/><\/span><span style=\"color: #FF8000\">\/\/&nbsp;Load&nbsp;configuration <br \/><\/span><span style=\"color: #0000BB\">$config&nbsp;<\/span><span style=\"color: #007700\">=&nbsp;require&nbsp;<\/span><span style=\"color: #0000BB\">__DIR__&nbsp;<\/span><span style=\"color: #007700\">.&nbsp;<\/span><span style=\"color: #DD0000\">'\/config.php'<\/span><span style=\"color: #007700\">; <br \/> <br \/><\/span><span style=\"color: #FF8000\">\/\/&nbsp;Ensure&nbsp;backup&nbsp;directory&nbsp;exists <br \/><\/span><span style=\"color: #0000BB\">$backupDir&nbsp;<\/span><span style=\"color: #007700\">=&nbsp;<\/span><span style=\"color: #0000BB\">$config<\/span><span style=\"color: #007700\">[<\/span><span style=\"color: #DD0000\">'backup_dir'<\/span><span style=\"color: #007700\">]; <br \/>if&nbsp;(!<\/span><span style=\"color: #0000BB\">is_dir<\/span><span style=\"color: #007700\">(<\/span><span style=\"color: #0000BB\">$backupDir<\/span><span style=\"color: #007700\">))&nbsp;{ <br \/>&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(!<\/span><span style=\"color: #0000BB\">mkdir<\/span><span style=\"color: #007700\">(<\/span><span style=\"color: #0000BB\">$backupDir<\/span><span style=\"color: #007700\">,&nbsp;<\/span><span style=\"color: #0000BB\">0755<\/span><span style=\"color: #007700\">,&nbsp;<\/span><span style=\"color: #0000BB\">true<\/span><span style=\"color: #007700\">))&nbsp;{ <br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/span><span style=\"color: #0000BB\">fwrite<\/span><span style=\"color: #007700\">(<\/span><span style=\"color: #0000BB\">STDERR<\/span><span style=\"color: #007700\">,&nbsp;<\/span><span style=\"color: #DD0000\">\"Failed&nbsp;to&nbsp;create&nbsp;backup&nbsp;directory:&nbsp;<\/span><span style=\"color: #0000BB\">$backupDir<\/span><span style=\"color: #DD0000\">\\n\"<\/span><span style=\"color: #007700\">); <br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;exit(<\/span><span style=\"color: #0000BB\">2<\/span><span style=\"color: #007700\">); <br \/>&nbsp;&nbsp;&nbsp;&nbsp;} <br \/>} <br \/> <br \/><\/span><span style=\"color: #FF8000\">\/\/&nbsp;Prepare&nbsp;filename <br \/><\/span><span style=\"color: #0000BB\">$dbName&nbsp;<\/span><span style=\"color: #007700\">=&nbsp;<\/span><span style=\"color: #0000BB\">$config<\/span><span style=\"color: #007700\">[<\/span><span style=\"color: #DD0000\">'db_name'<\/span><span style=\"color: #007700\">]; <br \/><\/span><span style=\"color: #0000BB\">$date&nbsp;<\/span><span style=\"color: #007700\">=&nbsp;<\/span><span style=\"color: #0000BB\">date<\/span><span style=\"color: #007700\">(<\/span><span style=\"color: #DD0000\">'Y-m-d_H-i-s'<\/span><span style=\"color: #007700\">); <br \/><\/span><span style=\"color: #0000BB\">$filename&nbsp;<\/span><span style=\"color: #007700\">=&nbsp;<\/span><span style=\"color: #0000BB\">sprintf<\/span><span style=\"color: #007700\">(<\/span><span style=\"color: #DD0000\">'%s\/%s_%s.sql.gz'<\/span><span style=\"color: #007700\">,&nbsp;<\/span><span style=\"color: #0000BB\">rtrim<\/span><span style=\"color: #007700\">(<\/span><span style=\"color: #0000BB\">$backupDir<\/span><span style=\"color: #007700\">,&nbsp;<\/span><span style=\"color: #DD0000\">'\/\\\\'<\/span><span style=\"color: #007700\">),&nbsp;<\/span><span style=\"color: #0000BB\">$dbName<\/span><span style=\"color: #007700\">,&nbsp;<\/span><span style=\"color: #0000BB\">$date<\/span><span style=\"color: #007700\">); <br \/> <br \/><\/span><span style=\"color: #FF8000\">\/\/&nbsp;Prepare&nbsp;mysqldump&nbsp;command <br \/><\/span><span style=\"color: #0000BB\">$mysqldump&nbsp;<\/span><span style=\"color: #007700\">=&nbsp;<\/span><span style=\"color: #0000BB\">$config<\/span><span style=\"color: #007700\">[<\/span><span style=\"color: #DD0000\">'mysqldump_path'<\/span><span style=\"color: #007700\">]; <br \/> <br \/><\/span><span style=\"color: #FF8000\">\/\/&nbsp;Build&nbsp;command&nbsp;without&nbsp;exposing&nbsp;password&nbsp;in&nbsp;process&nbsp;list&nbsp;on&nbsp;*nix&nbsp;where&nbsp;possible. <br \/><\/span><span style=\"color: #0000BB\">$cmdParts&nbsp;<\/span><span style=\"color: #007700\">=&nbsp;[]; <br \/><\/span><span style=\"color: #0000BB\">$cmdParts<\/span><span style=\"color: #007700\">[]&nbsp;=&nbsp;<\/span><span style=\"color: #0000BB\">escapeshellcmd<\/span><span style=\"color: #007700\">(<\/span><span style=\"color: #0000BB\">$mysqldump<\/span><span style=\"color: #007700\">); <br \/><\/span><span style=\"color: #0000BB\">$cmdParts<\/span><span style=\"color: #007700\">[]&nbsp;=&nbsp;<\/span><span style=\"color: #DD0000\">'--host='&nbsp;<\/span><span style=\"color: #007700\">.&nbsp;<\/span><span style=\"color: #0000BB\">escapeshellarg<\/span><span style=\"color: #007700\">(<\/span><span style=\"color: #0000BB\">$config<\/span><span style=\"color: #007700\">[<\/span><span style=\"color: #DD0000\">'db_host'<\/span><span style=\"color: #007700\">]); <br \/><\/span><span style=\"color: #0000BB\">$cmdParts<\/span><span style=\"color: #007700\">[]&nbsp;=&nbsp;<\/span><span style=\"color: #DD0000\">'--user='&nbsp;<\/span><span style=\"color: #007700\">.&nbsp;<\/span><span style=\"color: #0000BB\">escapeshellarg<\/span><span style=\"color: #007700\">(<\/span><span style=\"color: #0000BB\">$config<\/span><span style=\"color: #007700\">[<\/span><span style=\"color: #DD0000\">'db_user'<\/span><span style=\"color: #007700\">]); <br \/><\/span><span style=\"color: #FF8000\">\/\/&nbsp;Use&nbsp;--password=...&nbsp;(mysqldump&nbsp;requires&nbsp;it)&nbsp;\u2014&nbsp;on&nbsp;some&nbsp;systems&nbsp;consider&nbsp;using&nbsp;~\/.my.cnf&nbsp;for&nbsp;safety. <br \/><\/span><span style=\"color: #0000BB\">$cmdParts<\/span><span style=\"color: #007700\">[]&nbsp;=&nbsp;<\/span><span style=\"color: #DD0000\">'--password='&nbsp;<\/span><span style=\"color: #007700\">.&nbsp;<\/span><span style=\"color: #0000BB\">escapeshellarg<\/span><span style=\"color: #007700\">(<\/span><span style=\"color: #0000BB\">$config<\/span><span style=\"color: #007700\">[<\/span><span style=\"color: #DD0000\">'db_pass'<\/span><span style=\"color: #007700\">]); <br \/><\/span><span style=\"color: #0000BB\">$cmdParts<\/span><span style=\"color: #007700\">[]&nbsp;=&nbsp;<\/span><span style=\"color: #DD0000\">'--single-transaction'<\/span><span style=\"color: #007700\">; <br \/><\/span><span style=\"color: #0000BB\">$cmdParts<\/span><span style=\"color: #007700\">[]&nbsp;=&nbsp;<\/span><span style=\"color: #DD0000\">'--quick'<\/span><span style=\"color: #007700\">; <br \/><\/span><span style=\"color: #0000BB\">$cmdParts<\/span><span style=\"color: #007700\">[]&nbsp;=&nbsp;<\/span><span style=\"color: #0000BB\">escapeshellarg<\/span><span style=\"color: #007700\">(<\/span><span style=\"color: #0000BB\">$dbName<\/span><span style=\"color: #007700\">); <br \/><\/span><span style=\"color: #0000BB\">$cmd&nbsp;<\/span><span style=\"color: #007700\">=&nbsp;<\/span><span style=\"color: #0000BB\">implode<\/span><span style=\"color: #007700\">(<\/span><span style=\"color: #DD0000\">'&nbsp;'<\/span><span style=\"color: #007700\">,&nbsp;<\/span><span style=\"color: #0000BB\">$cmdParts<\/span><span style=\"color: #007700\">); <br \/> <br \/><\/span><span style=\"color: #0000BB\">$descriptors&nbsp;<\/span><span style=\"color: #007700\">=&nbsp;[ <br \/>&nbsp;&nbsp;&nbsp;&nbsp;<\/span><span style=\"color: #0000BB\">1&nbsp;<\/span><span style=\"color: #007700\">=&gt;&nbsp;[<\/span><span style=\"color: #DD0000\">'pipe'<\/span><span style=\"color: #007700\">,&nbsp;<\/span><span style=\"color: #DD0000\">'w'<\/span><span style=\"color: #007700\">],&nbsp;<\/span><span style=\"color: #FF8000\">\/\/&nbsp;stdout <br \/>&nbsp;&nbsp;&nbsp;&nbsp;<\/span><span style=\"color: #0000BB\">2&nbsp;<\/span><span style=\"color: #007700\">=&gt;&nbsp;[<\/span><span style=\"color: #DD0000\">'pipe'<\/span><span style=\"color: #007700\">,&nbsp;<\/span><span style=\"color: #DD0000\">'w'<\/span><span style=\"color: #007700\">],&nbsp;<\/span><span style=\"color: #FF8000\">\/\/&nbsp;stderr <br \/><\/span><span style=\"color: #007700\">]; <br \/> <br \/><\/span><span style=\"color: #FF8000\">\/\/&nbsp;Execute&nbsp;mysqldump&nbsp;process <br \/><\/span><span style=\"color: #0000BB\">$process&nbsp;<\/span><span style=\"color: #007700\">=&nbsp;@<\/span><span style=\"color: #0000BB\">proc_open<\/span><span style=\"color: #007700\">(<\/span><span style=\"color: #0000BB\">$cmd<\/span><span style=\"color: #007700\">,&nbsp;<\/span><span style=\"color: #0000BB\">$descriptors<\/span><span style=\"color: #007700\">,&nbsp;<\/span><span style=\"color: #0000BB\">$pipes<\/span><span style=\"color: #007700\">); <br \/>if&nbsp;(!<\/span><span style=\"color: #0000BB\">is_resource<\/span><span style=\"color: #007700\">(<\/span><span style=\"color: #0000BB\">$process<\/span><span style=\"color: #007700\">))&nbsp;{ <br \/>&nbsp;&nbsp;&nbsp;&nbsp;<\/span><span style=\"color: #0000BB\">fwrite<\/span><span style=\"color: #007700\">(<\/span><span style=\"color: #0000BB\">STDERR<\/span><span style=\"color: #007700\">,&nbsp;<\/span><span style=\"color: #DD0000\">\"Failed&nbsp;to&nbsp;start&nbsp;mysqldump&nbsp;process\\n\"<\/span><span style=\"color: #007700\">); <br \/>&nbsp;&nbsp;&nbsp;&nbsp;exit(<\/span><span style=\"color: #0000BB\">3<\/span><span style=\"color: #007700\">); <br \/>} <br \/> <br \/><\/span><span style=\"color: #0000BB\">$out&nbsp;<\/span><span style=\"color: #007700\">=&nbsp;<\/span><span style=\"color: #0000BB\">$pipes<\/span><span style=\"color: #007700\">[<\/span><span style=\"color: #0000BB\">1<\/span><span style=\"color: #007700\">]; <br \/><\/span><span style=\"color: #0000BB\">$err&nbsp;<\/span><span style=\"color: #007700\">=&nbsp;<\/span><span style=\"color: #0000BB\">$pipes<\/span><span style=\"color: #007700\">[<\/span><span style=\"color: #0000BB\">2<\/span><span style=\"color: #007700\">]; <br \/> <br \/><\/span><span style=\"color: #FF8000\">\/\/&nbsp;Open&nbsp;gzip&nbsp;file&nbsp;for&nbsp;writing <br \/><\/span><span style=\"color: #0000BB\">$gz&nbsp;<\/span><span style=\"color: #007700\">=&nbsp;@<\/span><span style=\"color: #0000BB\">gzopen<\/span><span style=\"color: #007700\">(<\/span><span style=\"color: #0000BB\">$filename<\/span><span style=\"color: #007700\">,&nbsp;<\/span><span style=\"color: #DD0000\">'w9'<\/span><span style=\"color: #007700\">); <br \/>if&nbsp;(<\/span><span style=\"color: #0000BB\">$gz&nbsp;<\/span><span style=\"color: #007700\">===&nbsp;<\/span><span style=\"color: #0000BB\">false<\/span><span style=\"color: #007700\">)&nbsp;{ <br \/>&nbsp;&nbsp;&nbsp;&nbsp;<\/span><span style=\"color: #0000BB\">fclose<\/span><span style=\"color: #007700\">(<\/span><span style=\"color: #0000BB\">$out<\/span><span style=\"color: #007700\">); <br \/>&nbsp;&nbsp;&nbsp;&nbsp;<\/span><span style=\"color: #0000BB\">fclose<\/span><span style=\"color: #007700\">(<\/span><span style=\"color: #0000BB\">$err<\/span><span style=\"color: #007700\">); <br \/>&nbsp;&nbsp;&nbsp;&nbsp;<\/span><span style=\"color: #0000BB\">proc_close<\/span><span style=\"color: #007700\">(<\/span><span style=\"color: #0000BB\">$process<\/span><span style=\"color: #007700\">); <br \/>&nbsp;&nbsp;&nbsp;&nbsp;<\/span><span style=\"color: #0000BB\">fwrite<\/span><span style=\"color: #007700\">(<\/span><span style=\"color: #0000BB\">STDERR<\/span><span style=\"color: #007700\">,&nbsp;<\/span><span style=\"color: #DD0000\">\"Failed&nbsp;to&nbsp;open&nbsp;output&nbsp;file&nbsp;for&nbsp;writing:&nbsp;<\/span><span style=\"color: #0000BB\">$filename<\/span><span style=\"color: #DD0000\">\\n\"<\/span><span style=\"color: #007700\">); <br \/>&nbsp;&nbsp;&nbsp;&nbsp;exit(<\/span><span style=\"color: #0000BB\">4<\/span><span style=\"color: #007700\">); <br \/>} <br \/> <br \/><\/span><span style=\"color: #FF8000\">\/\/&nbsp;Stream&nbsp;dump&nbsp;output&nbsp;into&nbsp;gz&nbsp;file <br \/><\/span><span style=\"color: #007700\">while&nbsp;(!<\/span><span style=\"color: #0000BB\">feof<\/span><span style=\"color: #007700\">(<\/span><span style=\"color: #0000BB\">$out<\/span><span style=\"color: #007700\">))&nbsp;{ <br \/>&nbsp;&nbsp;&nbsp;&nbsp;<\/span><span style=\"color: #0000BB\">$chunk&nbsp;<\/span><span style=\"color: #007700\">=&nbsp;<\/span><span style=\"color: #0000BB\">fread<\/span><span style=\"color: #007700\">(<\/span><span style=\"color: #0000BB\">$out<\/span><span style=\"color: #007700\">,&nbsp;<\/span><span style=\"color: #0000BB\">8192<\/span><span style=\"color: #007700\">); <br \/>&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(<\/span><span style=\"color: #0000BB\">$chunk&nbsp;<\/span><span style=\"color: #007700\">===&nbsp;<\/span><span style=\"color: #0000BB\">false<\/span><span style=\"color: #007700\">)&nbsp;break; <br \/>&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(<\/span><span style=\"color: #0000BB\">$chunk&nbsp;<\/span><span style=\"color: #007700\">!==&nbsp;<\/span><span style=\"color: #DD0000\">''<\/span><span style=\"color: #007700\">)&nbsp;<\/span><span style=\"color: #0000BB\">gzwrite<\/span><span style=\"color: #007700\">(<\/span><span style=\"color: #0000BB\">$gz<\/span><span style=\"color: #007700\">,&nbsp;<\/span><span style=\"color: #0000BB\">$chunk<\/span><span style=\"color: #007700\">); <br \/>} <br \/> <br \/><\/span><span style=\"color: #FF8000\">\/\/&nbsp;Capture&nbsp;any&nbsp;stderr&nbsp;output <br \/><\/span><span style=\"color: #0000BB\">$stderrOutput&nbsp;<\/span><span style=\"color: #007700\">=&nbsp;<\/span><span style=\"color: #0000BB\">stream_get_contents<\/span><span style=\"color: #007700\">(<\/span><span style=\"color: #0000BB\">$err<\/span><span style=\"color: #007700\">); <br \/><\/span><span style=\"color: #0000BB\">fclose<\/span><span style=\"color: #007700\">(<\/span><span style=\"color: #0000BB\">$out<\/span><span style=\"color: #007700\">); <br \/><\/span><span style=\"color: #0000BB\">fclose<\/span><span style=\"color: #007700\">(<\/span><span style=\"color: #0000BB\">$err<\/span><span style=\"color: #007700\">); <br \/> <br \/><\/span><span style=\"color: #FF8000\">\/\/&nbsp;Close&nbsp;process&nbsp;and&nbsp;gzip&nbsp;file <br \/><\/span><span style=\"color: #0000BB\">$exitCode&nbsp;<\/span><span style=\"color: #007700\">=&nbsp;<\/span><span style=\"color: #0000BB\">proc_close<\/span><span style=\"color: #007700\">(<\/span><span style=\"color: #0000BB\">$process<\/span><span style=\"color: #007700\">); <br \/><\/span><span style=\"color: #0000BB\">gzclose<\/span><span style=\"color: #007700\">(<\/span><span style=\"color: #0000BB\">$gz<\/span><span style=\"color: #007700\">); <br \/> <br \/><\/span><span style=\"color: #FF8000\">\/\/&nbsp;Check&nbsp;for&nbsp;errors <br \/><\/span><span style=\"color: #007700\">if&nbsp;(<\/span><span style=\"color: #0000BB\">$exitCode&nbsp;<\/span><span style=\"color: #007700\">!==&nbsp;<\/span><span style=\"color: #0000BB\">0<\/span><span style=\"color: #007700\">)&nbsp;{ <br \/>&nbsp;&nbsp;&nbsp;&nbsp;@<\/span><span style=\"color: #0000BB\">unlink<\/span><span style=\"color: #007700\">(<\/span><span style=\"color: #0000BB\">$filename<\/span><span style=\"color: #007700\">); <br \/>&nbsp;&nbsp;&nbsp;&nbsp;<\/span><span style=\"color: #0000BB\">fwrite<\/span><span style=\"color: #007700\">(<\/span><span style=\"color: #0000BB\">STDERR<\/span><span style=\"color: #007700\">,&nbsp;<\/span><span style=\"color: #DD0000\">\"mysqldump&nbsp;failed&nbsp;(code&nbsp;<\/span><span style=\"color: #0000BB\">$exitCode<\/span><span style=\"color: #DD0000\">):&nbsp;<\/span><span style=\"color: #0000BB\">$stderrOutput<\/span><span style=\"color: #DD0000\">\\n\"<\/span><span style=\"color: #007700\">); <br \/>&nbsp;&nbsp;&nbsp;&nbsp;exit(<\/span><span style=\"color: #0000BB\">$exitCode<\/span><span style=\"color: #007700\">); <br \/>} <br \/> <br \/>echo&nbsp;<\/span><span style=\"color: #DD0000\">\"Backup&nbsp;created:&nbsp;<\/span><span style=\"color: #0000BB\">$filename<\/span><span style=\"color: #DD0000\">\\n\"<\/span><span style=\"color: #007700\">; <br \/> <br \/><\/span><span style=\"color: #FF8000\">\/\/&nbsp;Rotate&nbsp;old&nbsp;backups <br \/><\/span><span style=\"color: #0000BB\">$retention&nbsp;<\/span><span style=\"color: #007700\">=&nbsp;(int)<\/span><span style=\"color: #0000BB\">$config<\/span><span style=\"color: #007700\">[<\/span><span style=\"color: #DD0000\">'retention_days'<\/span><span style=\"color: #007700\">]; <br \/>if&nbsp;(<\/span><span style=\"color: #0000BB\">$retention&nbsp;<\/span><span style=\"color: #007700\">&gt;&nbsp;<\/span><span style=\"color: #0000BB\">0<\/span><span style=\"color: #007700\">)&nbsp;{ <br \/>&nbsp;&nbsp;&nbsp;&nbsp;<\/span><span style=\"color: #0000BB\">$pattern&nbsp;<\/span><span style=\"color: #007700\">=&nbsp;<\/span><span style=\"color: #0000BB\">rtrim<\/span><span style=\"color: #007700\">(<\/span><span style=\"color: #0000BB\">$backupDir<\/span><span style=\"color: #007700\">,&nbsp;<\/span><span style=\"color: #DD0000\">'\/\\\\'<\/span><span style=\"color: #007700\">)&nbsp;.&nbsp;<\/span><span style=\"color: #DD0000\">'\/'&nbsp;<\/span><span style=\"color: #007700\">.&nbsp;<\/span><span style=\"color: #0000BB\">$dbName&nbsp;<\/span><span style=\"color: #007700\">.&nbsp;<\/span><span style=\"color: #DD0000\">'_*.sql.gz'<\/span><span style=\"color: #007700\">; <br \/>&nbsp;&nbsp;&nbsp;&nbsp;foreach&nbsp;(<\/span><span style=\"color: #0000BB\">glob<\/span><span style=\"color: #007700\">(<\/span><span style=\"color: #0000BB\">$pattern<\/span><span style=\"color: #007700\">)&nbsp;as&nbsp;<\/span><span style=\"color: #0000BB\">$file<\/span><span style=\"color: #007700\">)&nbsp;{ <br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(<\/span><span style=\"color: #0000BB\">filemtime<\/span><span style=\"color: #007700\">(<\/span><span style=\"color: #0000BB\">$file<\/span><span style=\"color: #007700\">)&nbsp;&lt;&nbsp;<\/span><span style=\"color: #0000BB\">time<\/span><span style=\"color: #007700\">()&nbsp;-&nbsp;(<\/span><span style=\"color: #0000BB\">$retention&nbsp;<\/span><span style=\"color: #007700\">*&nbsp;<\/span><span style=\"color: #0000BB\">86400<\/span><span style=\"color: #007700\">))&nbsp;{ <br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@<\/span><span style=\"color: #0000BB\">unlink<\/span><span style=\"color: #007700\">(<\/span><span style=\"color: #0000BB\">$file<\/span><span style=\"color: #007700\">); <br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} <br \/>&nbsp;&nbsp;&nbsp;&nbsp;} <br \/>} <br \/> <br \/>exit(<\/span><span style=\"color: #0000BB\">0<\/span><span style=\"color: #007700\">); <br \/><\/span><span style=\"color: #0000BB\">?&gt;<\/span><\/pre>\n<h2>\ud83d\udd52 Step 3: Test Backup Script Manually<\/h2>\n<p>Before scheduling the backup script, it\u2019s important to test it manually to ensure it works as expected.<\/p>\n<ol>\n<li>Open your terminal or command prompt.<\/li>\n<li>Navigate to the project directory where <code>backup.php<\/code> is located.<\/li>\n<li>Run the script using the PHP CLI:\n<pre>php backup.php<\/pre>\n<p>If everything is configured correctly, you\u2019ll see:<\/p>\n<pre>Backup created: \/path\/to\/your\/project\/backups\/your_database_YYYY-MM-DD_HH-MM-SS.sql.gz<\/pre>\n<\/li>\n<li>Check the <code>backups\/<\/code> directory to verify that a new backup file has been created.<\/li>\n<li>Review any output messages for errors or confirmations.<\/li>\n<\/ol>\n<h2>\u23f0 Step 4: Schedule Auto Backups<\/h2>\n<p>Now that you have the backup script ready, you need to schedule it to run automatically at regular intervals.<\/p>\n<h3>Scheduling on Linux\/macOS using Cron:<\/h3>\n<ol>\n<li>Open your terminal.<\/li>\n<li>Edit your crontab file by running:\n<pre>crontab -e<\/pre>\n<\/li>\n<li>Add a new line to schedule the backup script. For example, to run the backup every day at 2 AM, add:\n<pre>0 2 * * * \/usr\/bin\/php \/path\/to\/your\/project\/backup.php<\/pre>\n<p>Make sure to replace <code>\/usr\/bin\/php<\/code> with the path to your PHP executable and <code>\/path\/to\/your\/project\/backup.php<\/code> with the actual path to your script.<\/li>\n<li>Save and exit the crontab editor.<\/li>\n<li>Your backups will now run automatically at the scheduled time.<\/li>\n<\/ol>\n<p><b>Cron Timing Format:<\/b><\/p>\n<pre>\r\n* * * * *\r\n\u2502 \u2502 \u2502 \u2502 \u2502\r\n\u2502 \u2502 \u2502 \u2502 \u2514\u2500\u2500 Day of week\r\n\u2502 \u2502 \u2502 \u2514\u2500\u2500\u2500\u2500 Month\r\n\u2502 \u2502 \u2514\u2500\u2500\u2500\u2500\u2500\u2500 Day of month\r\n\u2502 \u2514\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500 Hour\r\n\u2514\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500 Minute\r\n<\/pre>\n<h3>Scheduling on Windows using Task Scheduler:<\/h3>\n<ol>\n<li>Open <b>Task Scheduler<\/b> from the Start menu.<\/li>\n<li>Click on <b>Create Basic Task&#8230;<\/b> in the Actions pane.<\/li>\n<li>Follow the wizard to name your task and set the trigger (e.g., daily at 2 AM).<\/li>\n<li>For the action, select <b>Start a program<\/b>.<\/li>\n<li>In the &#8220;Program\/script&#8221; field, enter the path to your PHP executable (e.g., <code>C:\\path\\to\\php.exe<\/code>).<\/li>\n<li>In the &#8220;Add arguments (optional)&#8221; field, enter the path to your <code>backup.php<\/code> script (e.g., <code>C:\\path\\to\\your\\project\\backup.php<\/code>).<\/li>\n<li>Finish the wizard to create the task.<\/li>\n<li>Your backups will now run automatically at the scheduled time.<\/li>\n<\/ol>\n<h2>\ud83d\udd10 Security Best Practices<\/h2>\n<ul>\n<li>Store backups outside the public web directory<\/li>\n<li>Restrict folder access using <code>.htaccess<\/code><\/li>\n<li>Use a dedicated MySQL user with read-only access<\/li>\n<li>Rotate backups regularly using retention policy<\/li>\n<li>Optionally upload backups to cloud storage (S3, Google Drive)<\/li>\n<\/ul>\n<h2>\u2705 Final Thoughts<\/h2>\n<p>Automating MySQL backups using PHP and Cron is a simple yet powerful solution for protecting your application data. With compression, retention cleanup, and proper scheduling, this script is production-ready and easy to maintain.<\/p>\n<p>You can safely run this script on shared hosting, VPS, or dedicated servers with minimal configuration.<\/p>\n<p>Here are some additional enhancements you might consider:<\/p>\n<ul>\n<li>Encrypt backups for added security<\/li>\n<li>Send email notifications on backup success\/failure<\/li>\n<li>Integrate with cloud storage APIs for offsite backups (AWS S3 \/ Google Drive)<\/li>\n<li>Multiple database support<\/li>\n<li>Implement logging in database for backup operations<\/li>\n<\/ul>\n<p>Happy coding and stay backed up! \ud83d\ude80<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Automatically backing up your MySQL database is critical for production websites and applications. Manual backups are error-prone and often forgotten, while automated backups ensure your data is safe even if something goes wrong. In this <\/p>\n","protected":false},"author":1,"featured_media":5996,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[4],"tags":[395,98,19,14],"class_list":["post-5992","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-php","tag-automation","tag-database","tag-mysql","tag-php","cat-4-id","has_thumb"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.4 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Auto Backup MySQL Database using PHP Cron (Linux &amp; Windows) - CodexWorld<\/title>\n<meta name=\"description\" content=\"Learn how to auto backup a MySQL database using PHP cron job or Windows Task Scheduler. Create compressed backups, rotate old files, and secure your data automatically.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.codexworld.com\/auto-backup-mysql-database-using-php-cron\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Auto Backup MySQL Database using PHP Cron (Linux &amp; Windows) - CodexWorld\" \/>\n<meta property=\"og:description\" content=\"Learn how to auto backup a MySQL database using PHP cron job or Windows Task Scheduler. Create compressed backups, rotate old files, and secure your data automatically.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.codexworld.com\/auto-backup-mysql-database-using-php-cron\/\" \/>\n<meta property=\"og:site_name\" content=\"CodexWorld\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/codexworld\" \/>\n<meta property=\"article:author\" content=\"https:\/\/www.facebook.com\/codexworld\" \/>\n<meta property=\"article:published_time\" content=\"2025-12-27T10:30:30+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2026-01-16T14:48:29+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.codexworld.com\/wp-content\/uploads\/2025\/12\/auto-backup-mysql-database-using-php-cron-codexworld.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"1344\" \/>\n\t<meta property=\"og:image:height\" content=\"768\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"CodexWorld\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@codexworldblog\" \/>\n<meta name=\"twitter:site\" content=\"@codexworldweb\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"CodexWorld\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"4 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.codexworld.com\\\/auto-backup-mysql-database-using-php-cron\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.codexworld.com\\\/auto-backup-mysql-database-using-php-cron\\\/\"},\"author\":{\"name\":\"CodexWorld\",\"@id\":\"https:\\\/\\\/www.codexworld.com\\\/#\\\/schema\\\/person\\\/9da51d8fa3cdefeb5ec9c69136d4baf0\"},\"headline\":\"Auto Backup MySQL Database using PHP Cron (Linux &#038; Windows)\",\"datePublished\":\"2025-12-27T10:30:30+00:00\",\"dateModified\":\"2026-01-16T14:48:29+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.codexworld.com\\\/auto-backup-mysql-database-using-php-cron\\\/\"},\"wordCount\":758,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\\\/\\\/www.codexworld.com\\\/#organization\"},\"image\":{\"@id\":\"https:\\\/\\\/www.codexworld.com\\\/auto-backup-mysql-database-using-php-cron\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.codexworld.com\\\/wp-content\\\/uploads\\\/2025\\\/12\\\/auto-backup-mysql-database-using-php-cron-codexworld.jpg\",\"keywords\":[\"Automation\",\"Database\",\"MySQL\",\"PHP\"],\"articleSection\":[\"PHP\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/www.codexworld.com\\\/auto-backup-mysql-database-using-php-cron\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.codexworld.com\\\/auto-backup-mysql-database-using-php-cron\\\/\",\"url\":\"https:\\\/\\\/www.codexworld.com\\\/auto-backup-mysql-database-using-php-cron\\\/\",\"name\":\"Auto Backup MySQL Database using PHP Cron (Linux & Windows) - CodexWorld\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.codexworld.com\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/www.codexworld.com\\\/auto-backup-mysql-database-using-php-cron\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/www.codexworld.com\\\/auto-backup-mysql-database-using-php-cron\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.codexworld.com\\\/wp-content\\\/uploads\\\/2025\\\/12\\\/auto-backup-mysql-database-using-php-cron-codexworld.jpg\",\"datePublished\":\"2025-12-27T10:30:30+00:00\",\"dateModified\":\"2026-01-16T14:48:29+00:00\",\"description\":\"Learn how to auto backup a MySQL database using PHP cron job or Windows Task Scheduler. Create compressed backups, rotate old files, and secure your data automatically.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.codexworld.com\\\/auto-backup-mysql-database-using-php-cron\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.codexworld.com\\\/auto-backup-mysql-database-using-php-cron\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.codexworld.com\\\/auto-backup-mysql-database-using-php-cron\\\/#primaryimage\",\"url\":\"https:\\\/\\\/www.codexworld.com\\\/wp-content\\\/uploads\\\/2025\\\/12\\\/auto-backup-mysql-database-using-php-cron-codexworld.jpg\",\"contentUrl\":\"https:\\\/\\\/www.codexworld.com\\\/wp-content\\\/uploads\\\/2025\\\/12\\\/auto-backup-mysql-database-using-php-cron-codexworld.jpg\",\"width\":1344,\"height\":768,\"caption\":\"auto-backup-mysql-database-using-php-cron-codexworld\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.codexworld.com\\\/auto-backup-mysql-database-using-php-cron\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.codexworld.com\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Auto Backup MySQL Database using PHP Cron (Linux &#038; Windows)\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.codexworld.com\\\/#website\",\"url\":\"https:\\\/\\\/www.codexworld.com\\\/\",\"name\":\"CodexWorld\",\"description\":\"Web &amp; Mobile App Development Company\",\"publisher\":{\"@id\":\"https:\\\/\\\/www.codexworld.com\\\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.codexworld.com\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/www.codexworld.com\\\/#organization\",\"name\":\"CodexWorld\",\"url\":\"https:\\\/\\\/www.codexworld.com\\\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.codexworld.com\\\/#\\\/schema\\\/logo\\\/image\\\/\",\"url\":\"https:\\\/\\\/www.codexworld.com\\\/wp-content\\\/uploads\\\/2014\\\/09\\\/codexworld-logo.png\",\"contentUrl\":\"https:\\\/\\\/www.codexworld.com\\\/wp-content\\\/uploads\\\/2014\\\/09\\\/codexworld-logo.png\",\"width\":200,\"height\":19,\"caption\":\"CodexWorld\"},\"image\":{\"@id\":\"https:\\\/\\\/www.codexworld.com\\\/#\\\/schema\\\/logo\\\/image\\\/\"},\"sameAs\":[\"https:\\\/\\\/www.facebook.com\\\/codexworld\",\"https:\\\/\\\/x.com\\\/codexworldweb\",\"https:\\\/\\\/www.linkedin.com\\\/company\\\/codexworld\",\"https:\\\/\\\/www.youtube.com\\\/codexworld\"]},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/www.codexworld.com\\\/#\\\/schema\\\/person\\\/9da51d8fa3cdefeb5ec9c69136d4baf0\",\"name\":\"CodexWorld\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/cf4999db3b409de559f80677afa01729bb2eeda19be273c254e8b2c22729e386?s=96&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/cf4999db3b409de559f80677afa01729bb2eeda19be273c254e8b2c22729e386?s=96&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/cf4999db3b409de559f80677afa01729bb2eeda19be273c254e8b2c22729e386?s=96&r=g\",\"caption\":\"CodexWorld\"},\"description\":\"CodexWorld is a programming blog, one-stop destination for web professionals \u2014 developers, programmers, freelancers, and site owners.\",\"sameAs\":[\"http:\\\/\\\/www.codexworld.com\",\"https:\\\/\\\/www.facebook.com\\\/codexworld\",\"https:\\\/\\\/x.com\\\/codexworldblog\"],\"url\":\"https:\\\/\\\/www.codexworld.com\\\/author\\\/nitya192265\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Auto Backup MySQL Database using PHP Cron (Linux & Windows) - CodexWorld","description":"Learn how to auto backup a MySQL database using PHP cron job or Windows Task Scheduler. Create compressed backups, rotate old files, and secure your data automatically.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.codexworld.com\/auto-backup-mysql-database-using-php-cron\/","og_locale":"en_US","og_type":"article","og_title":"Auto Backup MySQL Database using PHP Cron (Linux & Windows) - CodexWorld","og_description":"Learn how to auto backup a MySQL database using PHP cron job or Windows Task Scheduler. Create compressed backups, rotate old files, and secure your data automatically.","og_url":"https:\/\/www.codexworld.com\/auto-backup-mysql-database-using-php-cron\/","og_site_name":"CodexWorld","article_publisher":"https:\/\/www.facebook.com\/codexworld","article_author":"https:\/\/www.facebook.com\/codexworld","article_published_time":"2025-12-27T10:30:30+00:00","article_modified_time":"2026-01-16T14:48:29+00:00","og_image":[{"width":1344,"height":768,"url":"https:\/\/www.codexworld.com\/wp-content\/uploads\/2025\/12\/auto-backup-mysql-database-using-php-cron-codexworld.jpg","type":"image\/jpeg"}],"author":"CodexWorld","twitter_card":"summary_large_image","twitter_creator":"@codexworldblog","twitter_site":"@codexworldweb","twitter_misc":{"Written by":"CodexWorld","Est. reading time":"4 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.codexworld.com\/auto-backup-mysql-database-using-php-cron\/#article","isPartOf":{"@id":"https:\/\/www.codexworld.com\/auto-backup-mysql-database-using-php-cron\/"},"author":{"name":"CodexWorld","@id":"https:\/\/www.codexworld.com\/#\/schema\/person\/9da51d8fa3cdefeb5ec9c69136d4baf0"},"headline":"Auto Backup MySQL Database using PHP Cron (Linux &#038; Windows)","datePublished":"2025-12-27T10:30:30+00:00","dateModified":"2026-01-16T14:48:29+00:00","mainEntityOfPage":{"@id":"https:\/\/www.codexworld.com\/auto-backup-mysql-database-using-php-cron\/"},"wordCount":758,"commentCount":0,"publisher":{"@id":"https:\/\/www.codexworld.com\/#organization"},"image":{"@id":"https:\/\/www.codexworld.com\/auto-backup-mysql-database-using-php-cron\/#primaryimage"},"thumbnailUrl":"https:\/\/www.codexworld.com\/wp-content\/uploads\/2025\/12\/auto-backup-mysql-database-using-php-cron-codexworld.jpg","keywords":["Automation","Database","MySQL","PHP"],"articleSection":["PHP"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.codexworld.com\/auto-backup-mysql-database-using-php-cron\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.codexworld.com\/auto-backup-mysql-database-using-php-cron\/","url":"https:\/\/www.codexworld.com\/auto-backup-mysql-database-using-php-cron\/","name":"Auto Backup MySQL Database using PHP Cron (Linux & Windows) - CodexWorld","isPartOf":{"@id":"https:\/\/www.codexworld.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.codexworld.com\/auto-backup-mysql-database-using-php-cron\/#primaryimage"},"image":{"@id":"https:\/\/www.codexworld.com\/auto-backup-mysql-database-using-php-cron\/#primaryimage"},"thumbnailUrl":"https:\/\/www.codexworld.com\/wp-content\/uploads\/2025\/12\/auto-backup-mysql-database-using-php-cron-codexworld.jpg","datePublished":"2025-12-27T10:30:30+00:00","dateModified":"2026-01-16T14:48:29+00:00","description":"Learn how to auto backup a MySQL database using PHP cron job or Windows Task Scheduler. Create compressed backups, rotate old files, and secure your data automatically.","breadcrumb":{"@id":"https:\/\/www.codexworld.com\/auto-backup-mysql-database-using-php-cron\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.codexworld.com\/auto-backup-mysql-database-using-php-cron\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.codexworld.com\/auto-backup-mysql-database-using-php-cron\/#primaryimage","url":"https:\/\/www.codexworld.com\/wp-content\/uploads\/2025\/12\/auto-backup-mysql-database-using-php-cron-codexworld.jpg","contentUrl":"https:\/\/www.codexworld.com\/wp-content\/uploads\/2025\/12\/auto-backup-mysql-database-using-php-cron-codexworld.jpg","width":1344,"height":768,"caption":"auto-backup-mysql-database-using-php-cron-codexworld"},{"@type":"BreadcrumbList","@id":"https:\/\/www.codexworld.com\/auto-backup-mysql-database-using-php-cron\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.codexworld.com\/"},{"@type":"ListItem","position":2,"name":"Auto Backup MySQL Database using PHP Cron (Linux &#038; Windows)"}]},{"@type":"WebSite","@id":"https:\/\/www.codexworld.com\/#website","url":"https:\/\/www.codexworld.com\/","name":"CodexWorld","description":"Web &amp; Mobile App Development Company","publisher":{"@id":"https:\/\/www.codexworld.com\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.codexworld.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.codexworld.com\/#organization","name":"CodexWorld","url":"https:\/\/www.codexworld.com\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.codexworld.com\/#\/schema\/logo\/image\/","url":"https:\/\/www.codexworld.com\/wp-content\/uploads\/2014\/09\/codexworld-logo.png","contentUrl":"https:\/\/www.codexworld.com\/wp-content\/uploads\/2014\/09\/codexworld-logo.png","width":200,"height":19,"caption":"CodexWorld"},"image":{"@id":"https:\/\/www.codexworld.com\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/codexworld","https:\/\/x.com\/codexworldweb","https:\/\/www.linkedin.com\/company\/codexworld","https:\/\/www.youtube.com\/codexworld"]},{"@type":"Person","@id":"https:\/\/www.codexworld.com\/#\/schema\/person\/9da51d8fa3cdefeb5ec9c69136d4baf0","name":"CodexWorld","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/cf4999db3b409de559f80677afa01729bb2eeda19be273c254e8b2c22729e386?s=96&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/cf4999db3b409de559f80677afa01729bb2eeda19be273c254e8b2c22729e386?s=96&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/cf4999db3b409de559f80677afa01729bb2eeda19be273c254e8b2c22729e386?s=96&r=g","caption":"CodexWorld"},"description":"CodexWorld is a programming blog, one-stop destination for web professionals \u2014 developers, programmers, freelancers, and site owners.","sameAs":["http:\/\/www.codexworld.com","https:\/\/www.facebook.com\/codexworld","https:\/\/x.com\/codexworldblog"],"url":"https:\/\/www.codexworld.com\/author\/nitya192265\/"}]}},"jetpack_publicize_connections":[],"jetpack_featured_media_url":"https:\/\/www.codexworld.com\/wp-content\/uploads\/2025\/12\/auto-backup-mysql-database-using-php-cron-codexworld.jpg","jetpack_shortlink":"https:\/\/wp.me\/p6bxIh-1yE","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.codexworld.com\/wp-json\/wp\/v2\/posts\/5992","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.codexworld.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.codexworld.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.codexworld.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.codexworld.com\/wp-json\/wp\/v2\/comments?post=5992"}],"version-history":[{"count":5,"href":"https:\/\/www.codexworld.com\/wp-json\/wp\/v2\/posts\/5992\/revisions"}],"predecessor-version":[{"id":5998,"href":"https:\/\/www.codexworld.com\/wp-json\/wp\/v2\/posts\/5992\/revisions\/5998"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.codexworld.com\/wp-json\/wp\/v2\/media\/5996"}],"wp:attachment":[{"href":"https:\/\/www.codexworld.com\/wp-json\/wp\/v2\/media?parent=5992"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.codexworld.com\/wp-json\/wp\/v2\/categories?post=5992"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.codexworld.com\/wp-json\/wp\/v2\/tags?post=5992"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}