learn shell

2023-12-14 11:08:19

批量修改密码

#!/bin/bash

[ -f /etc/init.d/functions ] && . /etc/init.d/functions
OLD_INFO=old_pass.txt
NEW_INFO=new_pass.txt
# yum install -y sshpass
for IP in $(awk '/^[^#]/{print $1}' $OLD_INFO); do
    USER=$(awk -v I=$IP 'I==$1{print $2}' $OLD_INFO)
    PASS=$(awk -v I=$IP 'I==$1{print $3}' $OLD_INFO)
    PORT=$(awk -v I=$IP 'I==$1{print $4}' $OLD_INFO)
    NEW_PASS=$(openssl rand -base64 8)
    #NEW_PASS=1
    sshpass -p "$PASS" ssh -p $PORT $USER@$IP -o StrictHostKeyChecking=true "echo $NEW_PASS |passwd --stdin $USER" && \
    (
    echo "$IP $USER $NEW_PASS $PORT [`date '+%F %T'`]" >> $NEW_INFO && \
    action "配置成功 $USER $IP $NEW_PASS" /bin/true
    ) || \
    action "配置失败 $USER $IP $PASS" /bin/false
done

数据备份

mysql 物理备份

#!/usr/bin/bash

DBUSER=root
DBPASSWORD=123
BACKUPDIR=/dback
HISTLOG=/tmp/history.log
RULE=(0 1 2 1 2 2 2) # 0 full 1 diff 2 incr
DATE=$(date "+%Y-%m-%d_%H-%M-%S")
logdev=local2

full="innobackupex --user=$DBUSER --password=$DBPASSWORD $BACKUPDIR"
diff="innobackupex --user=$DBUSER --password=$DBPASSWORD --incremental $BACKUPDIR --incremental-basedir=${BACKUPDIR}/"

envCheck(){
    # 检查 MySQL 连接
    mysql -u$DBUSER -p$DBPASSWORD -e "select user()" &>/dev/null
    if [ $? -ne 0 ];then
        logger -p ${logdev}.error "MySQL 连接异常" && exit 2
    fi
    # 检查备份目录
    if [ ! -e $BACKUPDIR ];then
        logger -p ${logdev}.error "备份目录未正常初始化" && \
        mkdir $BACKUPDIR
    fi
}

work(){
    datetime=$[`date "+%u"`-1]
    case ${RULE[$datetime]} in
    0)
        # 移动历史备份文件日志
        mv $HISTLOG $BACKUPDIR/ && \
        # 打包历史备份文件
        tar cvzf /tmp/${DATE}.tar.gz $BACKUPDIR && \
        logger -p ${logdev}.info "历史备份文件打包成功 /tmp/${DATE}.tar.gz" && \
        # 删除原备份目录和日志文件,重新创建备份目录
        rm -rf $BACKUPDIR $HISTLOG && mkdir $BACKUPDIR || \
        logger -p ${logdev}.warn "历史备份文件打包异常 ${DATE}"
        # 执行完整备份
        eval $full && \
        # 记录完整备份日志
        echo "full `ls $BACKUPDIR |tail -n1`" >> $HISTLOG && \
        logger -p ${logdev}.info "完整备份完成 `ls $BACKUPDIR |tail -n1`" || \
        logger -p ${logdev}.error "完整备份异常 ${DATE}"
        ;;
    1)
        # 获取上一次完整备份的目录
        BEFOREDIR=$(awk '$1 ~ /full/{print $2}' $HISTLOG)
        # 执行差异备份
        eval $diff${BEFOREDIR} && \
        # 记录差异备份日志
        echo "diff `ls $BACKUPDIR |tail -n1`" >> $HISTLOG && \
        logger -p ${logdev}.info "差异备份完成 `ls $BACKUPDIR |tail -n1`" || \
        logger -p ${logdev}.error "差异备份异常 ${DATE}"
        ;;
    2)
        # 获取上一次备份的类型
        BEFOREDIR=$(tail -n1 $HISTLOG|awk '{print $2}')
        # 执行增量备份
        eval $diff${BEFOREDIR} && \
        # 记录增量备份日志
        echo "incr `ls $BACKUPDIR |tail -n1`" >> $HISTLOG && \
        logger -p ${logdev}.info "增量备份完成 `ls $BACKUPDIR |tail -n1`" || \
        logger -p ${logdev}.error "增量备份异常 ${DATE}"
        ;;
    *)
        echo "异常操作"
        ;;
    esac
}

# 调用函数
envCheck
work

mysql 物理备份2

#!/usr/bin/bash
<<!
mysql 物理备份脚本
工具:xtrabackup 2.3.6
备份策略:周日完整备份 周一、二、四、五增量备份 周三、六差异备份
1、完全后台执行
2、备份完整
3、日志记录
4、历史数据的处理
!

