mysql backup database, table script

Prepare the environment: mysql database
1, install mysql

[root@localhost ~]#yum -y install mariadb mariadb-server

2. Start the database

[root@localhost ~]# systemctl start mariadb

3. Write a script to back up the database (MySQL sub-database automatic backup plan)

vim mysql_db.sh
#!/bin/bash
#MySQL分库自动备份方案
MYSQL_LIST=$(mysql -e "show databases"|grep -v "Database")
MYSQL_DIR=mysql_backup_`date +%F`
[ ! -d $MYSQL_DIR ] && mkdir $MYSQL_DIR
for i in $MYSQL_LIST
do
        mysqldump -uroot -B $i > ${MYSQL_DIR}/${i}.sql 2>/dev/null
done

3.1, execute script

[root@localhost ~]# sh mysql_db.sh

Insert picture description here
························································································

MySQL sub-table automatic backup scheme:
1. Write a script for mysql backup table

[root@localhost ~]# vim mysql_tables.sh
#!/bin/bash
#MySQL分库自动备份方案
MYSQL_LIST=$(mysql -e "show databases"|grep -v "Database")
MYSQL_DIR=mysql_backup_`date +%F`
[ ! -d $MYSQL_DIR ] && mkdir $MYSQL_DIR
for i in $MYSQL_LIST
do
        for j in `mysql -e "use $i;show tables;"|grep -v "Tables_in_mysql"`
        do
                mysqldump -uroot ${i} ${j} > ${MYSQL_DIR}/${i}_${j}.sql 2>/dev/null
        done
done

Execute the script:

[root@localhost ~]# sh mysql_tables.sh

Insert picture description here

Guess you like

Origin blog.csdn.net/xuetengbo111/article/details/109103790