This text will focus on learn how to create a backup of a MySQL database utilizing PHP and automate the method of backup.
Robotically Backup MySQL Database Utilizing PHP
To put in writing PHP code, open any code editor. For this put up, “Visible Studio Code” is getting used:
Create a PHP file named “db_backup.php”:
Sort this code and supply your MySQL database credentials:
Outline the Backup listing, the place the backup recordsdata can be saved:
Set the date format for the title of backup file:
$date = date(‘Y-m-d_H-i-s’);
Outline the “backup_file”:
$backup_file = BACKUP_DIR . ‘/’ . DB_NAME . ‘-‘ . $date . ‘.sql’;
To create a backup file use the mysqldump utility, and supply the database credentials:
system($command);
Compress the backup file utilizing the “gzip” software:
system($gzip_command);
Sort this piece of code to take away the outdated backup recordsdata, for this put up the recordsdata which might be “7” days outdated can be deleted:
system($find_command);
Save the file, and run it to substantiate if the backup file is created or not. Open the code editor terminal and sort the command to execute the file:
Record the listing to see if the backup file exists. The output shows the backup file that’s created efficiently:
To automate the method of backup, open the beginning menu, search “Process Scheduler” and click on on the “Open” button:
From the “Actions” and press the “Create Process” choice:
A brand new wizard will open. Head into the “Common” tab and supply the duty’s title. Choose the choice that ensures the backup even when the person is logged out:
Navigate to the “Triggers” tab and press the “New” button:
Choose the choice “On a schedule”. Choose the scheduled time as “Each day” and alter the “Begin” time. Within the Superior settings verify the “Enabled” choice and press the “OK” button:
The standing will change to “Enabled”:
Choose the “Actions” tab and click on on the “New” button:
Sort the title for “Motion” and browse for the “Program/script” PHP file you created and “Add arguments” and click on on “OK”:
The motion can be created efficiently:
Go to the “Situations” tab and verify the checkbox “Wake the pc to rub this activity”:
Within the “Settings” tab and choose the choices as displayed within the output and alter the time for the restart and click on on the “OK” button:
The immediate will seem, kind the credentials and click on on the “OK” button:
Your MySQL database will routinely backup. Verify the backup listing to see the backup recordsdata when wanted:
You’ve gotten efficiently created the PHP file to backup MySQL database, then to automate the backup course of you used the Process Scheduler.
Conclusion
Use the code editor to create a PHP file, present the MySQL database credentials, and backup file title format and date format. Use the mysqldump command to create an SQL file and gzip software to compress the SQL file. Use the Process Scheduler for the automation of the backup course of. This put up demonstrated learn how to routinely backup a MySQL database utilizing PHP.