dbUser="root"
dbPasswd="QianFeng@123"
backCMD="/usr/bin/innobackupex"
backDir="/backup"
logPath="/var/log/backup.log"
# 定义规则数组 0 完整备份 1 增量备份 2 差异备份
# 数组的值是备份的方式,数组的下标是星期几
rule=(0 1 1 2 1 1 2)
#     0 1 2 3 4 5 6
datetime=$(date +%w)

full(){
    # 移动之前的日志
    mv $logPath $backDir
    # 压缩之前的备份
    tar cvzf /tmp/mysql_back_`date +%F`.tar.gz "$backDir" && \
    rm -rf $backDir
    # 创建备份目录结构
    mkdir -p $backDir/{full,incr,diff}
    # 执行完整备份
    $backCMD --user=$dbUser --password=$dbPasswd $backDir/full
    # 记录备份日志
    echo "full `ls $backDir/full |tail -n 1`" >> $logPath
}

other(){
    # $1 incr|diff 备份的类型
    # $2 需要基于的完整路径
    $backCMD --user=$dbUser --password=$dbPasswd --incremental $backDir/$1 --incremental-basedir=$2
    # 记录备份日志
    echo "$3 `ls $backDir/$3 |tail -n 1`" >> $logPath
}

case ${rule[$datetime]} in
0)
    full 
    ;;
1)
    # 获取上一次备份的类型
    type=$(tail -n 1 $logPath|awk '{print $1}')
    # 获取上一次备份的目录
    time=$(tail -n 1 $logPath|awk '{print $2}')
    # 执行增量备份
    other incr $backDir/$type/$time incr
    ;;
2)
    # 获取上一次完整备份的目录
    time=$(grep full $logPath|awk '{print $2}')
    # 执行差异备份
    other diff $backDir/full/$time diff
    ;;
esac

redis 数据备份?

#!/bin/bash

# Redis 数据备份脚本

# Redis 服务器地址和端口
REDIS_HOST="localhost"
REDIS_PORT="6379"

# 备份保存路径
BACKUP_DIR="/path/to/backup"

# 备份文件名前缀
BACKUP_PREFIX="redis_backup"

# 获取当前日期
DATE=$(date +"%Y%m%d_%H%M%S")

# 创建备份目录
mkdir -p "$BACKUP_DIR"

# 备份 Redis 数据
redis-cli -h $REDIS_HOST -p $REDIS_PORT SAVE

# 将备份文件保存到指定目录
BACKUP_FILE="$BACKUP_DIR/$BACKUP_PREFIX-$DATE.rdb"
mv dump.rdb "$BACKUP_FILE"

# 清理过期备份,只保留最近 N 天的备份
DAYS_TO_KEEP=7
find $BACKUP_DIR -type f -name "$BACKUP_PREFIX*" -mtime +$DAYS_TO_KEEP -exec rm {} \;

echo "Redis 数据备份完成,备份文件保存在: $BACKUP_FILE"

elasticsearch 数据备份

#!/bin/bash

# Elasticsearch 快照备份脚本

# Elasticsearch 服务器信息
ES_HOST="localhost"
ES_PORT="9200"
ES_USER="es"
ES_PASSWORD="123"

# 快照存储库设置
REPO_NAME="hello"
SNAPSHOT_NAME="snapshot_$(date +%Y%m%d%H%M%S)"

# 备份目录
BACKUP_DIR="/opt/es/backup"

# 日志文件
LOG_FILE="/var/log/es_snapshot_backup.log"

# 如果目录不存在,则创建备份目录
mkdir -p "$BACKUP_DIR"

# Elasticsearch 快照 API URL
SNAPSHOT_API_URL="http://$ES_HOST:$ES_PORT/_snapshot/$REPO_NAME/$SNAPSHOT_NAME"

# 记录日志消息的函数
log() {
    echo "$(date +'%Y-%m-%d %H:%M:%S') - $1" >> "$LOG_FILE"
}

# 检查 Elasticsearch 是否可访问
curl -s -k --user "$ES_USER:$ES_PASSWORD" "$ES_HOST:$ES_PORT" >/dev/null
if [ $? -ne 0 ]; then
    log "错误:无法连接到 Elasticsearch,请检查 Elasticsearch 设置。"
    exit 1
fi

# 创建快照
log "创建 Elasticsearch 快照:$SNAPSHOT_NAME"
curl -s -k --user "$ES_USER:$ES_PASSWORD" -X PUT "$SNAPSHOT_API_URL" >> "$LOG_FILE" 2>&1

