Mysqlexport – Exporting the Database with PHP

Whether you are a programmer or a website owner, it’s up to everyone; you want to back up your databases but you use shared hosting and it becomes impossible to export and back up your databases because of problems from the hosting company. Even if you do not have such a problem, you can manage your own server like me, and it’s hard for you to back up many databases manually. You would like to have a system (PHP script) that automatically backs up all your databases. Anyway, the programmer is lazy, the programmers are people who automate or facilitate everything to stay lazy.

I also created a PHP class to automatically back up the databases on my server. I will get rid of this problem by creating a cronjob. You can either use it for automatic backup like me or you can create a custom page to backup with a password you will enter via the web.

You can find my “mysqlexport” class on github. You can back up your databases as you like with the following sample usage. You should only do this with a user who has access to all databases. Otherwise, it will only back up the database if it has permission.

Usage Steps;

  1. First, we include our class.
  2. We create a new “mysqlexport” class instance. In the first parameter, we write the path to be backed up. We then write the database connection address (usual localhost), the username, and the password as parameters.
  3. After that, we have two choices; we define the databases we want to back up in an array with setDatabases(array $databases), or we get the names of the databases with the getDatabases() function.
  4. Finally, we end the backup with the export() function.
<?php
include 'Export.php';
$export = new MysqlExport\Export('backup', 'localhost', 'root', 'dev1');
$export->getDatabases();
$export->export();
0 0 votes
Article Rating
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Newest
Oldest Most Voted
Inline Feedbacks
View all comments