[email protected] / / m...">

当前位置:网站首页>Multi database and multi table backup and restore of MySQL under Linux

Multi database and multi table backup and restore of MySQL under Linux

2022-06-25 20:55:00 Gem, Chaochao

One 、 Write a bash Script files

1、 Create directory

Create a file directory in the root directory

1) The scripts directory

[[email protected] /]#mkdir scripts/

2) Backup file directory

[[email protected] /]#mkdir -p server/backup/

2、 establish bash Script files

[[email protected] /]#cd /scripts/
[[email protected] scripts]#vim mysqldump1.sh 

The contents of the document are as follows :

#!bin/bash
USER=root
PASSWD=root
SOCKET=/software/mysql/mysql.sock
LOGIN="mysql -u$USER -p$PASSWD -S $SOCKET"
DUMP="mysqldump --no-defaults -u$USER -p$PASSWD -S $SOCKET"
DATABASE=$($LOGIN -e "show databases;"|egrep -v "*chema|mysql|sys"|sed '1d')
for database in $DATABASE
do
  TABLE=$($LOGIN -e "use $database;show tables;"|sed '1d')
  for table in $TABLE
   do
     [ -d /server/backup/$database ] || mkdir /server/backup/$database -p
     $DUMP $database $table |gzip >/server/backup/$database/${database}_${table}_$(date +%F).sql.gz
   done
done

3、 Perform backup operations

[[email protected] /]#cd /scripts/
[[email protected] scripts]#sh mysqldump1.sh 

Two 、 Extract files and restore databases

[[email protected] /]# cd /server/backup/
[[email protected] backup]# ll
 Total usage  0
drwxr-xr-x. 2 root root 109 4 month   30 15:29 test_interface
[[email protected] backup]# cd test_interface/
[[email protected] test_interface]# ll
 Total usage  4
-rw-r--r--. 1 root root 855 4 month   30 15:29 test_interface_config_total_2021-04-30.sql.gz
[[email protected] test_interface]#gunzip <test_interface_config_total_2021-04-30.sql.gz | mysql -uroot -proot test_interface
mysql: [Warning] Using a password on the command line interface can be insecure.
原网站

版权声明
本文为[Gem, Chaochao]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202181337141247.html