Scheduled backup of the server database (with Windows Task Scheduler and Mysqldump)

Source: Internet
Author: User

More community recently, considering the security of data, to backup the database once a day, just in case;

Linux is not very well known, first use the windows of the scheduled task it;

The general idea is to borrow a scheduled task from Windows to perform a backup of the remote database locally;

Mostly moved. HA: http://www.php-note.com/article/detail/388

First, PHP program: Borrow mysqldump to export the database, and then PHP operation to store and delete the old data;

Profile: db.config.php (I am a backup of five databases, increase the reduction on the $dbs array can be modified)

<?PHP/** * Database configuration file*/$dbs=Array(    Array(        ' Host ' = ' 127.0.1.1 ', ' port ' = 3306, ' user ' = ' root ', ' pass ' = ' root ', ' name ' = ' Qixiang ', ' charset ' = ' UTF8 '    ),Array(        ' Host ' = ' 127.0.1.1 ', ' port ' = 3306, ' user ' = ' root ', ' pass ' = ' root ', ' name ' = ' Liushen ', ' charset ' = ' UTF8 '    ),Array(        ' Host ' = ' 127.0.1.1 ', ' port ' = 3306, ' user ' = ' root ', ' pass ' = ' root ', ' name ' = ' newcommunity ', ' charset ' = ' UTF8 '    ),Array(        ' Host ' = ' 127.0.1.1 ', ' port ' = 3306, ' user ' = ' root ', ' pass ' = ' root ', ' name ' = ' Userclub ', ' charset ' = ' UTF8 '    ),Array(        ' Host ' = ' 127.0.1.1 ', ' port ' = 3306, ' user ' = ' root ', ' pass ' = ' root ', ' name ' = ' CKD ', ' charset ' = ' UTF8 '    ),);$backupPath= ' d:/hcr/'.Date(' Ym '). ‘/‘ .Date(' d ');$oldPath= ' d:/hcr/'. (intval(Date(' Ym ')) -1);//Note: If there are spaces in the path, you need to enclose them in double quotation marks$cmd= ' "D:/program Files (x86)/wamp/bin/mysql/mysql5.6.17/bin/mysqldump" ';//Export All$cmd. = "-h%s-p%s-u%s-p%s%s--default_character-set=%s >%s";//Export only table structure//$cmd. = "-h%s-p%s-u%s-p%s%s--no-data--default_character-set=%s >%s";//Export only data//$cmd. = "-h%s-p% s-u%s-p%s%s--no-create-info--default_character-set=%s >%s ";

Program Files: backup.php

<?PHP/** * Backup DATABASE*/Header("content-type:text/html; Charset=utf-8 ");d Ate_default_timezone_set (' PRC ');Set_time_limit(0);Define(' Root_path ',dirname(__file__));if([email protected]includeRoot_path. '/db.config.php '){    Echo' Config error '; Write (' Config error '); Exit(1);}//Create a new backup directoryNew_mkdir ($backupPath);//Delete data from one months agoNew_rmdir ($oldPath);//Batch Backup Databaseforeach($dbs  as $db){    $cmd 2= ' '; if(isset($db[' Name ']))    {        $fileName=$backupPath. ‘/‘ .$db[' Name ']. ‘_‘ .Date(' Ymd_his '). '. SQL '; $cmd 2=sprintf($cmd,$db[' Host '],$db[' Port '],$db[' User '],$db[' Pass '],$db[' Name '],$db[' CharSet '],$fileName); Write ($cmd 2); Try        {            exec($cmd 2); }        Catch(Exception $e)        {            //Write error log (omitted)            Echo $e-GetMessage (); }    }}/** * Create Folder * * @param string $path folder path * @param int $mode Access * @param boolean $recursive whether to create recursively*/functionNew_mkdir ($path= ",$mode= 0777,$recursive=true){    Clearstatcache(); if(!Is_dir($path))    {        mkdir($path,$mode,$recursive); chmod($path,$mode); $ret= @Touch($path. '/index.html '); @chmod($path. '/index.html ', 0777); return $ret; }    return true;}/** * Empty/delete folder * * @param string $dirname folder path * @param bool $self Delete current folder * @return bool*/functionNew_rmdir ($dirname= ",$self=true){    if(!file_exists($dirname))    {        return false; }    if(Is_file($dirname) ||Is_link($dirname))    {        return unlink($dirname); }    $dir=dir($dirname); if($dir)    {         while(false!==$entry=$dir-Read ()) {            if($entry= = '. ' | |$entry= = ' ... ')            {                Continue; } new_rmdir ($dirname. ‘/‘ .$entry); }    }    $dir-Close (); $self&&rmdir($dirname);}/** * Write file * * @param string $filename file name * @param string $text text string to write * @param string $openmod text Write mode (' W ': Overwrite override, ' a ': Text append) * @return Boolean*/functionWrite_file ($filename= ",$text= ",$openmod= ' W '){    if(@$fp=fopen($filename,$openmod))    {        Flock($fp, 2); fwrite($fp,$text); fclose($fp); return true; }    Else    {        return false; }}/** * Write objects (including numbers, strings, arrays) * * @param string $text text string to write * @param string $type text Write type (' W ': Overwrite override, ' A ': text append)*/functionWrite$text= ",$type= ' A '){    if(!Is_dir(' D:/HCR '))    {        return false; }    $filename= ' D:/hcr/write.txt '; $text= "\r\n++++++++++++++++++++++++++++++++++++++++++\r\n".Date(' y-m-d h:i:s '). "\ r \ n".Print_r($text,true); Write_file ($filename,$text,$type);}

Specific to see: http://www.php-note.com/article/detail/85

The test steps are as follows:

1. All Programs > Accessories > System Tools > Task Scheduler > ' Create Task ' > Task name in the top right corner

2. Create a trigger:

3. Create a new operation:

Complete the scheduled task (I set the daily backup at noon);

The final effect is as follows:

Scheduled backup of the server database (with Windows Task Scheduler and Mysqldump)

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.