標籤:php shell linux Database Backup crontab
引言
大項目中的Database Backup是必不可少的,否則一旦出現大問題就GG了,雖然本文是講述利用PHP實現資料庫定時備份,但是這並不是好的方案
想要定時備份資料庫,最快捷安全的方法就是利用shell指令碼了,功能強大操作方便,而且執行速度極快,不像PHP還需要被apache解析一把。。
當然,不管是用php定時備份,還是shell指令碼定時備份,都離不開crontab這玩意,畢竟它才是真正的定時器,這裡粗略的說一下crontab吧
crontab使用簡介
crontab常用基礎命令
- crontab -e //編輯定時任務,預設以VI開啟
- crontab -l //列出當前的定時任務
- crontab -r //刪除定時任務
任務格式 :
* * * * * program 分 時 日 月 星期 命令
crontab這裡就不多說了,有興趣的可以搜查相關的資料。
當然 ,還有mysqlDatabase Backup的命令
- mysqldump -h host -u user_name -ppassword database_name > filename 備份全資料
- mysqldump -h host -u user_name -ppassword database_name –no-data > filename 只備份表結構
- mysqldump -h host -u user_name -ppassword database_name –no-create-info > filename 只備份資料
註:-ppassword中間是沒有空格的
這裡都是些簡介,不夠詳細,但是足夠使用了,接下來看代碼:
相關代碼
DB_config.php
<?php return array( ‘db_host‘ => ‘127.0.0.1‘, //主機地址 ‘db_name‘ => ‘root‘, //資料庫名 ‘db_user‘ => ‘root‘, //username ‘db_pwd‘ => ‘root‘, //password ‘db_back_path‘ => ‘/home/db_back‘, //備份目錄,建議使用絕對路徑 ‘back_file_suffix‘ => ‘aaa‘, //備份檔案的尾碼名 //type = 1 back all 備份資料和表結構 //type = 2 just back table structure 只備份表結構 //type = 3 just back table data 只備份表資料 ‘back_type‘ => 1) ?>
DB_config.php
<?php class DB_back { //這裡是預設配置 private $db_config = array( ‘db_host‘ => ‘127.0.0.1‘, ‘db_name‘ => ‘ftdtian‘, ‘db_user‘ => ‘root‘, ‘db_pwd‘ => ‘123‘, ‘db_back_path‘ => ‘/home/db_back‘, ‘back_file_suffix‘ => ‘bak‘, //type = 1 back all //type = 2 just back table structure //type = 3 just back table data ‘back_type‘ => 3 ); //備份檔案名 private $file_name; //shell命令 private $back_shell; public function __construct() { //無限制指令碼時間 set_time_limit(0); //設定時區 date_default_timezone_set(‘PRC‘); //merge設定檔 $this->db_config = array_merge($this->db_config,require "DB_config.php"); $this->set_config(); } //配置config private function set_config() { //目前時間 $date_format = date("Y-m-d-H:i:s",time()); $common_shell = "mysqldump -h %s -u %s -p%s %s "; //預設備份檔案名(備份表結構和資料) $file_format = $date_format.‘_all.‘.$this->db_config[‘back_file_suffix‘]; //預設備份全部的shell $this->back_shell = $common_shell.‘ > %s‘; switch ($this->db_config[‘back_type‘]) { case ‘1‘: break; case ‘2‘: //只備份表結構 $file_format = $date_format.‘_table_structure.‘.$this->db_config[‘back_file_suffix‘]; $this->back_shell = $common_shell.‘ --no-data > %s‘; break; //只備份表資料 case ‘3‘: $file_format = $date_format.‘_table_data.‘.$this->db_config[‘back_file_suffix‘]; $this->back_shell = $common_shell.‘ --no-create-info > %s‘; default: break; } $this->db_config[‘db_back_path‘] = $this->db_config[‘db_back_path‘].DIRECTORY_SEPARATOR.date("Y-m-d",time()); //建立檔案夾 $this->make_dir($this->db_config[‘db_back_path‘]); //構建檔案全路徑 $this->file_name = $this->db_config[‘db_back_path‘].DIRECTORY_SEPARATOR.$file_format; } //建立檔案夾 private function make_dir($path , $mode = 0755, $recursive = true) { if(!is_dir($path)) { try { mkdir($path,$mode,$recursive); chmod($path,$mode); } catch(Exception $e) { //這裡面可以寫log,不再多寫 echo $e->getMessage(); } } return true; } //開始備份 public function start_back() { //字串格式名產生shell命令 $shell = sprintf($this->back_shell,$this->db_config[‘db_host‘],$this->db_config[‘db_user‘],$this->db_config[‘db_pwd‘],$this->db_config[‘db_name‘],$this->file_name); try { //執行shell shell_exec($shell); }catch (Exception $e) { echo $e->getMessage(); } }}$obj = new DB_back();$obj->start_back(); ?>
將這兩個PHP檔案放到同一目錄中,我們假設放在/var/www/html/back/下
crontab -e//每天淩晨兩點半執行備份30 2 * * * /usr/bin/php /var/www/html/back/DB_back.php
整個流程就是這樣,如果需要修改相對應的配置,請直接修改DB_config.php設定檔
備忘:
- 在使用php的mkdir時,需要確定目前的目錄的父目錄是否具有相應的寫入權限,如果沒有,請先進入終端進行chmod父目錄,否則不會順利建立目錄
代碼寫的並不好,如有Bug或者建議,感謝指正
著作權聲明:本文為博主原創文章,未經博主允許不得轉載。
Linux下crontab+php實現Mysql資料庫定時備份