当前位置:网站首页>MySQL 8 sub database and table backup database shell script

MySQL 8 sub database and table backup database shell script

2022-07-06 17:44:00 Oh, no, I forgot my paper

Don't bullshit Directly upload script content


Type of operating system :Linux centos7.6

Database version :MySQL8.0.21

The script is as follows

vim mysqldump.sh
#!/bin/bash
date=`date +'%Y-%m-%d'`
/usr/bin/expect <<-EOF
  spawn sh -c "/usr/bin/mysql -h 192.168.74.4 -ushilu -p -e 'show databases' > /data/database.txt"
  expect "Enter password:"
  send "Shilu2021!\r"
  expect EOF
EOF
Db=`cat /data/database.txt|grep -v Database`
for data in $Db
do
	[ -d /data/$date/ ] || mkdir -pv /data/$date
	/usr/bin/expect <<-EOF
	  spawn sh -c "/usr/bin/mysqldump -h 192.168.74.4 -ushilu -p $data > /data/$date/${data}.sql"
	  expect "Enter password:"
	  send "Shilu2021!\r"
	  expect EOF
	EOF
	/usr/bin/expect <<-EOF
	  spawn sh -c "/usr/bin/mysql -h 192.168.74.4 -ushilu -p -e 'show tables from $data' > /data/table_list"
	  expect "Enter password:"
	  send "Shilu2021!\r"
	  expect EOF
	EOF
	Tb=`cat /data/table_list|grep -v Tables`
	[ -d /data/$date/$data/ ] || mkdir -pv /data/$date/$data/
	for tb in $Tb
	do
		/usr/bin/expect <<-EOF
		  spawn sh -c "/usr/bin/mysqldump -h 192.168.74.4 -ushilu -p $data $tb > /data/$date/$data/$tb.sql"
		  expect "Enter password:"
		  send "Shilu2021!\r"
		  expect EOF
		EOF
	done
done

Then give the script execution permission and add it to crontab Just in the scheduled task ~


Script is not optimized , It may be rough ~

原网站

版权声明
本文为[Oh, no, I forgot my paper]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202131309431877.html