使用私有git仓库备份服务器脚本和配置文件

1. 创建私有git仓库

服务器端配置:
    # 安装 git
    yum -y install git

    # 创建 git 用户
    useradd git
    
    # 创建私有仓库数据存储目录
    mkdir /git_back/

    # 修改权限
    chown git:git /git_back/

    # 生成私有仓库 key
    su - git
    ssh-keygen
    cd .ssh
    cp id_rsa.pub authorized_keys

    # 初始化私有仓库
    cd /git_back/
    git init --bare gitserver

    # 修改 git 用户的登录 shell, 禁止其登录系统
    su - root
    usermod -s $(which git-shell) git

  

2. 配置客户端环境

[root@localhost]# cat init_back.sh 

#!/bin/bash
#描述: 初始化 git 客户端, 用于备份服务器上的脚本和配置文件到私有 git 仓库
#建议: 备份的文件总大小最好在 100M 以下


export PATH=$PATH:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin

# 客户端连接私有 git 仓库的用户
back_user='root'
# 客户端的备份目录
back_dir='/opt/git_back'
# 服务端 git 用户的私钥名称
key_name='id_dsa'



# 私有 git 仓库的地址
git_server='192.168.0.104'
# 私有 git 仓库的用户
git_server_user='git'
# 私有 git 仓库所在目录
git_server_dir='/git_back'
# 私有 git 仓库名
git_server_repertory='gitserver'
# 每一个主机创建一个目录(以内网 IP 命名), 当本台主机的所有备份放入该目录中
# 设置内网 IP 段
client_ip='192.168.0'



# git 基础配置
git_user_email='you@example.com'
git_user_name='Your Name'

if [ ! -f "${key_name}" ];then 
    echo "请将服务器端 key 放入当前目录" 
    exit 10
fi

which git &> /dev/null || yum -y install git &> /dev/null
which ifconfig &> /dev/null || yum -y install net-tools &> /dev/null

which git &> /dev/null
if [ ${?} -ne 0 ];then
    echo "无法安装 git 命令"
    exit 10
fi

which git &> /dev/null
if [ ${?} -ne 0 ];then
    echo "无法安装 ifconfig 命令"
    exit 10
fi


if [ ${back_user} == 'root' ];then
    [ -d '/root/.ssh/' ] || mkdir /root/.ssh/
    if [ ! -f "/root/.ssh/${key_name}" ];then 
        cp ${key_name} /root/.ssh/
        chmod 600 /root/.ssh/${key_name}
    else
        echo "key 已存在, 请手动更改key的名称和/etc/ssh/ssh_config"
        exit 1
    fi
else
    ssh_dir="/home/${back_user}/.ssh"
    [ -d ${ssh_dir} ] || mkdir ${ssh_dir}
    if [ ! -f "${ssh_dir}/${key_name}" ];then
        cp ${key_name} ${ssh_dir}
        chmod 600 ${ssh_dir}/${key_name}
    else
        echo "key 已存在, 请手动更改key的名称和/etc/ssh/ssh_config"
        exit 2
    fi 
fi 

if [ -d ${back_dir} ];then
    echo "备份目录已存在"
    exit 3
else
    mkdir ${back_dir}
fi

cd ${back_dir}
# 第一次连接, 自动将主机加入到 known_hosts, git 用户禁止登录系统
ssh git@192.168.0.104 -o StrictHostKeyChecking=no ifconfig &> /dev/null
git clone ${git_server_user}@${git_server}:${git_server_dir}/${git_server_repertory}

intranet_ip=$(ifconfig | grep "${client_ip}" | awk '{print $2}' | grep -o "${client_ip}.*")
cd ${back_dir}/${git_server_repertory}
if [ ! -d ${intranet_ip} ];then
    mkdir ${intranet_ip}
else
    echo "${intranet_ip} 目录已存在"
    exit 5
fi

git config --global user.email ${git_user_email}
git config --global user.name ${git_user_name}
git config --global push.default simple
git push --set-upstream origin master

 

3. 提交修改后的文件

#!/bin/bash
#描述: 每天提交一次修改过的文件


back_dir='/opt/git_back/gitserver/'
cd ${back_dir}
git pull &> /dev/null
git add -A &> /dev/null
git commit -m "$(date +%Y-%m-%d)" &> /dev/null && git push &> /dev/null

  

原文地址:https://www.cnblogs.com/huyuanblog/p/10071197.html