How to take backup of mySQL database using PHP

Database backups are really important for any website or application. If your database crashes and you don’t have any backup then you could lose a lot of business and what’s more your credibility could be at stake.

Fortunately all leading databases provide an option to take backups and restore them as well which help you to overcome any untoward situation.

Now I know most people would say to perform the backup using PHPMyAdmin which is fine if the size of the database is not large but if the size is very large then this would not only be a time taking process but there would also be no guarantee that the backup taken is reliable or not.

Its not the number of backups you take that count but the restores which get the job done

Create a PHP script and write the following line which will not only take the backup but gzip it to a location you specify.

exec("mysqldump -u <db username> -p<db password> <db name> --verbose | gzip > <path to file where the back up will be gzipped and saved>);

Now you can create a cron job which will run the above script at the time you specify.

I hope the above helped.


Please follow and like us:

Be the first to comment

Leave a Reply

Your email address will not be published.


*