# 检查快照是否成功创建
if [ $? -eq 0 ]; then
    log "快照创建成功。"
    
    # 将快照移动到备份目录
    log "将快照移动到备份目录:$BACKUP_DIR"
    mv "$SNAPSHOT_NAME" "$BACKUP_DIR/"
    
    log "备份完成。快照存储在:$BACKUP_DIR/$SNAPSHOT_NAME"
else
    log "错误:快照创建失败,请检查 Elasticsearch 日志以获取详细信息。"
fi

etcd 数据备份?

#!/bin/bash

# etcd 数据库备份脚本

# etcd 集群连接信息
ETCD_ENDPOINTS="http://127.0.0.1:2379"

# 备份目录
BACKUP_DIR="/opt/etcd/backup"

# 备份文件名称
BACKUP_FILE="etcd_backup_$(date +'%Y%m%d%H%M%S').db"

# 日志文件
LOG_FILE="/var/log/etcd_backup.log"

# 创建备份目录
mkdir -p "$BACKUP_DIR"

# 记录日志消息的函数
log() {
    echo "$(date +'%Y-%m-%d %H:%M:%S') - $1" >> "$LOG_FILE"
}

# 检查 etcdctl 是否可用
if ! command -v etcdctl >/dev/null; then
    log "错误:etcdctl 未找到,请确保已安装 etcdctl。"
    exit 1
fi

# 检查 etcd 连接
etcdctl --endpoints="$ETCD_ENDPOINTS" endpoint health &>/dev/null
if [ $? -ne 0 ]; then
    log "错误:无法连接到 etcd,请检查 etcd 设置。"
    exit 1
fi

# 备份 etcd 数据库
log "开始备份 etcd 数据库到目录:$BACKUP_DIR"
etcdctl --endpoints="$ETCD_ENDPOINTS" snapshot save "$BACKUP_DIR/$BACKUP_FILE" &>> "$LOG_FILE"

# 检查备份是否成功
if [ $? -eq 0 ]; then
    log "etcd 数据库备份成功。备份文件存储在:$BACKUP_DIR/$BACKUP_FILE"
else
    log "错误:etcd 数据库备份失败,请检查 etcdctl 输出以获取详细信息。"
fi

日志备份

#!/bin/bash

# 备份 /var/log/ 目录下所有10天之前的日志到 /tmp/logbak 目录中,并打包压缩

# 备份源目录
SOURCE_DIR="/var/log/"

# 目标备份目录
BACKUP_DIR="/tmp/logbak/"

# 备份文件名
BACKUP_FILE="log_backup_$(date +'%Y%m%d%H%M%S').tar.gz"

# 保留备份天数
RETENTION_DAYS=7

# 创建备份目录
mkdir -p "$BACKUP_DIR"

# 记录日志
LOG_FILE="/var/log/log_backup.log"

# 清理过期备份函数
cleanup_old_backups() {
    find "$BACKUP_DIR" -name "log_backup_*" -type f -mtime +$RETENTION_DAYS -exec rm {} \;
}

# 备份操作
backup_logs() {
    log_files=$(find "$SOURCE_DIR" -type f -mtime +$RETENTION_DAYS)

    if [ -z "$log_files" ]; then
        echo "没有需要备份的日志文件。"
        exit 0
    fi

    tar czf "$BACKUP_DIR/$BACKUP_FILE" $log_files
}

# 执行备份
backup_logs

# 清理过期备份
cleanup_old_backups

# 记录日志
echo "$(date +'%Y-%m-%d %H:%M:%S') - 日志备份完成。备份文件:$BACKUP_DIR/$BACKUP_FILE" >> "$LOG_FILE"

gitlab 数据备份?

#!/bin/bash

# 备份 GitLab 仓库数据脚本

# GitLab 安装路径
GITLAB_PATH="/var/opt/gitlab"

# 备份目标路径
BACKUP_DIR="/tmp/gitlab_backup"

# GitLab 备份工具路径
GITLAB_BACKUP_TOOL="$GITLAB_PATH/bin/gitlab-rake gitlab:backup:create"

# 备份文件名
BACKUP_FILE="gitlab_backup_$(date +'%Y%m%d%H%M%S').tar.gz"

# 保留备份天数
RETENTION_DAYS=7

# 创建备份目录
mkdir -p "$BACKUP_DIR"

# 清理过期备份函数
cleanup_old_backups() {
    find "$BACKUP_DIR" -name "gitlab_backup_*" -type f -mtime +$RETENTION_DAYS -exec rm {} \;
}

# 备份操作
backup_gitlab() {
    $GITLAB_BACKUP_TOOL
}

# 执行备份
backup_gitlab

# 清理过期备份
cleanup_old_backups

# 压缩备份文件
tar czf "$BACKUP_DIR/$BACKUP_FILE" "$GITLAB_PATH/backups"

