MySQL backup script

Source: Internet
Author: User
Tags getmessage sprintf ziparchive mysql backup

mysqlbackup.php:

<?PHP//backup MySQL        Set_time_limit(0); Date_default_timezone_set (' PRC '); //Configuration    $configs=Array(        ' Host1 ' =Array(            ' localhost ', ' root ', ' password ',Array(),//back up all databases as empty, or back up those databases' D:/xampp/mysql/bin/mysqldump ',//Backup Tools            dirname(__file__)." /localhost ",//Directory plus host name5,//Delete SQL files for the first 5 days),); foreach($configs  as $config) {Backup ($config); }    functionBackup$config) {        List($host,$username,$password,$databases,$backuptool,$backupdir,$day) =$config; $command= "$backuptool-U$username-H$host-P$password%s >%s "; $logsfile=$backupdir.‘ /‘.Date(' Ymd '). Log; $backfilename=$backupdir.‘ /‘.Date(' YMD '). " %s.sql ";//backing up the SQL file, naming the database                if(!Is_dir($backupdir)) {            mkdir($backupdir, 0755,true); }                //Delete backup files for the first 10 daysGet_dir_files ($backupdir,$returnval); if($returnval) {            foreach($returnval  as $v) {                $time=Filemtime($v); if($time<Strtotime("-$dayDay ") && (PathInfo($v, pathinfo_extension)) = = ' Zip ') {                    unlink($v); }            }        }                if(!$databases) {            $databases= Getdatabases ($host,$username,$password); }        //Start Backup        foreach($databases  as $database) {            $outputfile=sprintf($backfilename,$database); $execcommand=sprintf($command,$database,$outputfile); Try {                if(system($execcommand) ===false) {                    Throw New Exception(' Execute backup command error! ')); }                if(file_exists($outputfile)) {                    $zip=Newziparchive (); $filename=PathInfo($outputfile,pathinfo_filename); $zipname=$filename.‘. Zip; if($zip->open ($zipname, ziparchive::overwrite) = = =true) {                        $zip->addfile ($outputfile,$filename.‘.‘.PathInfo($outputfile,pathinfo_extension)); $zip-Close (); }Else {                        Throw New Exception(' Ziparchive open error! '); }                    $resource=file_get_contents($zipname); file_put_contents($backupdir.‘ /‘.$zipname,$resource); unlink($zipname); unlink($outputfile); $message=Date(' y-m-d h:i:s '). "$databaseBackup complete!\r\n "; Logs ($logsfile,$message); }            }Catch(Exception $e) {                $message=Date(' y-m-d h:i:s ').$e->getline (). ' ‘.$e->getmessage (). " \ r \ n "; Logs ($logsfile,$message); }        }    }    functionGetdatabases ($host,$username,$password) {        $databases=Array(); Try {                $mysqli=NewMysqli ($host,$username,$password); $result=$mysqli->query ("Show Databases"); if($result) {                     while($row=$result-Fetch_row ()) {                        ( Current($row)! = ' Information_schema ' && Current($row)! = ' MySQL ' && Current($row)! = ' Performance_schema ') &&$databases[] = Current($row); }                    $result-Free_result (); }                $mysqli-Close (); }Catch(Exception $e) {                $message=Date(' y-m-d h:i:s ').$e->getline (). ' ‘.$e->getmessage (). " \ r \ n "; Logs ($logsfile,$message); }        return $databases; }        functionLogs$file,$contents) {        $dirname=dirname($file); Try {            if(!Is_dir(dirname($file)) || !is_writeable(dirname($file))) {                Throw New Exception("File is not direcotory or file can ' t write"); }            file_put_contents($file,$contents,file_append); }Catch(Exception $e) {            $message=Date(' y-m-d h:i:s ').$e->getline. ' ‘.$e->getmessage (). " \ r \ n "; Logs ($logfile,$message); }    }            //get the file under the current directory (without subfolders)    functionGet_dir_files ($currPath, &$returnVal=Array()) {        if(Is_dir($currPath)) {            $currPath= (substr($currPath, -1,1) = = '/')?substr($currPath, 0,strlen($currPath)-1):$currPath; if($handler=Opendir($currPath)) {                 while(($fileName=Readdir($handler)) !==false) {                    if($fileName! = '. ' &&$fileName! = '. ' &&$fileName[0]! = '. ') {                        if(Is_file($currPath. ‘/‘ .$fileName)) {                            $returnVal[] =$currPath. ‘/‘ .$fileName; }                    }                }                Closedir($handler); }        }    }?>

Mysqlbackup.bat:

D:\xampp\php\php.exe-q D:\wamp\www\php_lib\basic\mysqlbackup.php
Pause

Linux system shell backup MySQL:

#!/bin/SH# sed-I.'s/^m//g'/home/taskschd/backup.SH#注意:The input method of the ^m is CTRL + V, then CTRL +MDBs=(test) Root_dir=/home/backup/ fordbnameinch${dbs[@]} Do#备份数据 Backup_dir= $ROOT _dir$dbname'_'$(Date+%y%m%d). SQL/usr/local/mysql/bin/mysqldump--OPT-UROOT-PABC $dbname >$BACKUP _dir #删除三天前数据 delete_file= $dbname'_'$(Date-D"-5 Day" "+%y%m%d")'. SQL'    RM$ROOT _dir$delete_file Done

Another shell backup MySQL script: http://www.cnblogs.com/luoyunshu/p/3435378.html

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.