shell脚本数据库自动化备份

先做一个时间同步到aliyun
 [root@date ~]# yum install -y ntp
 [root@date ~]# systemctl status ntpd
 [root@date ~]# systemctl enable ntpd --now
 [root@date ~]# systemctl is-enabled ntpd
  enabled
 [root@date ~]# systemctl status ntpd
 [root@date ~]# ntpdate aliyun.com
 [root@date ~]# date
 Mon Nov 27 18:10:00 CST 2023
安装数据库
"安装mariadb服务"
# yum install -y mariadb mariadb-server
systemctl disable firewalld --now && setenforce 0 "关闭防火墙"
systemctl enable mariadb --now "开启mariadb服务"

"初始化数据库"
# mysql_secure_installation
Set root password? [Y/n]  # y <– 是否设置root用户密码,输入y并回车或直接回车
New password: # <– 设置root用户的密码
Re-enter new password: # <– 再输入一次你设置的密码
Remove anonymous users? [Y/n]  # y <– 是否删除匿名用户,回车
Disallow root login remotely? [Y/n] # n < –是否禁止root远程登录,根据自己的需求选择Y/n并回车
Remove test database and access to it? [Y/n] # y <– 是否删除test数据库,回车
Reload privilege tables now? [Y/n] #	y <– 是否重新加载权限表,回车
"创建库"
> create database cloud;
"创建表"
use cloud;
create table company(id int not null primary key,name varchar(50),addr varchar(255));
insert into company values(1,"alibaba","china");
"查看表的数据"
select * from company;
编写脚本
[root@localhost ~]# chmod +x database.sh 
[root@localhost ~]# ll
total 8
-rw-------. 1 root root 1219 Jun 16 01:02 anaconda-ks.cfg
-rwxr-xr-x. 1 root root  779 Dec  1 09:18 database.sh

[root@localhost ~]# cat database.sh 
#!/bin/bash
# 备份目录
DIR=/opt/db
# 获取时间戳
DATE=$(date +"%Y-%m-%d_%H%M%S")
# 数据库地址
HOST=localhost
# 数据库用户
USER=root
# 数据库密码
PASSWORD=000000
# 备份的数据库
DATABASE=cloud
# 创建用于存放数据的目录
[ ! -d "${DIR}" ] && mkdir -p "${DIR}"

if [ -z "${DIR}" ]
then
  echo "${DIR} 为空创建失败"
else
  echo "${DIR} 不为空创建成功"
fi

# 导出指定的数据库
mysqldump -u${USER} -p${PASSWORD} --host=${HOST} --databases ${DATABASE} > ${DIR}/${DATE}.sql

# 打包tar包
cd ${DIR} && tar -zcvf ${DATE}.tar.gz ${DATE}.sql

rm -rf ${DATE}.sql

# 指定删除1分钟之前的文件
find ${DIR} -mmin +1 -name "*.tar.gz" -exec rm -rf {} \;

# rsync文件同步
rsync -av /opt/db/${DATE}.tar.gz 192.168.169.4:/root/
### 在第二台节点导入测试
[root@localhost ~]# mysql -uroot -p000000
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 13
Server version: 5.5.68-MariaDB MariaDB Server

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
+--------------------+
3 rows in set (0.00 sec)

MariaDB [(none)]> create database cloud;
Query OK, 1 row affected (0.00 sec)

MariaDB [(none)]> show databases;       
+--------------------+
| Database           |
+--------------------+
| information_schema |
| cloud              |
| mysql              |
| performance_schema |
+--------------------+
4 rows in set (0.00 sec)

MariaDB [(none)]> Ctrl-C -- exit!
Aborted

[root@ftp ~]# ll
total 8
-rw-r--r--. 1 root root  882 Dec  5 13:44 2023-12-05_134456.tar.gz
-rw-------. 1 root root 1531 Nov 24 03:58 anaconda-ks.cfg

[root@ftp ~]# tar -zxvf 2023-12-05_134456.tar.gz 
2023-12-05_134456.sql

[root@ftp ~]# mysql -uroot -p000000 cloud < 2023-12-05_134456.sql 

[root@ftp ~]# mysql -uroot -p000000
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 15
Server version: 5.5.68-MariaDB MariaDB Server

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> use cloud;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
MariaDB [cloud]> show tables;
+-----------------+
| Tables_in_cloud |
+-----------------+
| company         |
+-----------------+
1 row in set (0.00 sec)

MariaDB [cloud]> select * from company;
+----+---------+-------+
| id | name    | addr  |
+----+---------+-------+
|  1 | alibaba | china |
+----+---------+-------+
1 row in set (0.00 sec)
### 用于编辑当前用户的crontab定时任务列表,这个任务每天凌晨12点执行一次
[root@localhost ~]# crontab -e
0 0 * * * /root/database.sh

### 列出当前用户的crontab定时任务列表
[root@lnmp ~]# crontab -l
0 0 * * * /root/database.sh

### 删除定时任务
[root@lnmp ~]# crontab -r
关闭提示You have new mail in /var/spool/mail/root
echo "unset MAILCHECK" >> /etc/profile
source /etc/profile

你可能感兴趣的:(linux,mysql,自动化运维,数据库,自动化,运维)