# 记录日志
LOG_FILE="/var/log/gitlab_backup.log"
echo "$(date +'%Y-%m-%d %H:%M:%S') - GitLab 备份完成。备份文件:$BACKUP_DIR/$BACKUP_FILE" >> "$LOG_FILE"

docker 镜像迁移?

手动迁移

#!/bin/bash

TAG_PREFIX=

save(){
    if [ ! -d "./images" ]; then
        mkdir images
    fi
    cd images
    docker images --format "{{.ID}} {{.Repository}}:{{.Tag}} {{.Size}}" |grep $TAG_PREFIX > images_pull.txt
    while read line
    do
        image_id=`echo $line | awk '{print $1}'`
        image_repository=`echo $line | awk '{print $2}'`
        image_size=`echo $line | awk '{print $3}'`
        docker save -o $image_id.tar $image_repository && \
        echo "Image $image_repository saved, size $image_size"
    done < images_pull.txt
}

load(){
    cd images
    while read line
    do
        image_id=`echo $line | awk '{print $1}'`
        image_repository=`echo $line | awk '{print $2}'`
        docker load -i $image_id.tar && \
        echo "Image $image_repository loaded"
    done < images_pull.txt
}
if [ -z "$1" ]; then
    echo "Usage: image_operation.sh [save|load]"
fi

eval $1

系统初始化?

centos 7

#!/usr/bin/bash

#set -e

yum_host=10.36.172.9
yum_host2=10.36.174.100
color(){
    unset c
    declare -A c=([Error]=31 [Success]=32 [Warning]=33 [Info]=34)
    #echo -e "\033[${c[$1]}m[`date +%T`]($1)  $2\033[0m"
    printf "\033[${c[$1]}m%-10s%-10s %-30s\033[0m\n" "[`date +%T`]" "($1)" "$2"
    sleep 0.5
}

static_addr(){
ifname=$(ip -f inet a | awk '/^2/{print $2}')
ifname=$(echo ${ifname/:/})
ipaddr=$(ip -f inet a show dev $ifname | awk '/inet/{print $2}' | awk -F'/' '{print $1}')
prefix=$(ip -f inet a show dev $ifname | awk '/inet/{print $2}' | awk -F'/' '{print $2}')
gateway=$(ip r |awk '/default/{print $3}')
cat > /etc/sysconfig/network-scripts/ifcfg-$ifname <<EOF
TYPE="Ethernet"
PROXY_METHOD="none"
BROWSER_ONLY="no"
BOOTPROTO="static"
DEFROUTE="yes"
IPV4_FAILURE_FATAL="no"
NAME="${ifname}"
UUID="`uuidgen`"
DEVICE="${ifname}"
ONBOOT="yes"
IPADDR=${ipaddr}
PREFIX=${prefix}
GATEWAY=${gateway}
DNS1=${gateway}
DNS2=114.114.114.114
DNS3=8.8.8.8
EOF
systemctl restart network
ping -w2 -c2 www.baidu.com &>/dev/null && \
return 10 || \
return 20
}

local_yum_make(){
    cd /etc/yum.repos.d/ && \
    rename .repo .repo.bak *.repo &>/dev/null
    grep "$yum_host package.qf.com" /etc/hosts &>/dev/null
    if [ $? -ne 0 ];then
    cat >> /etc/hosts <<EOF
$yum_host package.qf.com
$yum_host2 package2.qf.com
EOF
    fi
    curl -o /etc/yum.repos.d/centos7.repo https://download.beyourself.org.cn/repo/centos7-repo && \
    yum repolist && yum clean all && yum makecache && \
    color Success "本地源配置完成" || color Error "yum配置错误,可以\ncd /etc/yum.repos.d/ && rm -rf centos7.repo && rename .repo.bak .repo *.repo.bak \n使用手动恢复历史yum"
}

remote_yum_make(){
    cd /etc/yum.repos.d/ && \
    rename .repo .repo.bak *.repo &>/dev/null
    curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo
    curl -o /etc/yum.repos.d/epel.repo https://mirrors.aliyun.com/repo/epel-7.repo
    yum repolist && yum clean all && yum makecache && \
    color Success "阿里源配置完成" || color Error "yum配置错误,可以\ncd /etc/yum.repos.d/ && rm -rf centos7.repo && rename .repo.bak .repo *.repo.bak \n使用手动恢复历史yum"
}

