MySQL backup script, mysql script _ PHP Tutorial

Source: Internet
Author: User
Tags ziparchive mysql automatic backup mysql backup
MySQL backup script and mysql script. MySQL backup script, mysql script mysqlbackup. php :? Php backup mysqlset_time_limit (0); date_default_timezone_set (PRC); configuration $ configsarray (host1array (localh MySQL backup script, mysql script

Mysqlbackup. php:

 Array ('localhost', 'root', 'root', array (), // empty backup 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) {$ logsfile = $ config [5]. '/'. date ('ymmd '). '. log'; logs ($ logsfile, $ config [0]. "backup \ n"); backup ($ config);} function backup ($ config) {list ($ host, $ username, $ password, $ databases, $ backuptool, $ backupd Ir, $ day) = $ config; $ command = "$ backuptool-u $ username-h $ host-p $ password % s> % s"; $ logsfile = $ backupdir. '/'. 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 the file is too large, the compression will fail (the SQL file tested is 4.62 GB, and the compression fails. the compressed file is not created. Test 2.81G can) if (file_exists ($ outputfile) {$ zip = new ZipArchive (); $ filename = pathinfo ($ outputfile, PATHINFO_FILENAME); $ zipname = $ backupdir. '/'.w.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! ') ;}} If (! File_exists ($ zipname) | (filesize ($ zipname) = 0) {throw new Exception ('ziparchive create error! ');} $ 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 ();} else {throw new Exception ('No databases! ') ;}} 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 ($ c UrrPath,-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 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


How does one back up mySQL databases in linux?

Method 1: suitable for mysql databases in all formats. it is regularly executed by exporting and importing a script to the database:
1. export the entire database mysqldump-u user name-p database name> exported file name mysqldump-u wcnc-p smgp_1__wcnc>/storage path/wcnc. SQL
2. export a table mysqldump-u user name-p database name table name> exported file name mysqldump-u wcnc-p smgp_rj_wcnc users>/storage path/wcnc_users. SQL
3. export a database structure mysqldump-u wcnc-p-d -- add-drop-table smgp_rj_wcnc>/storage path/wcnc_db. SQL
Definition:
-D no data
-- Add-drop-table: add a drop table before each create statement.
4. run the source command to import the database to the mysql database console:
For example, mysql-u root-p mysql> use database

Method 2: for mysql data tables in the format of MyISAM
Assume that the data file is in/var/lib/mysql
Write a script directly.
Cp-r/var/lib/mysql/directory to which the backup is saved

Remote Backup uses rsync increments or regular full backup.

Who can give me a script for MYSQL automatic backup?

Maybe you won't use it.

You can put this script into crontab and execute it once every morning to automatically back up data.

This script can be executed only once a day, and only the backups of the last five days are retained on the server.

Code:

#! /Bin/bash
# This is a ShellScript For Auto DB Backup
# Powered by aspbiz
#2004-09

# Setting
# Set the database name, database login name, password, backup path, log path, data file location, and backup mode
# By default, the backup method is tar, mysqldump, or mysqldotcopy.
# By default, log on to the mysql database with root (empty) and back up to/root/dbxxxxx. tgz
DBName = mysql
DBUser = root
DBPasswd =
BackupPath =/root/
LogFile =/root/db. log
DBPath =/var/lib/mysql/
# BackupMethod = mysqldump
# BackupMethod = mysqlhotcopy
# BackupMethod = tar
# Setting End

NewFile = "$ BackupPath" db $ (date + % y % m % d). tgz
DumpFile = "$ BackupPath" db $ (date + % y % m % d)
OldFile = "$ BackupPath" db $ (date + % y % m % d -- date = '5 days ago '). tgz

Echo "-----------------------------------------"> $ LogFile
Echo $ (date + "% y-% m-% d % H: % M: % S") >>$ LogFile
Echo "--------------------------"> $ LogFile
# Delete Old File
If [-f $ OldFile]
Then
Rm-f $ OldFile >>$ LogFile 2> & 1
Echo "[$ OldFile] Delete Old File Success! ">>> $ LogFile
Else
Echo "[$ OldFile] No Old Backup File! ">>> $ LogFile
Fi

If [-f $ NewFile]
Then
Echo "[$ NewFile] The Backup File is exists, Can't Backup! ">>> $ LogFile
Else
Case $ BackupMethod in
Mysqldump)
If [-z $ DBPasswd]
Then
Mysqldump-u $ DBUser -- opt $ DBName> $ DumpFile
Else
Mysqldump-u $ DBUser-p $ DBPasswd -- opt $ DBName> $ DumpFile
F... the remaining full text>
 

Using mysqlbackup. php :? Php // Back Up mysql set_time_limit (0); date_default_timezone_set ('prc'); // Configure $ configs = array ('host1' = array ('localh...

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.