這篇文章主要介紹“mysql怎么進(jìn)行備份”,在日常操作中,相信很多人在mysql怎么進(jìn)行備份問(wèn)題上存在疑惑,小編查閱了各式資料,整理出簡(jiǎn)單好用的操作方法,希望對(duì)大家解答”mysql怎么進(jìn)行備份”的疑惑有所幫助!接下來(lái),請(qǐng)跟著小編一起來(lái)學(xué)習(xí)吧!
創(chuàng)新互聯(lián)建站堅(jiān)持“要么做到,要么別承諾”的工作理念,服務(wù)領(lǐng)域包括:
成都做網(wǎng)站、成都網(wǎng)站制作、企業(yè)官網(wǎng)、英文網(wǎng)站、手機(jī)端網(wǎng)站、網(wǎng)站推廣等服務(wù),滿足客戶于互聯(lián)網(wǎng)時(shí)代的
嘉禾網(wǎng)站設(shè)計(jì)、移動(dòng)媒體設(shè)計(jì)的需求,幫助企業(yè)找到有效的互聯(lián)網(wǎng)解決方案。努力成為您成熟可靠的網(wǎng)絡(luò)建設(shè)合作伙伴!
MySQL 5.5.47
備份路徑 /backup/
安裝
apt-get install percona-xtrabackup11
一、 全量備份
root@iZu1dc59z8tZ:/var/lib# innobackupex --defaults-file=/etc/mysql/my.cnf --user=root --password=123123 /backup/ InnoDB Backup Utility v1.5.1-xtrabackup; Copyright 2003, 2009 Innobase Oy
and Percona LLC and/or its affiliates 2009-2013. All Rights Reserved.
This software is published under
the GNU GENERAL PUBLIC LICENSE Version 2, June 1991.
Get the latest version of Percona XtraBackup, documentation, and help resources:
http://www.percona.com/xb/ubuntu
160217 11:17:53 innobackupex: Connecting to MySQL server with DSN 'dbi:mysql:;mysql_read_default_file=/etc/mysql/my.cnf;mysql_read_default_group=xtrabackup' as 'root' (using password: YES).
160217 11:17:53 innobackupex: Connected to MySQL server
160217 11:17:53 innobackupex: Executing a version check against the server...
160217 11:17:53 innobackupex: Done.
IMPORTANT: Please check that the backup run completes successfully.
At the end of a successful backup run innobackupex
prints "completed OK!".
innobackupex: Using mysql server version 5.5.47-0ubuntu0.14.04.1innobackupex: Created backup directory /backup/2016-02-17_11-17-53160217 11:17:53 innobackupex: Starting ibbackup with command: xtrabackup_55 --defaults-file="/etc/mysql/my.cnf" --defaults-group="mysqld" --backup --suspend-at-end --target-dir=/backup/2016-02-17_11-17-53 --tmpdir=/tmp
innobackupex: Waiting for ibbackup (pid=30486) to suspend
innobackupex: Suspend file '/backup/2016-02-17_11-17-53/xtrabackup_suspended_2'xtrabackup_55 version 2.1.8 for Percona Server 5.5.35 Linux (x86_64) (revision id: undefined)
xtrabackup: uses posix_fadvise().
xtrabackup: cd to /var/lib/mysql
xtrabackup: using the following InnoDB configuration:
xtrabackup: innodb_data_home_dir = ./
xtrabackup: innodb_data_file_path = ibdata1:10M:autoextend
xtrabackup: innodb_log_group_home_dir = ./
xtrabackup: innodb_log_files_in_group = 2xtrabackup: innodb_log_file_size = 5242880>> log scanned up to (3233814)
[01] Copying ./ibdata1 to /backup/2016-02-17_11-17-53/ibdata1
[01] ...done
>> log scanned up to (3233814)
xtrabackup: Creating suspend file '/backup/2016-02-17_11-17-53/xtrabackup_suspended_2' with pid '30487'160217 11:17:55 innobackupex: Continuing after ibbackup has suspended160217 11:17:55 innobackupex: Starting to lock all tables...160217 11:17:55 innobackupex: All tables locked and flushed to disk160217 11:17:55 innobackupex: Starting to backup non-InnoDB tables and files
innobackupex: in subdirectories of '/var/lib/mysql'innobackupex: Backing up files '/var/lib/mysql/phpmyadmin/*.{frm,isl,MYD,MYI,MAD,MAI,MRG,TRG,TRN,ARM,ARZ,CSM,CSV,opt,par}' (37 files)
>> log scanned up to (3233814)
innobackupex: Backing up files '/var/lib/mysql/mysql/*.{frm,isl,MYD,MYI,MAD,MAI,MRG,TRG,TRN,ARM,ARZ,CSM,CSV,opt,par}' (72 files)
innobackupex: Backing up files '/var/lib/mysql/performance_schema/*.{frm,isl,MYD,MYI,MAD,MAI,MRG,TRG,TRN,ARM,ARZ,CSM,CSV,opt,par}' (18 files)
innobackupex: Backing up files '/var/lib/mysql/blog/*.{frm,isl,MYD,MYI,MAD,MAI,MRG,TRG,TRN,ARM,ARZ,CSM,CSV,opt,par}' (29 files)160217 11:17:56 innobackupex: Finished backing up non-InnoDB tables and files160217 11:17:56 innobackupex: Waiting for log copying to finish
xtrabackup: The latest check point (for incremental): '3233814'xtrabackup: Stopping log copying thread.
.>> log scanned up to (3233814)
xtrabackup: Creating suspend file '/backup/2016-02-17_11-17-53/xtrabackup_log_copied' with pid '30487'xtrabackup: Transaction log of lsn (3233814) to (3233814) was copied.160217 11:17:57 innobackupex: All tables unlocked
innobackupex: Backup created in directory '/backup/2016-02-17_11-17-53'160217 11:17:57 innobackupex: Connection to database server closed160217 11:17:57 innobackupex: completed OK!12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667681234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
completed OK! 表示備份成功。
還有很多其他常用的參數(shù)。
–stream=tar 壓縮類型, 默認(rèn)是輸出到終端,如果保存為文件
innobackupex --defaults-file=/etc/mysql/my.cnf --user=root --password=*** /backup/ --stream=tar > /backup/`date +"%Y%m%d_%H%M%S"`.tar#or gzip 壓縮innobackupex --defaults-file=/etc/mysql/my.cnf --user=root --password=*** /backup/ --stream=tar | gzip > /backup/`date +"%Y%m%d_%H%M%S"`.tar.gz123123
–parallel=4 –throttle=400 并行個(gè)數(shù),根據(jù)主機(jī)配置選擇合適的,默認(rèn)是1個(gè),多個(gè)可以加快備份速度。
二、 全量恢復(fù)
恢復(fù)備份文件要保證mysql datadir文件為空,否則會(huì)報(bào)錯(cuò)
測(cè)試恢復(fù)
service mysql stop #停止MySQLmv /var/lib/mysql/ /var/lib/mysql_bak #備份原mysql 的datadirmkdir /var/lib/mysql123123
恢復(fù)第一步:應(yīng)用日志
如果加了壓縮,需要解壓
mkdir /backup/20160217_114521
tar -ixvf /backup/20160217_114521.tar.gz -C /backup/20160217_1145211212innobackupex --defaults-file=/etc/mysql/my.cnf --apply-log /backup/20160217_11452111
恢復(fù)第二步:拷貝文件到MySQL datadir
innobackupex --defaults-file=/etc/mysql/my.cnf --copy-back /backup/20160217_11452111
恢復(fù)第三步:修改文件權(quán)限,啟動(dòng)mysql服務(wù)。
chown mysql: /var/lib/mysql -Rservice mysql start1212
三、 增量備份
增量備份需要基于全備,先假設(shè)我們已經(jīng)有了一個(gè)全備(/backup/20160217_114521),在該全表的基礎(chǔ)上做增量備份。
第一次在數(shù)據(jù)庫(kù)插入一條數(shù)據(jù),在全備上做增量備份
innobackupex --defaults-file=/etc/mysql/my.cnf --user=root --password=123123 --incremental-basedir=/backup/20160217_114521 --incremental /backup/11
incremental-basedir=全備的目錄
–incremental=增量備份的目錄
第二次在數(shù)據(jù)庫(kù)插入一條數(shù)據(jù),在第一次增量備份上做增量備份
innobackupex --defaults-file=/etc/mysql/my.cnf --user=root --password=123123 --incremental-basedir=/backup/2016-02-17_12-15-30 --incremental /backup/11incremental-basedir=在第一次增量備份的目錄--incremental=增量備份的目錄1212
備份目錄下,有一個(gè)文件xtrabackup_checkpoints記錄著備份信息,全備的信息如下:
root@iZu1dc59z8tZ:/backup# cat 20160217_114521/xtrabackup_checkpoints backup_type = full-preparedfrom_lsn = 0to_lsn = 3233814last_lsn = 3233814compact = 0
root@iZu1dc59z8tZ:/backup# cat 2016-02-17_12-15-30/xtrabackup_checkpoints backup_type = incrementalfrom_lsn = 3233814to_lsn = 3234430last_lsn = 3234430compact = 0
root@iZu1dc59z8tZ:/backup# cat 2016-02-17_12-1/xtrabackup_checkpoints 2016-02-17_12-15-30/ 2016-02-17_12-17-31/
root@iZu1dc59z8tZ:/backup# cat 2016-02-17_12-17-31/xtrabackup_checkpoints backup_type = incrementalfrom_lsn = 3234430to_lsn = 3234537last_lsn = 3234537compact = 012345678910111213141516171819201234567891011121314151617181920
從上面可以看出,增量備份的from_lsn正好等于全備的to_lsn。
四、 增量恢復(fù)
//測(cè)試恢復(fù)service mysql stop #停止MySQLmv /var/lib/mysql/ /var/lib/mysql_bak #備份原mysql 的datadirmkdir /var/lib/mysql12341234
恢復(fù)第一步:應(yīng)用所有增量日志
1,應(yīng)用全量的日志
innobackupex --defaults-file=/etc/mysql/my.cnf --apply-log --redo-only /backup/20160217_11452111
2,應(yīng)用增量的日志
//第一次增量innobackupex --defaults-file=/etc/mysql/my.cnf --apply-log --redo-only /backup/20160217_114521 --incremental-dir=/backup/2016-02-17_12-15-30/ 1212
……
//最后一次增量innobackupex --defaults-file=/etc/mysql/my.cnf --apply-log /backup/20160217_114521 --incremental-dir=/backup/2016-02-17_12-17-31/1212
恢復(fù)第二步:拷貝文件到MySQL datadir
innobackupex --defaults-file=/etc/mysql/my.cnf --copy-back /backup/20160217_11452111
恢復(fù)第三步:修改文件權(quán)限,啟動(dòng)mysql服務(wù)。
chown mysql: /var/lib/mysql -Rservice mysql start
到此,關(guān)于“mysql怎么進(jìn)行備份”的學(xué)習(xí)就結(jié)束了,希望能夠解決大家的疑惑。理論與實(shí)踐的搭配能更好的幫助大家學(xué)習(xí),快去試試吧!若想繼續(xù)學(xué)習(xí)更多相關(guān)知識(shí),請(qǐng)繼續(xù)關(guān)注創(chuàng)新互聯(lián)網(wǎng)站,小編會(huì)繼續(xù)努力為大家?guī)?lái)更多實(shí)用的文章!
另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)cdcxhl.cn,海內(nèi)外云服務(wù)器15元起步,三天無(wú)理由+7*72小時(shí)售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國(guó)服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡(jiǎn)單易用、服務(wù)可用性高、性價(jià)比高”等特點(diǎn)與優(yōu)勢(shì),專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場(chǎng)景需求。
當(dāng)前題目:mysql怎么進(jìn)行備份-創(chuàng)新互聯(lián)
本文網(wǎng)址:
http://weahome.cn/article/ddjpoi.html