init(){
    # 系统信息采集
    color Info "当前用户 $USER"
    color Info "当前时间 `date "+%F %X"`"
    echo 3 >/proc/sys/vm/drop_caches
    color Info "当前内存 $(free |awk 'NR==2{print $4/1024}')M"
    color Info "当前核心 $(grep -E '^processor' /proc/cpuinfo|wc -l)个"
    color Info "核心品牌 $(grep -E '^model name' /proc/cpuinfo |head -n 1 |awk -F: '{print $2}')"
    color Info "启动时长 $(uptime |awk -F',' '{print $1}')"
    color Success "开始配置静态ip"
cat >>/etc/security/limits.conf <<EOF
* soft nofile 65535
* hard nofile 65535
* soft nproc 1024
* hard nproc 1024
EOF
    color Success "已经优化系统文件打开数、进程打开数"
echo "net.ipv4.ip_forward=1" >> /etc/sysctl.conf
    color Success "已经开启系统路由转发"
sed -i 's/#UseDNS.*/UseDNS no/' /etc/ssh/sshd_config
    color Success "已经优化ssh服务连接速度"
    static_addr 
    if [ $? -eq 10 ];then
        color Success "静态ip配置并检测完成"
    else
        color Error  "静态ip配置异常"
    fi
    color Info "正在关闭防火墙、selinux"
    color Warning "注意,这将降低服务器安全性"
    systemctl disable firewalld postfix --now
    if [ `getenforce` = 'Enforcing' ];then
        setenforce 0
    fi
    sed -i 's/SELINUX=.*/SELINUX=disabled/g' /etc/selinux/config && \
    color Success "配置完成" || color Error "配置异常,请检查"
    color Info "配置yum源, 此操作将备份您的历史源"
    ping -w 2 -c 2 $yum_host &>/dev/null
    if [ $? -eq 0 ];then
        local_yum_make
    else
        remote_yum_make
    fi
    color Info "开始安装常用软件"
    yum install -y wget unzip vim yum-utils httpd-tools psacct net-tools nc ntpdate jq htop bash-completion && \
    color Success "安装完成" || color Warning "部分包可能失败"
    color Info "开始进行时间校准"
    ntpdate ntp.aliyun.com && \
    # 修改时间到硬件
    clock -w
    #保存时间到硬件
    hwclock -s
    color Success "时间校准完成"
    # color Info "修改终端前缀显示"
    # cat > /etc/profile.d/psdiy.sh <<EOF
    # export PS1="[\e[32m(\t)\u\e[36m@\h\e[31m\e[0m \W]\$ "
# EOF
    # color Success "修改完成,重新进入终端查看"
    echo {"time": "`date +'%F %T'`"} > /tmp/.init
}
if [ -f /tmp/.init ];then
    color Success "检测到此前已进行初始化,如需重新执行,请删除: rm -f /tmp/.init"
    exit 0
fi
init
color Success "初始化完成"

centos 9?

#!/usr/bin/bash

set -e
color(){
    unset c
    declare -A c=([Error]=31 [Success]=32 [Warning]=33 [Info]=34)
    printf "\033[${c[$1]}m%-10s%-10s %-30s\033[0m\n" "[`date +%T`]" "($1)" "$2"
    sleep 0.5
}

static_addr(){
ifname=$(ip -f inet a | awk '/^2/{print $2}')
ifname=$(echo ${ifname/:/})
ipaddr=$(ip -f inet a show dev $ifname | awk '/inet/{print $2}' | awk -F'/' '{print $1}')
prefix=$(ip -f inet a show dev $ifname | awk '/inet/{print $2}' | awk -F'/' '{print $2}')
gateway=$(ip r |awk '/default/{print $3}')
cat > /etc/sysconfig/network-scripts/ifcfg-$ifname <<EOF
[connection]
id=${ifname}
uuid=`uuidgen`
type=ethernet
autoconnect-priority=-999
interface-name=${ifname}
timestamp=1676740817

[ethernet]

[ipv4]
method=manual
address1=${ipaddr}/${prefix},${gateway}
dns=223.5.5.5

[ipv6]
method=auto
addr-gen-mode=eui64

[proxy]

EOF
nmcli c reload
nmcli c up ${ifname}
ping -w1 -c1 www.baidu.com &>/dev/null && \
return 10 || \
return 20
}


