Home Database Mysql Tutorial 讲解Linux系统下如何自动备份MySQL数据的基本教程_MySQL

讲解Linux系统下如何自动备份MySQL数据的基本教程_MySQL

May 27, 2016 pm 01:45 PM
linux mysql backup Tutorial

1.先创建一个数据库备份目录:

mkdir backup
cd backup
mkdir mysqlbackup
cd mysqlbackup
Copy after login

2.创建备份脚本

vi mysqlautobackup
Copy after login

3.编写脚本:

filename=`date +%Y%m%d`
/mysql的bin目录/mysqldump --opt 数据库名 -u(mysql账号) -p(mysql密码) | gzip > /备份到哪个目录/name$filename.gz
Copy after login


说明:以上采用gzip压缩,name可随意写,注意-u与mysql账号之间没有空格也无需括号,-p与mysql密码也是。
4.将脚本设置为可执行权限:

chmod +x autobackup
Copy after login


5.写入计划任务:

crontab -e
01 01 * * * /bin/bash /脚本所在目录/mysqlautobackup
Copy after login


每天凌晨1点零1分执行脚本。
重启计划任务:

/etc/rc.d/init.d/crond restart
Copy after login


至此所有步骤完成。

一些常用技巧
1、创建保存备份文件的路径/mysqldata

#mkdir /mysqldata
Copy after login

2、创建/usr/sbin/baktest文件
输入以下内容:

mysqldump -uroot -proot test | gzip > /mysqldata/test`date +%Y-%m-%d_%H%M%S`.sql.gz
Copy after login

3、修改文件属性,使其可以执行

#chmod +x /usr/sbin/baktest
Copy after login

4、修改/etc/crontab
在下面添加

01 3 * * * root /usr/sbin/baktest
Copy after login

表示每天3点钟01分执行备份

5、重新启动crond

#/etc/rc.d/init.d/crond restart
Copy after login

完成。


PS:
全备份脚本:

#!/bin/bash
# Name:qbk全备份脚本
# mysql qbk scripts
# By zxsdw.com
# Last Modify:2015-01-21
#定义脚本存放路径
#scriptsDir=/usr/sbin
#定义用户名及密码
user=root
userPWD=密码
#定义要备份的数据库
database=数据库
#定义完全备份文件存放路径
bakDir=/backup/mysql
#eMailFile=$bakDir/email.txt
#eMail=admin@zxsdw.com
#定义日志文件
LogFile=$bakDir/mysqlbak.log
DATE=`date +%Y%m%d`
echo " " >> $LogFile
echo " " >> $LogFile
echo "--------------------------" >> $LogFile
echo $(date +"%y-%m-%d %H:%M:%S") >>$LogFile
echo "-----------------" >> $LogFile
cd $bakDir
DumpFile=$DATE.sql.gz
mysqldump --flush-logs -u$user -p$userPWD --quick $database| gzip >$DumpFile
echo "Dump Done" >> $LogFile
echo "[$DumpFile]Backup Success!" >> $LogFile
daily_databakDir=$bakDir/daily_backup
cd $bakDir/daily_backup
find $daily_databakDir -name "daily*" -type f -mtime +35 -exec rm {} \; > /dev/null 2>&1
Copy after login


增量备份脚本

#!/bin/bash
# Name:zbk增量备份
# mysql zbk scripts
# By zxsdw.com
# Last modify:2015-01-21


#定义数据库用户名及密码
user=root
userPWD=密码
#定义数据库
database=数据库
#生成一个新的mysql-bin.00000X文件,如果err日志被清除,则自动新建一个。
/usr/local/mysql/bin/mysqladmin -u$user -p$userPWD flush-logs
#定义增量备份位置
daily_databakDir=/backup/mysql/daily_backup
#定义MYSQL数据日志目录
mysqlDataDir=/usr/local/mysql/var
#定义增量日志及目录
eMailFile=$daily_databakDir/email.txt
#eMail=admin@zxsdw.com
#定义变量DATE格式为20150127
DATE=`date +%Y%m%d`
#定义一个总的logFile日志
logFile=$daily_databakDir/mysql$DATE.log


#美化日志模板
echo "    " > $eMailFile
echo "-----------------------" >> $eMailFile
#时间格式为15-01-27 01:06:17
echo $(date +"%y-%m-%d %H:%M:%S") >> $eMailFile
echo "-------------------------" >> $eMailFile


#定义删除bin日志的时间范围,格式为20150124010540
TIME=$(date "-d 3 day ago" +%Y%m%d%H%M%S)
#定义需要增量备份数据的时间范围,格式为2015-01-26 01:04:11
StartTime=$(date "-d 1 day ago" +"%Y-%m-%d %H:%M:%S")

###########开始删除操作美化日志标题##############
echo "Delete 3 days before the log" >>$eMailFile

#删除三天前的bin文件,及更新index里的索引记录,美化日志标题
mysql -u$user -p$userPWD -e "purge master logs before ${TIME}" && echo "delete 3 days before log" |tee -a $eMailFile

