文章目录
- 一、MySQL数据库备份单循环
- 1. 安装mysql
- 2. 配置mysql环境变量
- 3. 刷新环境变量
- 4. 创建数据库和表lue
- 5. 脚本制作
- 6. 运行脚本
- 7. 查看备份的sql文件
- 7. 脚本升级动态传参
- 8. 运行脚本
- 9. 查看备份的sql文件
- 二、MySQL数据库表备份多循环
- 2.1. 脚本制作
- 2.2. 运行脚本
- 2.3. 指定目录查看指定数据库的表
- 2.4. 脚本升级
- 2.5. 运行脚本
- 三、添加备份输出
- 3.1 备份数据库
- 3.2 备份数据库和表
一、MySQL数据库备份单循环
1. 安装mysql
Linux6、7 系列 安装、卸载mysql
2. 配置mysql环境变量
vim /etc/profile
export MYSQL_HOME=/app/mysql-5.7.25
export PATH=$PATH:${JAVA_HOME}/bin:${MYSQL_HOME}/bin
3. 刷新环境变量
source /etc/profile
4. 创建数据库和表lue
5. 脚本制作
#创建脚本
vim db_backup.sh
#!/bin/bash
DATE=$(date +%F_%H-%M-%S)
HOST=localhost
USER=root
PASS=root
BACKUP_DIR=/data/db_backup
DB_LIST=$(mysql -h$HOST -u$USER -p$PASS -s -e "show databases;" 2>/dev/null |egrep -v "Database|information_schema|mysql|performance_schema|sys")for DB in $DB_LIST; doBACKUP_NAME=$BACKUP_DIR/${DB}_${DATE}.sqlif ! mysqldump -h$HOST -u$USER -p$PASS -B $DB > $BACKUP_NAME 2>/dev/null; thenecho "$BACKUP_NAME 备份失败!"fi
done
6. 运行脚本
[root@ly-01 ~]# bash db_backup.sh
7. 查看备份的sql文件
cd /data/db_backup
ll
[root@ly-01 db_backup]# pwd
/data/db_backup
[root@ly-01 db_backup]# ll
total 300
-rw-r--r-- 1 root root 303483 Feb 25 11:26 ke_2020-02-25_11-26-26.sql
[root@ly-01 db_backup]#
7. 脚本升级动态传参
#!/bin/bash
DATE=$(date +%F_%H-%M-%S)
HOST=$1
USER=$2
PASS=$3
BACKUP_DIR=/data/db_backup
DB_LIST=$(mysql -h$HOST -u$USER -p$PASS -s -e "show databases;" 2>/dev/null |egrep -v "Database|information_schema|mysql|performance_schema|sys")for DB in $DB_LIST; doBACKUP_NAME=$BACKUP_DIR/${DB}_${DATE}.sqlif ! mysqldump -h$HOST -u$USER -p$PASS -B $DB > $BACKUP_NAME 2>/dev/null; thenecho "$BACKUP_NAME 备份失败!"fi
done
8. 运行脚本
[root@ly-01 ~]# bash db_backup_yh.sh 192.168.43.242 root root
9. 查看备份的sql文件
[root@ly-01 db_backup]# pwd
/data/db_backup
[root@ly-01 db_backup]# ll
total 300
-rw-r--r-- 1 root root 303483 Feb 25 11:26 ke_2020-02-25_11-26-26.sql
[root@ly-01 db_backup]# ll
total 600
-rw-r--r-- 1 root root 303483 Feb 25 11:26 ke_2020-02-25_11-26-26.sql
-rw-r--r-- 1 root root 303488 Feb 25 11:29 ke_2020-02-25_11-29-20.sql
[root@ly-01 db_backup]#
二、MySQL数据库表备份多循环
2.1. 脚本制作
#创建脚本
vim db_table_backup.sh
#!/bin/bash
DATE=$(date +%F_%H-%M-%S)
HOST=localhost
USER=root
PASS=root
BACKUP_DIR=/data/db_backup
DB_LIST=$(mysql -h$HOST -u$USER -p$PASS -s -e "show databases;" 2>/dev/null |egrep -v "Database|information_schema|mysql|performance_schema|sys")for DB in $DB_LIST; doBACKUP_DB_DIR=$BACKUP_DIR/${DB}_${DATE}[ ! -d $BACKUP_DB_DIR ] && mkdir -p $BACKUP_DB_DIR &>/dev/nullTABLE_LIST=$(mysql -h$HOST -u$USER -p$PASS -s -e "use $DB;show tables;" 2>/dev/null)for TABLE in $TABLE_LIST; doBACKUP_NAME=$BACKUP_DB_DIR/${TABLE}.sqlif ! mysqldump -h$HOST -u$USER -p$PASS $DB $TABLE > $BACKUP_NAME 2>/dev/null; thenecho "$BACKUP_NAME 备份失败!"fidone
done
2.2. 运行脚本
bash db_table_backup.sh
2.3. 指定目录查看指定数据库的表
[root@ly-01 ~]# cd /data/db_backup/
[root@ly-01 db_backup]# ll
total 604
-rw-r--r-- 1 root root 303483 Feb 25 11:26 ke_2020-02-25_11-26-26.sql
-rw-r--r-- 1 root root 303488 Feb 25 11:29 ke_2020-02-25_11-29-20.sql
drwxr-xr-x 2 root root 4096 Feb 25 11:37 ke_2020-02-25_11-37-18
[root@ly-01 db_backup]# cd ke_2020-02-25_11-37-18/
[root@ly-01 ke_2020-02-25_11-37-18]# ll
total 340
-rw-r--r-- 1 root root 2282 Feb 25 11:37 ke_alarm_clusters.sql
-rw-r--r-- 1 root root 2117 Feb 25 11:37 ke_alarm_config.sql
-rw-r--r-- 1 root root 2337 Feb 25 11:37 ke_alarm_consumer.sql
-rw-r--r-- 1 root root 2200 Feb 25 11:37 ke_consumer_bscreen.sql
-rw-r--r-- 1 root root 2068 Feb 25 11:37 ke_consumer.sql
-rw-r--r-- 1 root root 1985 Feb 25 11:37 ke_logsize.sql
-rw-r--r-- 1 root root 2693 Feb 25 11:37 ke_metrics_offline.sql
-rw-r--r-- 1 root root 289927 Feb 25 11:37 ke_metrics.sql
-rw-r--r-- 1 root root 2177 Feb 25 11:37 ke_p_role.sql
-rw-r--r-- 1 root root 2747 Feb 25 11:37 ke_resources.sql
-rw-r--r-- 1 root root 2213 Feb 25 11:37 ke_role_resource.sql
-rw-r--r-- 1 root root 2132 Feb 25 11:37 ke_sql_history.sql
-rw-r--r-- 1 root root 1962 Feb 25 11:37 ke_topic_rank.sql
-rw-r--r-- 1 root root 1965 Feb 25 11:37 ke_user_role.sql
-rw-r--r-- 1 root root 2089 Feb 25 11:37 ke_users.sql
[root@ly-01 ke_2020-02-25_11-37-18]#
2.4. 脚本升级
#!/bin/bash
DATE=$(date +%F_%H-%M-%S)
HOST=$1
USER=$2
PASS=$3
BACKUP_DIR=/data/db_backup
DB_LIST=$(mysql -h$HOST -u$USER -p$PASS -s -e "show databases;" 2>/dev/null |egrep -v "Database|information_schema|mysql|performance_schema|sys")for DB in $DB_LIST; doBACKUP_DB_DIR=$BACKUP_DIR/${DB}_${DATE}[ ! -d $BACKUP_DB_DIR ] && mkdir -p $BACKUP_DB_DIR &>/dev/nullTABLE_LIST=$(mysql -h$HOST -u$USER -p$PASS -s -e "use $DB;show tables;" 2>/dev/null)for TABLE in $TABLE_LIST; doBACKUP_NAME=$BACKUP_DB_DIR/${TABLE}.sqlif ! mysqldump -h$HOST -u$USER -p$PASS $DB $TABLE > $BACKUP_NAME 2>/dev/null; thenecho "$BACKUP_NAME 备份失败!"fidone
done
2.5. 运行脚本
[root@ly-01 ~]# bash db_table_backup.sh 192.168.43.242 root root
三、添加备份输出
3.1 备份数据库
#!/bin/bash
DATE=$(date +%F_%H-%M-%S)
HOST=$1
USER=$2
PASS=$3
BACKUP_DIR=/data/db_backup
DB_LIST=$(mysql -h$HOST -u$USER -p$PASS -s -e "show databases;" 2>/dev/null |egrep -v "Database|information_schema|mysql|performance_schema|sys")for DB in $DB_LIST; doBACKUP_NAME=$BACKUP_DIR/${DB}_${DATE}.sqlecho "$BACKUP_NAME 备份成功!" if ! mysqldump -h$HOST -u$USER -p$PASS -B $DB > $BACKUP_NAME 2>/dev/null; thenecho "$BACKUP_NAME 备份失败!"fi
done
[root@ly-01 ~]# bash db_backup_yh.sh 192.168.43.242 root root
/data/db_backup/ke_2020-02-25_11-49-46.sql 备份成功!
[root@ly-01 ~]#
3.2 备份数据库和表
#!/bin/bash
DATE=$(date +%F_%H-%M-%S)
HOST=$1
USER=$2
PASS=$3
BACKUP_DIR=/data/db_backup
DB_LIST=$(mysql -h$HOST -u$USER -p$PASS -s -e "show databases;" 2>/dev/null |egrep -v "Database|information_schema|mysql|performance_schema|sys")for DB in $DB_LIST; doBACKUP_DB_DIR=$BACKUP_DIR/${DB}_${DATE}[ ! -d $BACKUP_DB_DIR ] && mkdir -p $BACKUP_DB_DIR &>/dev/nullTABLE_LIST=$(mysql -h$HOST -u$USER -p$PASS -s -e "use $DB;show tables;" 2>/dev/null)for TABLE in $TABLE_LIST; doBACKUP_NAME=$BACKUP_DB_DIR/${TABLE}.sqlecho "$BACKUP_NAME 备份成功!" if ! mysqldump -h$HOST -u$USER -p$PASS $DB $TABLE > $BACKUP_NAME 2>/dev/null; thenecho "$BACKUP_NAME 备份失败!"fidone
done
[root@ly-01 ~]# bash db_table_backup.sh 192.168.43.242 root root
/data/db_backup/ke_2020-02-25_11-46-50/ke_alarm_clusters.sql 备份成功!
/data/db_backup/ke_2020-02-25_11-46-50/ke_alarm_config.sql 备份成功!
/data/db_backup/ke_2020-02-25_11-46-50/ke_alarm_consumer.sql 备份成功!
/data/db_backup/ke_2020-02-25_11-46-50/ke_consumer.sql 备份成功!
/data/db_backup/ke_2020-02-25_11-46-50/ke_consumer_bscreen.sql 备份成功!
/data/db_backup/ke_2020-02-25_11-46-50/ke_logsize.sql 备份成功!
/data/db_backup/ke_2020-02-25_11-46-50/ke_metrics.sql 备份成功!
/data/db_backup/ke_2020-02-25_11-46-50/ke_metrics_offline.sql 备份成功!
/data/db_backup/ke_2020-02-25_11-46-50/ke_p_role.sql 备份成功!
/data/db_backup/ke_2020-02-25_11-46-50/ke_resources.sql 备份成功!
/data/db_backup/ke_2020-02-25_11-46-50/ke_role_resource.sql 备份成功!
/data/db_backup/ke_2020-02-25_11-46-50/ke_sql_history.sql 备份成功!
/data/db_backup/ke_2020-02-25_11-46-50/ke_topic_rank.sql 备份成功!
/data/db_backup/ke_2020-02-25_11-46-50/ke_user_role.sql 备份成功!
/data/db_backup/ke_2020-02-25_11-46-50/ke_users.sql 备份成功!
[root@ly-01 ~]#