init(){
    # 系统信息采集
    color Info "当前用户 $USER"
    color Info "当前时间 `date "+%F %X"`"
    echo 3 >/proc/sys/vm/drop_caches
    color Info "当前内存 $(free |awk 'NR==2{print $4/1024}')M"
    color Info "当前核心 $(grep -E '^processor' /proc/cpuinfo|wc -l)个"
    color Info "核心品牌 $(grep -E '^model name' /proc/cpuinfo |head -n 1 |awk -F: '{print $2}')"
    color Info "启动时长 $(uptime |awk -F',' '{print $1}')"
    color Success "开始配置静态ip"
cat >>/etc/security/limits.conf <<EOF
* soft nofile 65535
* hard nofile 65535
* soft nproc 1024
* hard nproc 1024
EOF
    color Success "已经优化系统文件打开数、进程打开数"
    echo "net.ipv4.ip_forward=1" >> /etc/sysctl.conf
    color Success "已经开启系统路由转发"
    static_addr 
    if [ $? -eq 10 ];then
        color Success "静态ip配置并检测完成"
    else
        color Error  "静态ip配置异常"
    fi
    color Info "正在关闭防火墙、selinux"
    color Warning "注意,这将降低服务器安全性"
    systemctl disable firewalld --now
    if [ `getenforce` = 'Enforcing' ];then
        setenforce 0
    fi
    sed -i 's/SELINUX=.*/SELINUX=disabled/g' /etc/selinux/config && \
    color Success "配置完成" || color Error "配置异常,请检查"
    dnf install -y epel-release
    dnf makecache
    dnf install -y wget unzip vim yum-utils httpd-tools psacct net-tools nc jq htop bash-completion lrzsz ntpsec && \
    color Success "安装完成" || color Warning "部分包可能失败"
    color Info "开始进行时间校准"
    ntpdate ntp.aliyun.com && \
    # 修改时间到硬件
    clock -w
    #保存时间到硬件
    hwclock -s
    color Success "时间校准完成"
    echo {"time": "`date +'%F %T'`"} > /tmp/.init
}
if [ -f /tmp/.init ];then
    color Success "检测到此前已进行初始化,如需重新执行,请删除: rm -f /tmp/.init"
    exit 0
fi
init
color Success "初始化完成"

ubuntu 20.04?

#!/bin/bash

# Ubuntu 20.04 System Initialization Script

color() {
    declare -A c=([Error]=31 [Success]=32 [Warning]=33 [Info]=34)
    printf "\033[${c[$1]}m%-10s%-10s %-30s\033[0m\n" "[`date +%T`]" "($1)" "$2"
    sleep 0.5
}

static_addr() {
    ifname=$(ip -f inet a | awk '/^2/{print $2}')
    ifname=$(echo ${ifname/:/})
    ipaddr=$(ip -f inet a show dev $ifname | awk '/inet/{print $2}' | awk -F'/' '{print $1}')
    prefix=$(ip -f inet a show dev $ifname | awk '/inet/{print $2}' | awk -F'/' '{print $2}')
    gateway=$(ip r |awk '/default/{print $3}')

    cat > /etc/netplan/01-network-manager-all.yaml <<EOF
network:
  version: 2
  renderer: networkd
  ethernets:
    ${ifname}:
      addresses:
        - ${ipaddr}/${prefix}
      gateway4: ${gateway}
      nameservers:
        addresses:
          - ${gateway}
          - 114.114.114.114
          - 8.8.8.8
EOF

    netplan apply
    ping -w2 -c2 www.ubuntu.com &>/dev/null && return 10 || return 20
}

init() {
    color Info "当前用户 $USER"
    color Info "当前时间 `date "+%F %X"`"
    echo 3 >/proc/sys/vm/drop_caches
    color Info "当前内存 $(free -m | awk 'NR==2{print $4}')M"
    color Info "当前核心 $(nproc)个"
    color Info "启动时长 $(uptime -p)"
    color Success "开始配置静态ip"
    
    static_addr 
    if [ $? -eq 10 ];then
        color Success "静态ip配置并检测完成"
    else
        color Error  "静态ip配置异常"
    fi

    color Info "正在关闭防火墙、apparmor"
    systemctl disable ufw --now
    systemctl disable apparmor --now

    color Info "配置yum源, 此操作将备份您的历史源"
    apt-get update && apt-get upgrade -y
    color Success "本地源配置完成"

    color Info "开始安装常用软件"
    apt-get install -y wget unzip vim net-tools ntpdate jq htop bash-completion

    color Info "开始进行时间校准"
    ntpdate ntp.aliyun.com
    color Success "时间校准完成"
}

init
color Success "初始化完成"

服务部署

安装docker

#!/usr/bin/bash

set -e
unset c
color(){
    declare -A c=([Error]=31 [Success]=32 [Warning]=33 [Info]=34)
    #echo -e "\033[${c[$1]}m[`date +%T`]($1)  $2\033[0m"
    printf "\033[${c[$1]}m%-10s%-10s %-30s\033[0m\n" "[`date +%T`]" "($1)" "$2"
    sleep 0.5
}

install(){
    color Info "开始安装docker服务"
    yum install -y yum-utils device-mapper-persistent-data lvm2 git && \
    yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo && \
    yum install -y docker-ce
	mkdir -p /etc/docker
    color Success "安装完成"
    color Info "开始配置镜像加速器"
	tee /etc/docker/daemon.json <<-'EOF'
{
  "registry-mirrors": ["https://pilvpemn.mirror.aliyuncs.com"]
}
EOF
    cat >> /etc/sysctl.conf <<EOF
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
net.ipv4.ip_forward=1
EOF
    systemctl daemon-reload && \
    systemctl enable docker --now
}
install
color Success "配置完成"

