Using PHP to Back Up All of Your MySQL Databases
If you haven't worked too much with the Linux bash (or command line), the following tasks may seem over-complicated to you:
- Run a PHP script every day at a specified time (a cron job)
- Have that PHP script get a list of all databases for a given user on a server
- Back up all of those databases
So in the interest of helping you out, I gift thee a guide...
First let's get that PHP script. You can find it on GitHub in the file backup.php or by clicking the following link: https://github.com/janhartigan/phpMySQLAutoBackup. Create a directory somewhere on your server (outside of your web root) called scripts and place backup.php in that directory.
Now that the script is on your server, let's go over it and see what it does.
Here you must define the following items:
- $dbhost: This is normally localhost, unless your mysql driver is hosted on a foreign server
- $dbuser: The mysql user that will connect to the database
- $dbpass: The password for $dbuser
- $path: The directory path where you want to store your backups. On my server, I store them in /home/admin/backups/mysql, but you can store them anywhere you like
