一键备份MySQL数据库与nginx虚拟主机添加
目录:
1、一键备份MYSQL数据库脚本
- 支持单个任意数据库的备份
- 支持多个数据库同时备份
- 支持所有数据库同时备份
2、一键Nginx虚拟主机添加脚本
- 支持单个虚拟主机添加
- 持任意域名虚拟主机添加;
1、一键备份MySQL数据库
前提MySQL密码:mysqladmin -u root password "abc-123"
#!/bin/bash
#mysql_db_backup_v1.sh
#################
MYSQL_NAME=$*
MYSQL_USER=root
MYSQL_PW=abc-123
MYSQL_DIR=/data/mysql/`date +%F`
if (( $# < 1 ));then
echo "请在脚本后输入所需要备份的数据库名"
exit 1
fi
if [[ $1 == all ]];then
/usr/bin/mysqldump -u$MYSQL_USER -p$MYSQL_PW --all-databases > $MYSQL_DIR/mysql_$*.sql
echo -e "\033[32m所有数据库已备份完毕\033[0m"
exit 0
fi
for i in $*
do
if [[ ! -d ${MYSQL_DIR} ]];then
mkdir -p ${MYSQL_DIR}
fi
/usr/bin/mysqldump -u$MYSQL_USER -p$MYSQL_PW -d $i > $MYSQL_DIR/mysql_${i}.sql
echo -e "\033[32m数据库${i}已备份完成\033[0m"
done
2、nginx虚拟主机添加
#!/bin/bash
#nginx_vhost_add_v4.sh
#yum_nginx版
#2021/03/03 16:26
######################
NGINX_BASE="/etc/nginx/conf.d"
NGINX_WEB="/usr/share/nginx/html"
NGINX_UP="/usr/sbin/nginx"
NGINX_YUM="yum -y install nginx-1.18.0"
if (( $# < 1 ));then
echo "在脚本后追加所需创建的虚拟主机的名称"
exit 1
fi
if [ ! -x $NGINX_UP ];then
echo -e "\033[32mnginx未安装,正在使用yum方式安装nginx\033[0m"
$NGINX_YUM > /dev/null
`systemctl start nginx` && echo -e "\033[32m已安装并成功启动nginx服务\033[0m"
fi
for i in $*
do
if [ ! -f ${NGINX_BASE}/${i}.conf ];then
touch ${NGINX_BASE}/${1}.conf
fi
cat > ${NGINX_BASE}/${i}.conf << EOF
server {
listen 80;
server_name $i;
location / {
index index.html;
root /usr/share/nginx/html/$i;
}
}
EOF
if [ ! -d $NGINX_WEB/$i ];then
mkdir -p /${NGINX_WEB}/${i}
echo -e "\033[32m 创建${i}发布目录\033[0m"
fi
cat > /$NGINX_WEB/$i/index.html << EOF
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
<h2> $i测试页面 </h2>
</body>
</html>
EOF
echo -e "\033[32m 虚拟主机${i}创建成功 \033[0m"
done
systemctl reload nginx
netstat -nptl | grep nginx
#PS:前提关闭防火墙