SHELL 自动备份MYSQL数据库

备份单个数据库及所有数据库

楓城浪子原创,转载请标明出处!

更多技术博文请见个人博客:https://fengchenglangzi.000webhostapp.com

微信bh19890922

QQ445718526、490425557

部分截图:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123

#!/bin/bash

#2017-8-16 08:58:22

#By baiyuanhong

#Auto_Backup_Mysql

###############################################

MYSQL_STATUS=`netstat -anpl |grep mysqld | grep 3306 | wc -l`

###############################################

###############################################

#system base info

IPADD=`ifconfig eth0 | grep "inet addr" | awk '{print $2}' | sed 's/addr://g'`

HOSTNAME=`hostname`

DATE=`date +"%y-%m-%d %H:%M:%S"`

USER=`whoami`

DISK_SDA1=`df -h | grep /dev/sda1 | awk '{print $5}'`

CPU_INFO=`cat /proc/loadavg`

###############################################

###############################################

#status_check

echo -e "\033[32m********************************************\033[0m"

if [ $MYSQL_STATUS -eq 0 ];then

    echo "Mysql..............................[NOT RUN]"

else

    echo "Mysql..............................[RUNNING]"

fi

echo -e "\033[32m********************************************\033[0m"

cat << EOF

|------------System Information-------------

|DATE :$DATE

|HOSTNAME :$HOSTNAME

|USER :$USER

|IP :$IPADD

|DISK_USED :$DISK_SDA1

|CPU_AVERAGE :$CPU_INFO

--------------------------------------------

EOF

echo -e "\033[32m*******Please Enter Your Choice:[1-3]******\033[0m"

cat << EOF

1)Backup Single Mysql DB

2)Backup All Mysql DB

3)exit

--------------------------------------------

EOF

read -p "Please Enter Your Choice:" input

#############################################

#############################################

if [ $input -eq 1 ];then

    mysql -uroot -p123456 -e "show databases;"

    read -p "Please Enter Your DBname:" DBname

    mysqldump -uroot -p123456 $DBname >/tmp/$DBname\_`date +%Y%m%d`.sql

fi

if [ $input -eq 2 ];then

    mysql -uroot -p123456 -e "show databases;"

    read -p "Please Confirm Your Choice(y/n):" yn

    if [ $yn == "y" ];then

        mysqldump -uroot -p123456 --all-databases >/tmp/all_databases_`date +%Y%m%d`.sql

    else

        exit

    fi

fi

if [ $input -eq 3 ];then

    exit

fi