What is Database backup ??

Database backup is a process of creating the duplicate copy of your database with architecture  and it’s stored data. A backup is a duplicate copy of your database that is use to reconstruct or restore your database structure and data.

Basically database is a structured form of data that is stored into a computer. it is a collection of schemas, tables and queries. Database is a very important part of your system. It is use to store all necessary and important data. We use this stored data in various ways.

Some Popular Database management sytems are as given below

1: Mysql

2: Microsoft SQL Server

3: Postgre SQL

4: Oracle Database

5: Microsoft Access

6: SQLite

7: Maria DB

Why Database backup is important ??

It is clear that if you want to recover any disaster, you should take the proper backup of your databases. As well as Database backup will also help you while you are upgrading your database.

You should take your database backup as regualr basis.

How to take backup of you Mysql Database using PHP  ??

Step 1: Create a connection file

connection.php

 

In the above php code i declare 4 constant variables with the names HOST, USER, PASSWORD, DATABASE . These variables will be use in following code

Step 2: I will create a function backup_mydb() as follow which take five parameters. Here you will pass your host, database username, database password and the name of database.

db_backup.php

in the above php code i created a function with the name backup_mydb() in which i passed 5 parameters respectively 4 constant variables HOST, USER, PASSWORD, DATABASE, which are defined in step 1 and last is $table that describes all tables of database  . Try to re-establish database connection and store it on $link and then select database.

if $table ==”*”,  it means you want to take the backup of all tables. i declare an empty array $tables and make a query with SHOW TABLES command of MYSQL which return the list of all existed tables  of database. Hold this list of tables in $result variable. Now start a while loop to store these tables into an array $tables that has defined previously.

After storing all tables ino an array $tables pass it into a foreach loop and fetch all stored data of tables with following command .

mysql_num_fields() is used to get the total number of fields into a result resource.

The above code will insert a DROP QUERY before the table into backup data which is use to drop the table if it is already exist when you import the database. All data of database will be stored into $return .

fopen() function with w+ mode is use to open a file to read and write.
fwrite() function is use to write an open file. It accept file name as  first parameter and the value to the write as second parameter.

Google place  autocomplete api

Finally after writing the data into file you will  redirect to download.

Thanks to read this post and if you feel any problem, guys can leave it on comment. I will try to solve it.