1.编写一键备份MYSQL数据库脚本;

1)支持任意单个或者多个数据库的备份;
2)支持多个数据库、所有库备份;

思路:

  • 备份mysql数据库命令
#到处mysql所有库的数据到mysql_all.sql
mysqldump -uroot -p123456 -a > mysql_all.sql
#备份到对应的服务器
scp /root/mysql_all.sql 192.168.142.100:/tmp/mysql
cp /root/mysql_all.sql /tmp/mysql/
#备份到想要存放的目录
mysqldump -uroot -p123456 -a > /tmp/mysql/mysql_all.sql
  • 支持任意单个或多个库的备份
#!/bin/bash
DATABASE_1=$1
DATABASE_2=$2
MYSQL_PASSWD=123456
MYSQL_USER=root
BACKUP_DIR=/tmp/mysql_bak/
mkdir -p ${BACKUP_DIR}

if [ $DATABASE_1 != " " ];then
    mysqldump -u$MYSQL_USER -p$MYSQL_PASSWD -B $DATABASE_1   > ${DATABASE_1}.sql
    elif [ $? -eq 0 ];then
        cp ${DATABASE_1}.sql ${BACKUP_DIR}
        echo "已备份{$DATABASE_1}库,备份为名${DATABASE_1}.sql,备份目录${BACKUP_DIR}"
else
    echo "不存在${DATABASE_1}数据库,备份失败"
fi
if [ $DATABASE_2 != " " ];then
    mysqldump -u$MYSQL_USER -p$MYSQL_PASSWD -B $DATABASE_2   > ${DATABASE_2}.sql
    elif [ $? -eq 0 ];then
        cp ${DATABASE_2}.sql ${BACKUP_DIR}
        echo "已备份{$DATABASE_2}库,备份为名${DATABASE_2}.sql,备份目录${BACKUP_DIR}"     
else
    echo "不存在${DATABASE_2}数据库,备份失败"
fi

输出:
[root@node1 ~]# sh 1.sh 1 2
mysqldump: Got error: 1049: "Unknown database '1'" when selecting the database
mysqldump: Got error: 1049: "Unknown database '2'" when selecting the database

#弊端
#1、无法输出后面判断
#2、会创建2个空的sql 1.sql 2.sql
  • 修正:
#!/bin/bash
read -p "请输入你需要备份的数据库:" DATABASE_1 DATABASE_2
MYSQL_PASSWD=123456
MYSQL_USER=root
BACKUP_DIR=/tmp/mysql_bak/
mkdir -p ${BACKUP_DIR}
touch test.txt
mysql -u$MYSQL_USER -p$MYSQL_PASSWD -e "show databases;"| grep $DATABASE_1 > test.txt
CAT_TEXT=$(cat test.txt|wc -l)
if [ $CAT_TEXT -ne 0 ];then
    mysqldump -u$MYSQL_USER -p$MYSQL_PASSWD -B $DATABASE_1   > ${DATABASE_1}.sql
    cp ${DATABASE_1}.sql ${BACKUP_DIR}
    echo "已备份[$DATABASE_1]库,备份为名${DATABASE_1}.sql,备份目录${BACKUP_DIR}"
else
    echo "不存在$[DATABASE_1]数据库,备份失败"
fi

mysql -u$MYSQL_USER -p$MYSQL_PASSWD -e "show databases;"| grep $DATABASE_2 > test.txt
CAT_TEXT=$(cat test.txt|wc -l)
if [ $CAT_TEXT -ne 0 ];then
    mysqldump -u$MYSQL_USER -p$MYSQL_PASSWD -B $DATABASE_2   > ${DATABASE_2}.sql
    cp ${DATABASE_2}.sql ${BACKUP_DIR}
    echo "已备份[$DATABASE_2]库,备份为名${DATABASE_2}.sql,备份目录${BACKUP_DIR}"     
else
    echo "不存在$[DATABASE_2]数据库,备份失败"
fi
#需要备份更多库,就在下面同样加上DATABASE_3以及添加上相应的代码即可。
  • 支持多个数据库、所有库备份

版本1:

#!/bin/bash
read -p "请输入你需要备份的数据库:" DATABASE_1 DATABASE_2 
MYSQL_PASSWD=123456
MYSQL_USER=root
BACKUP_DIR=/tmp/mysql_bak/
mkdir -p ${BACKUP_DIR}
touch test.txt
mysql -u$MYSQL_USER -p$MYSQL_PASSWD -e "show databases;"| grep $DATABASE_1 > test.txt
CAT_TEXT=$(cat test.txt|wc -l)
if [ $CAT_TEXT -ne 0 ];then
    mysqldump -u$MYSQL_USER -p$MYSQL_PASSWD -B $DATABASE_1 > ${DATABASE_1}.sql
    cp ${DATABASE_1}.sql ${BACKUP_DIR}
    echo "已备份[$DATABASE_1]库,备份为名${DATABASE_1}.sql,备份目录${BACKUP_DIR}"
else
    echo "不存在$[DATABASE_1]数据库,备份失败"
fi

mysql -u$MYSQL_USER -p$MYSQL_PASSWD -e "show databases;"| grep $DATABASE_2 > test.txt
CAT_TEXT=$(cat test.txt|wc -l)
if [ $CAT_TEXT -ne 0 ];then
    mysqldump -u$MYSQL_USER -p$MYSQL_PASSWD -B $DATABASE_2   > ${DATABASE_2}.sql
    cp ${DATABASE_2}.sql ${BACKUP_DIR}
    echo "已备份[$DATABASE_2]库,备份为名${DATABASE_2}.sql,备份目录${BACKUP_DIR}"     
