Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

To write a shell script to back up the mysql database

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/03 Report--

Let me tell you a little bit about the steps of writing a shell script to back up the mysql database. Have you learned about similar topics before? If you are interested, let's take a look at this article. I believe it is more or less helpful to read the steps of writing a shell script to back up the mysql database.

Mysql database shell backup script

Any project can not run without data, persistent data is particularly important for an enterprise, and should not be taken lightly at any time. Here is a shell script I wrote to back up the data and share it with you.

#! / bin/bash# database connection information MY_user= "" MY_pass= "MY_port=" 3306 "# environment variable PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:/data/mysql_57/binexport PATHMY_data= `date" +% F "`# script log shell _ LOG=" / root/mysql_bak.sh.log "# mysql directory Mysql_datadir=" / data/mysql_57 "Mysql_Binlog_dir=" / data/mysql_57/logs/bin-log "# backup directory Bak_dir=" / root/mysql_data "Full_dir=$ {Bak_dir} / full # full backup directory DB_dir=$ {Bak_dir} / db # single library backup directory Binlog_dir=$ {Bak_dir} / binlog # incremental binary backup directory # Write Log shell_log () {LOG_INFO=$1 echo" $(date "+%) F ") $(date" +% T "): $0: ${LOG_INFO}" > > ${SHELL_LOG}} case $1 in# full backup Full) mysqldump-u$ {MY_user}-p$ {MY_pass}-P$ {MY_port}-A-B-F-R-x-events | gzip > ${Full_dir} / ${MY_data} _ all.sql.gz if [[$?-eq 0]] Then shell_log "SUCCESS: mysql Full database bak is success" else shell_log "ERROR: mysql Full database bak error" fi # single library backup db) db_fun () {DB_name=$1 mysqldump-u$ {MY_user}-p$ {MY_pass}-P$ {MY_port}-F-B ${DB_name} | gzip > ${DB_dir} / ${MY_data} _ ${DB_name} .sql.gz if [[$?-eq 0]] Then shell_log "SUCCESS: mysql database ${DB_name} bak is success" else shell_log "ERROR: mysql database ${DB_name} bak error" fi} db_fun cadavertrace # incremental backup (binary logs are refreshed by both full and single database backups) cd ${Mysql_Binlog_dir} binlog= `ls-At mysql-bin* | head-n 1` gzip-c ${binlog} > ${Binlog_dir} / ${binlog} .gz mysql- u$ {MY_user}-p$ {MY_pass}-P$ {MY_port}-e "flush logs;" 2 > / dev/null *) echo "ERROR: Usage: mysqlbak [Full | db | increment]"; esac#if [[$#-eq 0]]; then# echo "Invalid paramete Full | db | increment "

# fi

What do you think of this article about the steps of writing a shell script to back up the mysql database? If you want to know more about it, you can continue to follow our industry information section.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report