Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the two kinds of database backups in the database?

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)05/31 Report--

I would like to share with you which two kinds of database backups are in the database. I believe most people don't know much about it, so share this article for your reference. I hope you will gain a lot after reading this article. Let's take a look at it!

Two methods of database backup are: 1, using mysqldump combined with exec function for database backup; 2, using [php+mysql+header] function for database backup.

Database backup is necessary generally use mysqldump for backup, I have written here two backup methods for reference.

The first is to use mysqldump combined with exec function for database backup operation.

The code is as follows

/ * Subject: php-mysql implements database backup. * User: luokakale * Date: 2018-11-9 * Time: 13:31 * / header ('Content-Type:text/html;charset=utf8'); ini_set ("max_execution_time", "0"); / / No limit on the running time of the code to prevent backup failure ini_set (' memory_limit', '128M'); / / set memory to modify date_default_timezone_set ("PRC") as needed / / create a folder where sql files need to be saved $path ='D:\ SQL\ databse_backup';// define database configuration $user =''; / / database account $pwd =''; / / database password $dbname =''; / / database name / / backup database command address file $sqladdress ='D:\ phpStudy\ MySQL\ bin\ mysqldump.exe';// backup specified address $time = time () $path ='D:\ SQL\ databse_backup'.'\\ '.date ("Ymd", $time); if (! file_exists ($path)) {mkdir ($path,0777,true);} / / backup database file name $sqlFile = $dbname. "_% date:~0,4%%date:~5,2%%date:~8,2%%time:~0,2%.sql"; / / determine whether there is a password $password = $pwd=='?':'- p'.$pwd / / splice backup command $order = $sqladdress.'-- opt-upright .roomuser.roompassword.' .$ dbname.' >'.$ path.'\\'.$ sqlFile;// executes the command exec ($order)

I use the mysqldump.exe under the mysql in the integrated phpstudy to back up. The backup database name is written as the database name + year, month, day and hour. I judged the database password in the above code, and some databases on my side do not need a password. Finally, use exec to execute the command.

The second is to use php+mysql+header function for database backup and download operation.

The code is as follows:

Header ('Content-Type:text/html;charset=utf8'); ini_set ("max_execution_time", "0"); / / Code runtime unlimited to prevent backup failure ini_set (' memory_limit', '1024M'); / / set memory to modify date_default_timezone_set ("PRC"); header ("Content-Type:text/html;charset=utf-8"); $host= "; $user="; / / account $password= "" / / password $dbname= ""; / / Database name $con = mysqli_connect ("$host", "$user", "$password", "$dbname"); mysqli_select_db ($con,$dbname); $mysql= "set charset utf8;\ r\ n"; # for mysql > = 5.0mysqli_query ($con, "SET NAMES 'UTF8'"); $q1=mysqli_query ($con, "show tables"); while ($t=mysqli_fetch_array ($Q1)) {$table=$t [0]; $q2=mysqli_query ($con, "show create table `$table`") $sql=mysqli_fetch_array ($Q2); $mysql.=$sql ['Create Table']. ";\ r\ n\ r\ n"; # DDL $q3=mysqli_query ($con, "select * from `$table`"); while ($data=mysqli_fetch_assoc ($Q3)) {$keys=array_keys ($data); $keys=array_map (' addslashes',$keys); $keys=join ('`, `', $keys); $keys= "`". $keys. $vals=array_values ($data); $vals=array_map ('addslashes',$vals); $vals=join ("','", $vals); $vals= "'". $vals. "'"; $mysql.= "insert into `$ table` ($keys) values ($vals);\ r\ n"; unset ($data);} $mysql.= "\ r\ n"; unset ($t);} mysqli_close ($con); $filename=date ('Ymj'). ".sql" / / date with the file name of the day $time = time (); $path ='D:\ SQL\ databse_backup'.'\\ '.date ("Ymd", $time).'\\'; / / check whether the directory exists if (! is_dir ($path)) {/ / New directory mkdir ($path, 0777, true);} $file_name = $path.$filename;$fp = fopen ($file_name,'w'); fputs ($fp,$mysql); fclose ($fp) $fp=fopen ($file_name, "r"); $file_size=filesize ($file_name); header ("Content-type: application/octet-stream"); header ("Accept-Ranges: bytes"); header ("Accept-Length:". $file_size); header ("Content-Disposition: attachment; filename=". $filename); / / echo must be used for output here, otherwise the downloaded writer is blank echo fread ($fp,$file_size); fclose ($fp); exit

Personally, it is recommended to use the first one and the second one is too memory-consuming.

The first can be made into scheduled backups, and scheduled tasks can be used in windows.

The above is all the contents of the article "which two kinds of database backups are in the database". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report