此脚本是备份数据库,可以改进命令,将其改进为备份重要的表
环境准备:
[root@localhost ~] yum -y install mariadb-server[root@localhost ~] mkdir /bak#创建存放备份文件的目录[root@localhost ~] systemctl restart mariadb[root@localhost ~] mysqladmin -u root password#创建mysql的root用户以及密码New password:Confirm new password:[root@localhost ~] mysql -u root -h localhost -pEnter password:#添加数据复制以下命令即可grant all on *.* to \'upuser\'@\'10.0.0.%\' identified by \'123.com\';create database data1;use data1;create table biao1(id int,name char(10),age int);insert into biao1(id,name,age) values(1,\'成龙\',49);insert into biao1(id,name,age) values(2,\'李连杰\',50);insert into biao1(id,name,age) values(3,\'李小龙\',100);
脚本部分:
#!/bin/bashmy_user=upusermy_pass=123.commy_host=10.0.0.2my_conn=\"-u $my_user -p$my_pass -h $my_host\"bak_command=\"mysqldump $my_conn\"db_name1=data1bf_dir=\"/bak\"#显示时间bf_time=`date +%Y%m%d%H%M`bf_filename=\"$db_name1-$bf_time.sql\"#命令执行部分$bak_command $db_name1 > $bf_dir/$bf_filename#日志部分if [ $? -eq 0 ]thenecho \"备份成功 $bf_filename \" >> /bak/succeed.logelseecho \"备份失败 $bf_filename \" >> /bak/error.logfi
修改crontab
[root@localhost bak] crontab -e添加:*/2 * * * * sh ~/mysql.sh每两分钟备份一次*顺序排列:分 时 日 月 周
模拟数据恢复
删除数据库[root@localhost bak] mysql -u upuser -h 10.0.0.2 -pMariaDB [(none)]> drop database data1;MariaDB [(none)]> exitBye因为恢复数据库内的数据必须要先创建出数据库后才可以恢复数据[root@localhost bak] mysql -u upuser -h 10.0.0.2 -pMariaDB [(none)]> create database data1;MariaDB [(none)]> exit恢复数据[root@localhost bak] mysql -u root -p data1 < /bak/data1-202005201520.sql[root@localhost bak] mysql -u upuser -h 10.0.0.2 -pMariaDB [(none)]> use data1;MariaDB [data1]> show tables;MariaDB [data1]> select * from biao1;