您好,登錄后才能下訂單哦!
一般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進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。