Linux运维常用脚本总结
原创 小驿 数据与共享 2023-06-13 19:30 发表于陕西 收录于合集#项目运维9个 1.日志切割# nginx 日志分割日志
#!/bin/bash --login
shopt -s expand_aliases
datestr=$(date -d "-1 days" +%Y-%m-%d)
echo $datestr;
mv /test/log/nginx/access.log /test/log/nginx/access_${datestr}.log
mv /test/log/nginx/error.log /test/log/nginx/error_${datestr}.log
sleep 1
service nginx reload
2.删除日志脚本
#!/bin/bash --login
shopt -s expand_aliases
#日志保留天数
log_max_stay_days=7
# 根据时间删除指定路径下的log文件
function del_log_by_time(){
dir=$1
days=$2
filePattern=$3
if [ -d ${dir} ] && [ ${#dir} -gt 2 ]; then
rm -f ${dir}/*.`date -d '-'${days}' days' +%Y-%m-%d`.*
find ${dir} -mtime +$((days-1)) -name "${filePattern}" -exec rm -f {} \;
fi
}
function del_log(){
# 删除tomcat日志
del_log_by_time "/test/tomcat/logs" $log_max_stay_days "*.*"
# 删除nginx日志
del_log_by_time "/test/log/nginx" $log_max_stay_days "*.log"
# 删除redis日志
del_log_by_time "/test/log/redis" $log_max_stay_days "*.log"
}
del_log $@
3.数据备份
# 临时备份,通常用xtrabackup做全量和增量备份。
#!/bin/bash
DB_USER=root
DB_PWD=123
DB_PORT=3306
DB_HOST=127.0.0.1
# 本机部署时,请将MEDIA_SERVER改为localhost或者置为空
MEDIA_SERVER=
SSH_PORT=22
BACKUP_BIZ=1
#数据库名称
DATABASE_BIZ="db1,db2,db3"
back_path=/test/backup/A_db_bak_noce_a_week/
#备份最少保留天数
file_backup_max_stay_days=7
function get_databases()
{
DATABASES=""
if [ "$BACKUP_BIZ" == "1" ];then
DATABASES=$DATABASE_BIZ
fi
DATABASES=${DATABASES##,}
if [ "$DATABASES" == "" ];then
ERROR_INFO="配置了不导出BIZ及STAT"
return 1
fi
return 0
}
# 根据时间删除指定路径下的log文件
function del_log_by_time(){
dir=$1
days=$2
filePattern=$3
dirFlag=$4
if [ -d ${dir} ] && [ ${#dir} -gt 2 ]; then
if [ "${dirFlag}" == "1" ];then
find ${dir} -mtime +$((days-1)) -type d -name "${filePattern}" -exec rm -rf {} \;
else
find ${dir} -mtime +$((days-1)) -name "${filePattern}" -exec rm -f {} \;
fi
fi
}
#复制到多媒体路径
function move_to_media_server()
{
to_move_file=$1
if [ "$MEDIA_SERVER" == "localhost" ] || [ `ifconfig |grep "$MEDIA_SERVER "|wc -l` -gt 0 ];then
ERROR_INFO="多媒体服务即本机,无需上传"
return 1
fi
if [ "$MEDIA_SERVER" == "" ];then
ERROR_INFO="未能获取多媒体服务器"
return 1
fi
# 判断免密状态,无则不复制
if [ `cat /root/.ssh/known_hosts |grep -E "$MEDIA_SERVER |$MEDIA_SERVER," |wc -l` -lt 1 ];then
ERROR_INFO="未设置免密登录到$MEDIA_SERVER"
return 1
fi
hostname=$(hostname)
ssh -p $SSH_PORT -o "StrictHostKeyChecking no" root@$MEDIA_SERVER "mkdir -p $back_path/${hostname}"
scp -r -P $SSH_PORT $to_move_file root@$MEDIA_SERVER:$back_path/${hostname}/
return 0
}
# 全量备份
function backup_full()
{
echo "开始全量备份...."
get_databases
if [ "$DATABASES" == "" ];then
SUCCESS_FLAG=0
else
OLD_IFS=$IFS
IFS=","
db_arr=($DATABASES)
cd $back_path
for dbname in ${db_arr[@]}
do
mydumper -h $DB_HOST -u $DB_USER -p $DB_PWD -P $DB_PORT -G -E -R -B $dbname -o ${dbname}_$(date +%Y-%m-%d)/ -v 3 -t 3 -k
SUCCESS_FLAG=$?
tar czf ${dbname}_$(date +%Y-%m-%d).tar.gz ${dbname}_$(date +%Y-%m-%d)/
rm -rf ${dbname}_$(date +%Y-%m-%d)/
move_to_media_server ${back_path}/${dbname}_$(date +%Y-%m-%d).tar.gz
if [ $? -eq 1 ];then
echo $ERROR_INFO
fi
del_log_by_time "${back_path}" $file_backup_max_stay_days "${dbname}_*.tar.gz"
del_log_by_time "${back_path}" $file_backup_max_stay_days "${dbname}_*.log"
done
fi
echo "结束备份!"
}
function run()
{
if [ "$DB_PORT" == "" ];then
DB_PORT=3306
fi
backup_full
}
run $@
4.过滤大表导出数据库
#!/bin/bash
#execute all script in specified directory
dt=`date +%Y%m%d%H%M`
db_IP=127.0.0.1 # 测试库IP
db_pass=abc@123 # 测试库密码
db_name=test # 测试库名称
db_port=3306 # 测试库端口
MAX=100 #大表定义,将不会导出超过100MB的表
dir=exp_$dt #导出目录
cd /test
mkdir $dir
# 导出
echo "查询 测试库大于$MAX的大表,并存入big_test.txt"
mysql -uroot -h$db_IP -p$db_pass -P$db_port -N -e "select concat('$db_name.', TABLE_NAME) from (SELECT TABLE_NAME,DATA_LENGTH,INDEX_LENGTH,(DATA_LENGTH+INDEX_LENGTH) as length,TABLE_ROWS,round((DATA_LENGTH+INDEX_LENGTH)/1024/1024,3) as total_size FROM information_schema.TABLES WHERE TABLE_SCHEMA='$db_name' ) a where a.total_size>$MAX;" >>/test/$dir/big_test.txt
echo "已找到"
#排除大表导出
mydumper -h $db_IP -u root -p $db_pass -P $db_port -G -E -R -B $db_name -O /test/$dir/big_test.txt -o /test/$dir/test -v 3 -t 4 -k
#单独导出大表结构,无数据
cat /test/$dir/big_test.txt |while read line
do
mysqldump -h$db_IP -uroot -p$db_pass -d $db_name ${line#*.} >> /test/$dir/$line.sql
echo $line
done
echo "导出 测试库成功!"
*欢迎关注*
收录于合集 #项目运维 9个 上一篇网络基本常识总结 阅读 4008 数据与共享 64篇原创内容 写下你的留言 精选留言
- ℡._落oο 来自浙江 再来多点脚本~ 数据与共享 (作者) 可以,抽时间整理一下。