shell study-16day--mysql database backup

1 , MariaDB database backup script

( 1) Introduction to MariaDB database

Starting from centos7.0, the mysql database that comes with the system has been changed to the mariadb database.

MariaDB database overview: MariaDB database management system is a branch of MySQL and is mainly maintained by the open source community. The purpose of using GPL license MariaDB is to be fully compatible with MySQL, including API and command line, so that it can easily become a substitute for MySQL.

After Oracle acquired MySQL, there was a potential risk of closing MySQL to its source, so the community adopted branching methods to avoid this risk. At present, many large Internet users and Linux distributors have abandoned MySQL and switched to the MariaDB camp. MariaDB is currently the most concerned MySQL database derivative and is also regarded as an alternative to the open source database MySQL

( 2) centos7.3 install mariadb

#installation

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

 #View installation version information

[root@test ~]# rpm -qa |grep mariadb 
mariadb-libs-5.5.68-1.el7.x86_64
mariadb-server-5.5.68-1.el7.x86_64
mariadb-5.5.68-1.el7.x86_64

#Start mariadb

[root@test ~]# systemctl start mariadb

#Set the mysql database root password

[root@test ~]# mysqladmin  -u root password "123456"

 #Log in to mysql and create the book library, create a user table in the book library, insert a record, the field id field value is 1.

[root@test ~]# mysql -u root -p 
MariaDB [(none)]> show databases;
MariaDB [(none)]> create database book;
MariaDB [(none)]> use book;
MariaDB [book]> create table user(id int); 
MariaDB [book]> insert into user values(1);
MariaDB [book]> select  * from user;    
+------+
| id   |
+------+
|    1 |
+------+
1 row in set (0.00 sec)
 
MariaDB [book]> commit;
MariaDB [book]>quit

( 3) Regular backup script

[root@test home]# cat mysql-back.sh  
#/bin/bash 
# msyql back  
# YX  
# 2020.11.21 
backdir=/home/mysql/back/`date +"%Y-%m-%d"` 
mysqldb=book 
mysqluser=root 
mysqlpassword=123456 #You 
must use root user, use $UID to judge, root user uid is 0. 
if [$UID -ne 0 ]; then 
    echo "YOU need root user" 
    exit 
fi #Judgment of 
 
backup files Whether the directory exists, create 
if [! -D $backdir ]; then  
   mkdir -p $backdir 
else 
   echo "this dir is exit" 
   exit 
fi 
 
#mysql backup 
/usr/bin/mysqldump -u$mysqluser -p$mysqlpassword $mysqldb >$backdir/${mysqldb}_`date +%Y-%m-%d-%H-%S`.sql;  
cd $backdir;
tar -zcvf ${mysqldb}_tar.gz *.sql;
find $backdir -type f -name *.sql -exec rm -rf {} \;
echo "mysql backup successfully"
[root@test home]# sh mysql-back.sh 
book_2020-11-21-23-43.sql
mysql backup successfully
[root@test home]# ls /home/mysql/back/2020-11-21/
book_tar.gz
[root@test home]#

Personal public number:

image.png


Guess you like

Origin blog.51cto.com/13440764/2575395