#查找index索引里的bin 2进制文件并赋值给 i。
filename=`cat $mysqlDataDir/mysql-bin.index |awk -F "/" '{print $2}'`
for i in $filename
do
#########开始增量备份操作,美化日志标题###########
echo "$StartTime start backup binlog" >> $eMailFile

#利用mysqlbinlog备份1天前增加的数据,并gzip压缩打包到增量备份目录
/usr/local/mysql/bin/mysqlbinlog -u$user -p$userPWD -d $database --start-datetime="$StartTime" $mysqlDataDir/$i |gzip >> $daily_databakDir/daily$DATE.sql.gz |tee -a $eMailFile

done


#如果以上备份脚本执行成功,接着运行下面的删除脚本
if [ $? = 0 ]
then
# 删除mtime>32的增量日志备份文件
find $daily_databakDir -name "*.log" -type f -mtime +32 -exec rm {} \; > /dev/null 2>&1
cd $daily_databakDir
echo "Daily backup succeed" >> $eMailFile
else
echo "Daily backup fail" >> $eMailFile
#mail -s "MySQL Backup" $eMail < $eMailFile #备份失败之后发送邮件通知
#fi结束IF判断
fi


#把变量eMailFile的内容替换logFile内容
cat $eMailFile > $logFile

#如果上面的IF判断失败,再次运行删除mtime>32的增量日志备份文件
find $daily_databakDir -name "*.log" -type f -mtime +32 -exec rm {} \; > /dev/null 2>&1
rsync -vzrtopg --delete --progress --password-file=/usr/local/rsync/rsync.passwd root@$ip:/zxs/allimg/$(date -d -1day +%y%m%d) /zxs/allimg/
gunzip < /backup/mysql/daily_backup/ceshi.sql.gz | /usr/local/mysql/bin/mysql -u用户名 -p密码 数据库名 --force
--force参数 忽略错误

Copy after login

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

MySQL's Role: Databases in Web Applications MySQL's Role: Databases in Web Applications Apr 17, 2025 am 12:23 AM

The main role of MySQL in web applications is to store and manage data. 1.MySQL efficiently processes user information, product catalogs, transaction records and other data. 2. Through SQL query, developers can extract information from the database to generate dynamic content. 3.MySQL works based on the client-server model to ensure acceptable query speed.

Laravel Introduction Example Laravel Introduction Example Apr 18, 2025 pm 12:45 PM

Laravel is a PHP framework for easy building of web applications. It provides a range of powerful features including: Installation: Install the Laravel CLI globally with Composer and create applications in the project directory. Routing: Define the relationship between the URL and the handler in routes/web.php. View: Create a view in resources/views to render the application's interface. Database Integration: Provides out-of-the-box integration with databases such as MySQL and uses migration to create and modify tables. Model and Controller: The model represents the database entity and the controller processes HTTP requests.

Linux Architecture: Unveiling the 5 Basic Components Linux Architecture: Unveiling the 5 Basic Components Apr 20, 2025 am 12:04 AM

The five basic components of the Linux system are: 1. Kernel, 2. System library, 3. System utilities, 4. Graphical user interface, 5. Applications. The kernel manages hardware resources, the system library provides precompiled functions, system utilities are used for system management, the GUI provides visual interaction, and applications use these components to implement functions.

Solve database connection problem: a practical case of using minii/db library Solve database connection problem: a practical case of using minii/db library Apr 18, 2025 am 07:09 AM

I encountered a tricky problem when developing a small application: the need to quickly integrate a lightweight database operation library. After trying multiple libraries, I found that they either have too much functionality or are not very compatible. Eventually, I found minii/db, a simplified version based on Yii2 that solved my problem perfectly.

How to run java code in notepad How to run java code in notepad Apr 16, 2025 pm 07:39 PM

Although Notepad cannot run Java code directly, it can be achieved by using other tools: using the command line compiler (javac) to generate a bytecode file (filename.class). Use the Java interpreter (java) to interpret bytecode, execute the code, and output the result.

How to check the warehouse address of git How to check the warehouse address of git Apr 17, 2025 pm 01:54 PM

To view the Git repository address, perform the following steps: 1. Open the command line and navigate to the repository directory; 2. Run the "git remote -v" command; 3. View the repository name in the output and its corresponding address.

Laravel framework installation method Laravel framework installation method Apr 18, 2025 pm 12:54 PM

Article summary: This article provides detailed step-by-step instructions to guide readers on how to easily install the Laravel framework. Laravel is a powerful PHP framework that speeds up the development process of web applications. This tutorial covers the installation process from system requirements to configuring databases and setting up routing. By following these steps, readers can quickly and efficiently lay a solid foundation for their Laravel project.

MySQL vs. Other Programming Languages: A Comparison MySQL vs. Other Programming Languages: A Comparison Apr 19, 2025 am 12:22 AM

Compared with other programming languages, MySQL is mainly used to store and manage data, while other languages ​​such as Python, Java, and C are used for logical processing and application development. MySQL is known for its high performance, scalability and cross-platform support, suitable for data management needs, while other languages ​​have advantages in their respective fields such as data analytics, enterprise applications, and system programming.

See all articles