MySQL backup script-mysql tutorial

Source: Internet
Author: User
MySQL backup script mysqlbackup. php:

 Array ('localhost', 'root', 'password', array (), // empty to back up all databases; otherwise, back up these databases 'd: /xampp/mysql/bin/mysqldump, // backup tool dirname (_ FILE __). "/localhost", // add host name 5 to the Directory, // delete the SQL file from the previous 5 days),); foreach ($ configs as $ config) {backup ($ config);} function backup ($ config) {list ($ host, $ username, $ password, $ databases, $ backuptool, $ backupdir, $ day) = $ config; $ command = "$ backuptool-u $ username-h $ host-p $ password % s> % s"; $ logsfile = $ B Ackupdir. '/'. date ('ymmd '). '. log'; $ backfilename = $ backupdir. '/'. date ('ymmd '). "% s. SQL "; // The backup SQL file named if (! Is_dir ($ backupdir) {mkdir ($ backupdir, 0755, true);} // delete the backup file get_dir_files ($ backupdir, $ returnval) in the last 10 days; if ($ returnval) {foreach ($ returnval as $ v) {$ time = filemtime ($ v); if ($ time <strtotime ("-$ day ") & (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 = new ZipArchive (); $ filename = pathinfo ($ outputfile, PATHINFO_FILENAME); $ zipname = your 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 '). "$ database backup 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) ;}} function getdatabases ($ host, $ username, $ password) {$ databases = array (); try {$ mysqli = new Mysqli ($ host, $ username, $ password); $ result = $ mysqli-> query ("show databases"); if ($ result) {while ($ row = $ result-> fetch_row () {(current ($ row )! = 'Information _ scheme' & current ($ row )! = 'Mysql' & current ($ row )! = 'Performance _ scheme') & $ 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;} function logs ($ file, $ contents) {$ dirname = dirname ($ file); try {if (! Is_dir (dirname ($ file) |! Is_writeable (dirname ($ file) {throw new Exception ("file is not direcoprocessor 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 files in the current directory (excluding subfolders) function get_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. phppause;

Linux shell backup MySQL:

#! /Bin/sh # sed-I's/^ M // g'/home/taskschd/backup. sh # Note: ^ The Input Method of M is Ctrl + v, and then Ctrl + Mdbs = (test) ROOT_DIR =/home/backup/for dbname in $ {dbs [@]} do # backup data BACKUP_DIR = $ ROOT_DIR $ dbname '_' $ (date + % Y % m % d ). SQL/usr/local/mysql/bin/mysqldump -- opt-uroot-pabc $ dbname> $ BACKUP_DIR # delete data three days ago delete_file = $ dbname '_' $ (date-d "-5 day" "+ % Y % m % d ") '. SQL 'rm $ ROOT_DIR $ delete_file done

A 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.