安装mysql?

#!/bin/bash
# by newrain
# time 2019-11-28
# lv 2.0
level=5.7
password="QianFeng@123"
remote_user="remote"
remote_passwd="QianFeng@123"
unset c
color(){
    declare -A c=([Error]=31 [Success]=32 [Warning]=33 [Info]=34)
    #echo -e "\033[${c[$1]}m[`date +%T`]($1)  $2\033[0m"
    printf "\033[${c[$1]}m%-10s%-10s %-30s\033[0m\n" "[`date +%T`]" "($1)" "$2"
	sleep 0.5
}

color Info "此脚本用于yum 安装mysql"
color Warning "此脚本将删除所有mysql数据,如需备份请ctrl+c终止脚本,程序将在3秒后执行"
sleep 3
if [[ $UID -ne 0 ]];then
	color Warning "使用root 执行此脚本"
	exit 1
fi

color Warning "清理环境"
systemctl stop mysqld mariadb &>/dev/null
yum erase -y `rpm -qa |grep mariadb` 2>/dev/null
yum erase -y `rpm -qa |grep mysql`  2>/dev/null
rm -rvf /etc/my.cnf /var/lib/mysql /var/log/mysql*
userdel -rf mysql &>/dev/null
ping -c1 -w1 www.baidu.com &>/dev/null
if [[ $? -eq 0 ]];then
	if [ ! -f /tmp/.init ];then
		yum install -y https://dev.mysql.com/get/mysql80-community-release-el7-1.noarch.rpm
	fi
else
	color Error "网络错误"
	exit 22
fi
yum -y install mysql-community-server mysql-community-devel --disablerepo mysql80-community --enablerepo mysql57-community --nogpgcheck
systemctl start mysqld
color Warning "启动成功,初始密码如下(mysql5.7前版本没有初始密码)"
passwd=$(grep -o 'root@localhost.*' /var/log/mysqld.log | awk 'END{print $NF}')
mysqladmin -uroot -p$passwd password $password && \
mysql -uroot -p$password -e "grant all on *.* to \"$remote_user\"@'%' identified by \"$remote_passwd\"; flush privileges;"
color Success "安装完成 初始密码为$password"

安装python3.10?

#!/usr/bin/bash

path=/opt/py3104
unset c
color(){
    declare -A c=([Error]=31 [Success]=32 [Warning]=33 [Info]=34)
    #echo -e "\033[${c[$1]}m[`date +%T`]($1)  $2\033[0m"
    printf "\033[${c[$1]}m%-10s%-10s %-30s\033[0m\n" "[`date +%T`]" "($1)" "$2"
    sleep 0.5
}

mkdir -pv $path
cd $path && \
yum -y install gcc gcc-c++ zlib-devel bzip2-devel  sqlite-devel readline-devel libffi-devel && \
color Info "openssl 包下载,请稍等..." && \
wget http://download.beyourself.org.cn/package/openssl-1.1.1n.tar.gz && \
tar xf openssl-1.1.1n.tar.gz && \
cd openssl-1.1.1n && \
./config --prefix=/usr/local/openssl  && \
make -j $(cat /proc/cpuinfo | grep processor |wc -l) && make install && cd .. && \
wget http://download.beyourself.org.cn/package/Python-3.10.4.tar.xz && \
tar xf Python-3.10.4.tar.xz && \
cd Python-3.10.4 && \
./configure --enable-shared  --prefix=/usr/local/python3.10.4 --with-openssl=/usr/local/openssl --with-openssl-rpath=auto && \
make -j $(cat /proc/cpuinfo | grep processor |wc -l) && \
make install && \
echo "/usr/local/python3.10.4/lib" >> /etc/ld.so.conf && \
ldconfig && \
echo "export PATH=/usr/local/python3.10.4/bin:\$PATH" >> /etc/profile && \
color Success "python安装完成"

color Info "配置pip加速"
mkdir ~/.pip &>/dev/null
tee ~/.pip/pip.conf <<EOF
[global]
index_url=https://mirrors.aliyun.com/pypi/simple
timeout=6000
EOF
color Success "配置完成" 

安装mysql[编译]?

#!/usr/bin/bash

source ./color.sh
source ./checkos.sh
source ./init.sh
# 数据库安装目录
install_dir=/usr/local/mysql
# 数据存放目录
data_dir=${install_dir}/data
# 配置文件存放目录
config_dir=/etc
# 解压目录名称
source_dir=mysql-5.7.27
# 默认端口
port=3306
# 源码包名称
packagename="mysql-boost-5.7.27.tar.gz"
# 编译线程数
cpus=`grep "processor" /proc/cpuinfo | wc -l`

