一般Mysql数据库备份会采用在MYSQL从库上执行全量备份+增量备份方式。在从库备份避免Mysql主库备份的时候锁表造成业务影响。
#!/bin/bash
set -e #当脚本有错误时,便停止执行脚本
#备份的数据库名
DATABASES=(
"magedu01"
"magedu02"
)
USER="root"
PASSWORD="dbpwd123"
MAIL="123456@163.com"
BACKUP_DIR=/data/backup
LOGFILE=/data/backup/data_backup.log
DATE=`date +%Y%m%d_%H%M`
cd $BACKUP_DIR
#开始备份之前,将备份信息头写入日记文件
echo "--------------------" >> $LOGFILE
echo "BACKUP DATE:" $(date +"%y-%m-%d %H:%M:%S") >> $LOGFILE
echo "-------------------" >> $LOGFILE
for DATABASE in ${DATABASES};do
/usr/local/mysql/bin/mysqldump -u$USER -p$PASSWORD --events -R --opt $DATABASE |gzip >${BACKUP_DIR}\/${DATABASE}_${DATE}.sql.gz
if [ $? == 0 ];then
echo "$DATE--$DATABASE is backup succeed" >> $LOGFILE
else
echo "Database Backup Fail!" >> $LOGFILE
fi
done
#判断数据库备份是否全部成功,全部成功就同步到异地备份f服务器
if [ $? == 0 ];then
/usr/bin/rsync -zrtopg --delete /data/backup/* root@192.168.10.10:/data/backup/ >/dev/null 2>&1
else
echo "Database Backup Fail!" >> $LOGFILE
#备份失败后向管理者发送邮件提醒
mail -s "database Daily Backup Fail!" $MAIL
fi
#删除30天以上的备份文件
find $BACKUP_DIR -type f -mtime +30 -name "*.gz" -exec rm -f {} \;
#!/bin/bash
set -e #当脚本有错误时,便停止执行脚本
LOG_PATH="/var/log/nginx" #定义日志存放路径
ACCESS_LOG="access.log"
ERROR_LOG="error.log"
YESTERDAY=$(date -d '1 day ago' +%F) #定义昨天的日期
for i in $(find ${LOG_PATH} -name ${ACCESS_LOG});do
cd $(dirname ${i})
if [[ -f ${ACCESS_LOG} ]];then
cp {,${YESTERDAY}-}${ACCESS_LOG} #将原本的日志文件复制一份
: > ${ACCESS_LOG} #将原本的日志内容清空
fi
if [[ -f ${ERROR_LOG} ]];then
ERROR_SIZE=$(ls -l ${ERROR_LOG} | awk '{print $5}')
if [[ ${ERROR_SIZE} -gt 20971520 ]];then #如果error.log日志大于20M,进行切割
cp {,${YESTERDAY}-}${ERROR_LOG}
: > ${ERROR_LOG}
fi
fi
done
find ${LOG_PATH} -type f -name "*.log" -mtime +7 -delete #将一周前的日志进行删除
企业负载均衡层如果用到Nginx+Keepalived架构,而Keepalived无法进行Nginx服务的实时切换,所以这里用了一个监控脚本check_nginx_pid.sh,每隔5秒就监控一次Nginx的运行状态,如果发现有问题就关闭本机的Keepalived程序,让VIP切换到从Nginx负载均衡器上。
#!/bin/bash
while :
do
nginxpid=`ps -C nginx --no-header | wc -l`
if [ $nginxpid -eq 0 ];then
ulimit -SHn 65535
/usr/local/nginx/sbin/nginx
sleep 5
else
/etc/init.d/keepalived stop
fi
sleep 5
done
1)此脚本应该能适应各种各样不同的内外网环境。
2)让脚本也顺便监控下MySQL是否正常运行。
3)Slave机器的IO和SQL状态都必须为YES,缺一不可,这里用到了多重条件判断-a。
#!/bin/bash
MYSQLPORT='netstat -na|grep "LISTEN"|grep "3306"|awk -F[:" "]+ '{print $4}''
MYSQLIP='ifconfig eth0|grep "inet addr" | awk -F[:" "]+ '{print $4}''
STATUS=$(/usr/local/mysql/bin/mysql -u dbuser -dbpwd123 -S /tmp/mysql.sock -e "show slave status\G" | grep -i "running")
IO_env='echo $STATUS | grep IO | awk ' {print $2}''
SQL_env='echo $STATUS | grep SQL | awk '{print $2}''
if [ "$MYSQLPORT" == "3306" ]
then
echo "mysql is running"
else
mail -s "warn!server: $MYSQLIP mysql is down" magedu@gmail.com
fi
if [ "$IO_env" = "Yes" -a "$SQL_env" = "Yes" ]
then
echo "Slave is running!"
else
echo "####### $date #########">> /data/log/check_mysql_slave.log
echo "Slave is not running!" >> /data/log/check_mysql_slave.log
mail -s "warn! $MySQLIP_replicate_error" magedu@gmail.com << /data/log/check_mysql_slave.log
fi
建议此脚本10分钟执行一次,可编写计划任务!
此脚本用于新装Linux的相关配置工作,比如更换默认yum源,优化系统内核、停掉一些没必要启动的系统服务等。此脚本尤其适合大批新安装的CentOS系列的服务器。适用于Centos7!
#!/bin/bash
#判断是否为root用户
if [ `whoami` != "root" ];then
echo " only root can run it"
exit 1
fi
#执行前提示
echo -e "\033[31m 这是centos7系统初始化脚本,将更新系统内核至最新版本,请慎重运行!\033[0m"
read -s -n1 -p "Press any key to continue or ctrl+C to cancel"
echo "Your inputs: $REPLY"
#1.定义配置yum源的函数
yum_config(){
mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup
wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
yum clean all && yum makecache
}
#2.定义配置NTP的函数
ntp_config(){
yum –y install chrony
systemctl start chronyd && systemctl enable chronyd
timedatectl set-timezone Asia/Shanghai && timedatectl set-ntp yes
}
#3.定义关闭防火墙的函数
close_firewalld(){
systemctl stop firewalld.service &> /dev/null
systemctl disable firewalld.service &> /dev/null
}
#4.定义关闭selinux的函数
close_selinux(){
setenforce 0
sed -i 's/enforcing/disabled/g' /etc/selinux/config
}
#5.定义安装常用工具的函数
yum_tools(){
yum install –y vim wget curl curl-devel bash-completion lsof iotop iostat unzip bzip2 bzip2-devel
yum install –y gcc gcc-c++ make cmake autoconf openssl-devel openssl-perl net-tools
source /usr/share/bash-completion/bash_completion
}
#6.定义升级最新内核的函数
update_kernel (){
rpm --import https://www.elrepo.org/RPM-GPG-KEY-elrepo.org
rpm -Uvh http://www.elrepo.org/elrepo-release-7.0-3.el7.elrepo.noarch.rpm
yum --enablerepo=elrepo-kernel install -y kernel-ml
grub2-set-default 0
grub2-mkconfig -o /boot/grub2/grub.cfg
}
#执行脚本
main(){
yum_config;
ntp_config;
close_firewalld;
close_selinux;
yum_tools;
update_kernel;
}
main
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。