rsync主备同步服务脚本

服务端(备份机器)rsync --daemon 守护进程

vim /data/auto_back/start_rsyncd.sh

#!/bin/sh
mobile='******'
IP='192.168.143.144'
pid_file='/etc/rsyncd/rsyncd.pid'
rsync_start_log='/data/log/start_rsyncd.log'
sms_url='http://test.sms.uu.cc:8081/SMS/sa/sms/send'
rsync_start='rsync --daemon --config=/etc/rsyncd/rsyncd.conf'
rsync_status=$(ps -ef | egrep "rsync --daemon.*rsyncd.conf" | grep -v 'grep')

if [ "${rsync_status}" == "" ]
then
   rm -f ${pid_file}
   ${rsync_start}
   if [ $? == 0 ]
   then
      w_time=`date "+%Y-%m-%d %H:%M:%S"` 
      content="${w_time} | gitlab备份机器(IP:${IP}) | rsyncd --daemon 重启成功"
   else
      w_time=`date "+%Y-%m-%d %H:%M:%S"`
      content="${w_time} | gitlab备份机器(IP:${IP}) | rsyncd --daemon 重启失败"
   fi
   echo ${content} >> ${rsync_start_log}
   curl -d "mobile=${mobile}&content=${content}" ${sms_url} -s >/dev/null
else
   echo '已经启动' >/dev/null
fi

添加定时任务:
crontab -e
*/1 * * * * /usr/bin/sh /data/auto_back/start_rsyncd.sh

客户端(线上机器)自动备份到备份机器脚本

vim /data/auto_back/rsyncd.sh

#!/bin/sh
mobile='******'
IP='192.168.143.36'
log_date=`date "+%Y-%m"`
sms_url='http://test.sms.uu.cc:8081/SMS/sa/sms/send'
error_log_file="/data/log/${log_date}_error_log.txt"
success_log_file="/data/log/${log_date}_success_log.txt"

#检测命令执行是否成功函数,$1 执行命令  $2 命令功能描述
function check() {
   $1 1>/dev/null 2>>${error_log_file}
   if [ $? == 0 ]
   then
      w_time=`date "+%Y-%m-%d %H:%M:%S"`
      echo "${w_time} | 成功 | $2">> ${success_log_file}
   else
      w_time=`date "+%Y-%m-%d %H:%M:%S"`
      content="${w_time} | 失败 | $2"
      echo ${content}>> ${error_log_file}
      echo >> ${error_log_file}
      curl -d "mobile=${mobile}&content=gitlab线上服务器(IP:${IP}):${content}" ${sms_url} -s >/dev/null
      exit
   fi
}

#将gitlab配置文件 /etc/gitlab/ 移动到上传 /data/gitlab/conf
log_info="配置文件:/etc/gitlab/ 复制到 /data/gitlab/conf/"
conf_rsync='rsync -av /etc/gitlab/ /data/gitlab/conf/'
check "${conf_rsync}" "${log_info}"


#执行gitlab本地备份操作
log_info="gitlab本地备份除版本库信息以外的备份文件"
gitlab_backups='gitlab-rake gitlab:backup:create SKIP=repositories CRON=1'
check "${gitlab_backups}" "${log_info}"


#执行 rsync 上传同步到备份服务器
log_info="采用rsync将本地的备份数据同步到远程的备份服务器上"
backup_rsync='rsync -avzP--delete --progress --port=873 --password-file=/etc/rsyncd/rsyncd.secrets /data/gitlab/ [email protected]::backup'
check "${backup_rsync}" "${log_info}"


#所有操作成功执行后短信通知
echo >> ${success_log_file}
w_time=`date "+%Y-%m-%d %H:%M:%S"`
curl -d "mobile=${mobile}&content=gitlab线上服务器(IP:${IP}):${w_time} | 同步到备份服务器成功!" ${sms_url} -s >/dev/null

添加定时任务:
crontab -e
30 2 * * * /usr/bin/sh /data/auto_back/rsyncd.sh

服务端(备份机器)从同步过来的备份文件恢复

/data/auto_back/gitlab_backup.sh

#!/bin/sh
mobile='******'
IP='192.168.143.144'
log_file='/data/log/gitlab_back.log'
sms_url='http://test.sms.uu.cc:8081/SMS/sa/sms/send'
backup_id=`ls -lt /data/gitlab/backups |awk 'NR==2{print $9}'|grep -o ".*10.1.0"`
gitlab_backup="gitlab-rake gitlab:backup:restore BACKUP=${backup_id}"

#检查备份服务配置重启、gitlab重启是否成功
function check() {
   if [ $1 != 0 ]
   then 
      w_time=`date "+%Y-%m-%d %H:%M:%S"`
      content="${w_time} | $2"
      echo ${content} >> ${log_file}
      curl -d "mobile=${mobile}&content=gitlab备份服务器(IP:${IP}):${content}" ${sms_url} -s >/dev/null
      exit
   fi
}

w_time=`date "+%Y-%m-%d %H:%M:%S"`
echo "备份恢复开始时间:${w_time}" >> ${log_file}
gitlab-ctl stop unicorn >> ${log_file}
gitlab-ctl stop sidekiq >> ${log_file}
rsync -av /data/gitlab/conf/gitlab-secrets.json /etc/gitlab/gitlab-secrets.json > /dev/null

#执行expect代码 
/usr/bin/expect << EOF
set timeout -1
log_user 0
log_file $log_file
spawn $gitlab_backup
expect {
    "(yes/no)?" {
         exp_send "yes\r"
         send_log "gitlab:backup yes\n"
         exp_continue
    }
}
EOF

gitlab-ctl reconfigure >/dev/null
check "$?" '备份恢复重启配置gitlab-ctl reconfigure 失败'

gitlab-ctl restart >> ${log_file}
check "$?" '备份恢复重启服务器gitlab-ctl restart 失败'

w_time=`date "+%Y-%m-%d %H:%M:%S"`
echo "备份恢复结束时间:${w_time}" >> ${log_file}
echo >> ${log_file}

添加定时任务:
crontab -e
0 5 * * * /usr/bin/sh /data/auto_back/gitlab_backup.sh

你可能感兴趣的:(gitlab安装部署)