checkEnv(){
    [ -d /var/lib/mysql ] && color yellow "检测到本机可能使用yum安装了mysql,可能会造成冲突,请问是否继续安装[Y|N]"
    read result
    if [ "$result" = 'Y' ];then
        color green "安装继续"
    else
        color green "退出脚本"
        exit 1
    fi
    [ -d /usr/local/mysql ] && color red "检测到${install_dir}已存在,请手动处理" && exit 1 || \
    color green "环境检测完成"
}

installPackage(){
    yum -y install ncurses ncurses-devel openssl-devel bison gcc gcc-c++ make glibc automake autoconf cmake && \
    color green "依赖包安装完成" || \
    color red "依赖包安装异常,请修复yum源[需要官方yum以及epelyum]"
    color green "开始准备源码包"
    sed -n -e '1,/^exit 0$/!p' $0 > ./$packagename
    tar xvf $packagename && \
    color green "源码包准备完成" && \
    cd $source_dir/
    cmake . \
    -DWITH_BOOST=boost/boost_1_59_0/ \
    -DCMAKE_INSTALL_PREFIX=$install_dir \
    -DSYSCONFDIR=$config_dir \
    -DMYSQL_DATADIR=$data_dir \
    -DINSTALL_MANDIR=/usr/share/man \
    -DMYSQL_TCP_PORT=$port \
    -DMYSQL_UNIX_ADDR=/tmp/mysql.sock \
    -DDEFAULT_CHARSET=utf8 \
    -DEXTRA_CHARSETS=all \
    -DDEFAULT_COLLATION=utf8_general_ci \
    -DWITH_READLINE=1 \
    -DWITH_SSL=system \
    -DWITH_EMBEDDED_SERVER=1 \
    -DENABLED_LOCAL_INFILE=1 \
    -DWITH_INNOBASE_STORAGE_ENGINE=1 && \
    make -j $cpus && make install && \
    color green "编译安装完成"
}

down(){
    cat >> /etc/profile.d/source_mysql.sh <<\EOF
    PATH=/usr/local/mysql/bin:$PATH
    export PATH
EOF
    password=$(${install_dir}/bin/mysqld --initialize --user=mysql --basedir=$install_dir --datadir=$data_dir 2>&1 | grep "A temporary password" | awk '{print $NF}')
    if [ -n "$password" ];then
        color green "初始化完成,初始密码为$password"
        color green "通过mysqladmin -p$password password '新密码'进行修改密码"
        color green "启动数据库 mysqld_safe &\nmysql 安装完成"
    else
        color red   "初始化异常,请手动检查"
    fi
    read -p '是否需要清理安装环境[Y|N]: ' ask
    if [ "$ask" = 'Y' ];then
        rm -rvf $source_dir
        rm -rvf $packagename
        color green "清理完成"
    else
        color green "脚本退出"
    fi
}


main(){
    checkEnv
    installPackage
    down
}
main
exit 0

k8s终端指令补全

#!/usr/bin/bash

yum -y install bash-completion
source /usr/share/bash-completion/bash_completion
echo 'source <(kubectl completion bash)' >> ~/.bashrc
echo '退出终端后重新进入既可'

?

?修改历史命令

#!/usr/bin/env bash
# history
# 获取登录用户的IP地址
USER_IP=$(env | grep SSH_CLIENT | awk '{print $1}' | sed 's/SSH_CLIENT=//' | awk -F " " '{print $1}')
# 如果无法获取用户IP,使用主机名作为标识
if [ -z $USER_IP ]; then
    USER_IP=$(hostname)
fi
# 指定历史命令保存路径
HISTDIR=/usr/share/.history
# 如果历史命令路径不存在,则创建
if [ ! -d $HISTDIR ]; then
    mkdir -p $HISTDIR
    chmod 777 $HISTDIR
fi
# 每个用户创建一个子目录保存历史命令
if [ ! -d $HISTDIR/${LOGNAME} ]; then
    mkdir -p $HISTDIR/${LOGNAME}
    chmod 300 $HISTDIR/${LOGNAME}
fi
# 设置历史命令保存条数
export HISTSIZE=4000
# 设置历史命令文件名后缀为当前日期时间
DT=$(date +%Y%m%d_%H%M%S)
export HISTFILE="$HISTDIR/${LOGNAME}/${USER_IP}.history.$DT"  # 文件路径
# 设置历史命令记录的时间格式
export HISTTIMEFORMAT="[%Y.%m.%d %H:%M:%S] "

# 设置历史命令文件权限
chmod 600 $HISTDIR/${LOGNAME}/*.history* 2>/dev/null


?


?

?

?

文章来源:https://blog.csdn.net/2301_76529529/article/details/134986659
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。