else
    echo "不存在$[DATABASE_2]数据库,备份失败"
fi

版本2:

#!/bin/bash
#2022年6月6日
###################
MYSQL_PASSWD=123456
MYSQL_USER=root
BACKUP_DIR=/tmp/mysql_bak/
DATE=$(date +%F)
if [ $# -eq 0 ];then
    echo "请在脚本后,输入要备份的数据库名。"
    exit 
fi
if [[ $* != "all" ]];then
    echo "无需备份全部库"
else
    echo "开始备份全部库。"
    mysqldump -u$MYSQL_USER -p$MYSQL_PASSWD -A > all_${DATE}.sql
    cp all_${DATE}.sql ${BACKUP_DIR}
    echo "备份库名为:all_${DATE}.sql"
    exit
fi
for BASE_UP in $*
do
mysql -u$MYSQL_USER -p$MYSQL_PASSWD -e "show databases;"| grep ${BASE_UP} > test.txt
CAT_TEXT=$(cat test.txt|wc -l)
if [ $CAT_TEXT -ne 0 ];then
    mysqldump -u$MYSQL_USER -p$MYSQL_PASSWD -B $BASE_UP > ${BASE_UP}_${DATE}.sql
    cp ${BASE_UP}_${DATE}.sql ${BACKUP_DIR}
    echo "已备份${BASE_UP}库,备份为名${BASE_UP}_${DATE}.sql,备份目录${BACKUP_DIR}"
else
    echo "不存在${BASE_UP}数据库,备份失败"
fi
done

2.编写一键Nginx虚拟主机添加脚本;

1)支持任意单个或者多个虚拟主机添加;
2)支持多个虚拟主机添加;

#!/bin/bash
#2022年6月6日
#一键Nginx虚拟主机添加脚本
###################
NG_BASE_DIR=/data/nginx
NG_CONF_DIR=${NG_BASE_DIR}/conf/sleep
NG_HTML_DIR=${NG_BASE_DIR}/html
if [ $# -eq 0 ];then
    echo "请输入你要添加的域名"
    exit
fi

#判断虚拟主机配置目录是否存在
if [ ! -d ${NG_CONF_DIR} ];then
    mkdir -p ${NG_CONF_DIR}
fi
#配置输入域名的配置文件
for CONF_NAME in $*
do
    echo "检查虚拟主机配置,有则跳过,没有则创建"
    if [ ! -f ${NG_CONF_DIR}/${CONF_NAME}.conf ];then
        touch ${NG_CONF_DIR}/${CONF_NAME}.conf
        echo "虚拟主机${CONF_NAME}不存在,在${NG_CONF_DIR}下创建新建虚拟文件${CONF_NAME}.conf"
    else
        echo "虚拟主机${CONF_NAME}存在"
    fi
    
    if [ ! -d ${NG_HTML_DIR}/${CONF_NAME} ];then
        mkdir -p ${NG_HTML_DIR}/${CONF_NAME}
        echo "${CONF_NAME}" > ${NG_HTML_DIR}/${CONF_NAME}/index.html
        echo "创建了${CONF_NAME}虚拟主机目录及首页文件index.html"
    else
        echo "${CONF_NAME}项目已经存在。"
    fi
    echo "添加虚拟主机配置文件"
cat > ${NG_CONF_DIR}/${CONF_NAME}.conf << EOF
server {
    listen 80;
    server_name ${CONF_NAME};
    
    location / {
        root ${NG_HTML_DIR};
        index index.html;
    }
}
EOF
echo "虚拟主机${NG_HTML_DIR}/${CONF_NAME}已经配置成功"
echo "-----------------------------------------------------------------"
done
#检查并平滑重启nginx
${NG_BASE_DIR}/sbin/nginx -t
${NG_BASE_DIR}/sbin/nginx -s reload
echo "已检查并且平滑重启"
echo "ls -l ${NG_HTML_DIR}"
ls -l ${NG_HTML_DIR}

3)支持删除虚拟主机、多个虚拟主机删除;

#!/bin/bash
#2022年6月6日
#支持删除虚拟主机、多个虚拟主机删除脚本
###################
NG_BASE_DIR=/data/nginx
NG_CONF_DIR=${NG_BASE_DIR}/conf/sleep
NG_HTML_DIR=${NG_BASE_DIR}/html
if [ $# -eq 0 ];then
    echo "请输入你要删除的域名"
    exit
fi

#配置输入域名的配置文件
for CONF_NAME in $*
do
    echo "检查虚拟主机配置,有则删除,没有则跳过"
    if [  -f ${NG_CONF_DIR}/${CONF_NAME}.conf ];then
        rm -f  ${NG_CONF_DIR}/${CONF_NAME}.conf
        echo "虚拟主机${CONF_NAME}存在,删除${NG_CONF_DIR}下虚拟文件${CONF_NAME}.conf"
    else
        echo "虚拟主机${CONF_NAME}不存在"
    fi
    
    if [ -d ${NG_HTML_DIR}/${CONF_NAME} ];then
        rm -fr ${NG_HTML_DIR}/${CONF_NAME}
        echo "删除${CONF_NAME}虚拟主机目录及首页文件index.html"
    else
        echo "${CONF_NAME}项目不存在。"
    fi
echo "虚拟主机${NG_HTML_DIR}/${CONF_NAME}已经删除成功"
echo "-----------------------------------------------------------------"
done
#检查并平滑重启nginx
${NG_BASE_DIR}/sbin/nginx -t
${NG_BASE_DIR}/sbin/nginx -s reload
echo "已检查并且平滑重启"
echo "ls -l ${NG_HTML_DIR}"
ls -l ${NG_HTML_DIR}