Automatically back up your website data

Source: Internet
Author: User

A problem occurs on the front-end time host provider server. The depressing thing is that the data is restored to a week ago, leading to the loss of some blog data.

After a headache, I came up with a method to automatically back up website data.

1. convert data to SQL on the server
To convert data on the server to an SQL script, you need to put a PHP file on the server. The php file is used to connect to the database, read the data, and convert it to SQL. For example, we put a tosql. php file in the htdocs directory of the server. The content is as follows:

<? PHP  Error_reporting ( E_all &~ E_notice  );  Include_once 'Application/configs/dbconfig. php' ;  Include_once 'Application/DES/DB. Class. php' ; Header ('Expires: Mon, 26 Jul 1997 05:00:00 gmt' );  Header ('Last-modified :'. Gmdate ('D, D m y h: I: s'). 'gmt' );  Header ('Cache-control: No-store, no-cache, must-revalidate' );  Header ('Cache-control: Post-check = 0, pre-check = 0 ', False  );  Header ('Pragma: No-cache' ); Header ("Content-Type: text/plain; charset = UTF-8" );  $ DB = New  DB ();  $ DB -> Connect ( $ Config ['Db'] ['host'], $ Config ['Db'] ['user'], $ Config ['Db'] ['pass' ]);  $ DB -> Select_db ( $ Config ['Db'] ['name' ]); $ DB -> Query ('set names utf8' );  $ Sqldump ='' ;  //  My table names start with TBS _  $ SQL = "Show table status where name like 'tbs _ % '" ;  $ Query = $ DB -> Query ( $ SQL  );  While ( $ Table =$ DB -> Fetch_array ( $ Query  )){  $ Sqldump . = SQL _dumptable ( $ Table ['Name' ]);}  Echo   $ Sqldump  ;  Function SQL _dumptable ( $ Table  ){  Global   $ DB  ; $ Tabledump = "Drop table if exists $ Table ; \ N" ;  $ Createtable = $ DB -> Query ("show CREATE TABLE $ Table " );  $ Create = $ DB -> Fetch_array ( $ Createtable  );  $ Tabledump . = $ Create [1]. "; \ n";  $ Rows = $ DB -> Query ("select * from $ Table " );  $ Numfields = $ DB -> Num_fields ( $ Rows  );  $ Numrows = $ DB -> Num_rows ( $ Rows  );  While ($ Row = $ DB -> Fetch_array ( $ Rows  )){  $ Comma = "" ;  $ Tabledump . = "Insert $ Table Values (" ;  For ( $ I = 0; $ I < $ Numfields ; $ I ++ ){  $ Tabledump . = $ Comma ."'". Mysql_escape_string ( $ Row [ $ I ]). "'" ;  $ Comma = "," ;}  $ Tabledump . = "); \ N" ;}  $ Tabledump . = "\ N" ;  Return  $ Tabledump  ;} 

 

2. Add a PHP file locally to obtain the SQL basic from the server and then write it to the local file. The PHP file named nextdata. php contains the following content:

 
<?PHP$ Filename=Dirname(_ File __). '/Data/tbs _'.Date('Ymdhis ',Mktime() + 3600*8). '. SQL';$ Sqldump=File_get_contents('HTTP: // www.nextphp.com/tosql.php ');
File_put_contents ($ filename, $ sqldump );

 

3. implement regular backup
A local Windows operating system is used. You can use scheduled tasks to regularly execute backup tasks. This requires a batch file. The batch processing file name is called nextdata. bat, as shown in the following figure:

 
D: \ Wamp \ bin \ PHP \ php5.3.5 \ PHP nextdata. php

 

After the above three steps, our scheduled task will regularly execute the tosql. php file on the server, and then write the content to the local machine, thus implementing the Database Backup Task.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.