>>分享流行的Java框架以及开源软件,对孙卫琴的《精通Spring:Java Web开发技术详解》提供技术支持 书籍支持  卫琴直播  品书摘要  在线测试  资源下载  联系我们
发表一个新主题 开启一个新投票 回复文章 您是本文章第 19612 个阅读者 刷新本主题
 * 贴子主题:  如何实现Git服务间同步 回复文章 点赞(0)  收藏  
作者:flybird    发表时间:2020-05-14 00:28:15     消息  查看  搜索  好友  邮件  复制  引用

  
好久没写博客了,虽然也没写几篇^_^...露个脸,表示还活跃在互联网行业中...不说没用的了,分享一下如何实现Git服务间的同步。
Git服务我们一般多会使用gitlab-rake工具定时做备份,当出现问题时利用备份恢复,那是否有一种需求是另外一个环境也需要部署一套Git服务,且需要和前一套Git服务去同步的,即使做冷备,既省去了恢复时间,也达到了定时做恢复演练的目的。
目前我们另外一个环境就遇到了这样的需求,当然可以研究学习Git API去实现,但由于目前需求对同步实时性要求不高,于是我准备用一个直接简单粗暴的方式来实现,其实就是拿Git备份去另外一套环境做自动恢复,交互问题我们交给expect工具来解决。
下面分别分享下Python和Shell脚本作为参考学习,同步时间则可以调整定时备份的时间及恢复时间即可,内容大同小异,思路基本一致:    
#!/usr/bin/python
# -*- coding: utf-8 -*-

import os
import sys
import pexpect
import paramiko

# 备份服务器的IP地址、用户名
git_src_host = "xxx.xxx.xxx.xxx"
git_src_user = "xxx"
git_src_port = "22"
# 地址自行根据环境变更
private_key  = paramiko.RSAKey.from_private_key_file('/xxx/xxx/xxx/.ssh/id_rsa')

git_bak_dir  = "/var/opt/gitlab/backups"

# Copy backup to xxx.xxx.xxx.xxx(查找备份服务器一天以内的备份传到恢复服务器所在备份目录)

try:
    ssh = paramiko.SSHClient()
    ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
    ssh.connect(
        hostname=git_src_host,
        username=git_src_user,
        port=git_src_port,
        password=private_key)

    cmd=("find " + git_bak_dir + " -type f -ctime -1")
    stdin, stdout, stderr = ssh.exec_command(cmd)

    for item in stdout.readlines():
        chown_cmd="chown git:git " + git_bak_dir + "/*;chmod 700 " + git_bak_dir
        rsync_cmd=('rsync -avzP '
                    + git_src_user + '@'
                    + git_src_host + ':'
                    + item.strip('
') + ' '
                    + git_bak_dir)

        #print(rsync_cmd)
        os.system(rsync_cmd)
        os.system(chown_cmd)

    ssh.close()
except Exception, e:
    print e

# Restore backup

ls_cmd="ls -t " + git_bak_dir + " | \
        head -n 1 | \
        sed -r 's/^([0-9]+_[0-9]+_[0-9]+_[0-9]+_.*)_gitlab_backup.tar/\\1/g'"

rs=os.popen(ls_cmd)
restore_file=rs.read()
# print(restore_file)

os.system("gitlab-ctl stop unicorn;gitlab-ctl stop sidekiq")

expectations=["Do you want to continue (yes/no)?","Do you want to continue (yes/no)?",]
child = pexpect.spawn('gitlab-rake gitlab:backup:restore BACKUP=' + restore_file,timeout=1800)
child.logfile = sys.stdout
while True:
    try:
        i = child.expect(expectations)
        if i == 0:
            child.sendline('yes')
        elif i == 1:
            child.sendline('yes')
    except pexpect.EOF:
        print('Exiting fexpect for EOF.')
        break

os.system("gitlab-ctl restart")

#!/bin/bash
#

git_src_host="xxx.xxx.xxx.xxx"
git_src_user="xxx"

git_bak_dir="/var/opt/gitlab/backups"
git_bak_file=`ssh ${git_src_user}@${git_src_host} "find ${git_bak_dir} -type f -ctime -1"`

# Copy backup to xxx.xxx.xxx.xxx

for bakfiles in $git_bak_file ; do

    echo "------------------------------------"
    rsync -avzP ${git_src_user}@${git_src_host}:${bakfiles} ${git_bak_dir}
    echo $bakfiles
    echo "------------------------------------"

done

chown git:root $git_bak_dir
chmod 700 $git_bak_dir

# Restore backup

restore_file=`ls -t $git_bak_dir | head -n 1 | sed -r 's/^([0-9]+_[0-9]+_[0-9]+_[0-9]+_.*)_gitlab_backup.tar/\1/g'`

gitlab-ctl stop unicorn
gitlab-ctl stop sidekiq

/bin/expect <<EOF
set time 30
spawn gitlab-rake gitlab:backup:restore BACKUP=$restore_file
expect "Do you want to continue (yes/no)?" {
    send "yes\r";
}
expect "Do you want to continue (yes/no)?" {
    send "yes\r";
}
expect "Do you want to continue (yes/no)?" {
    send "yes\r";
}
expect "Do you want to continue (yes/no)?" {
    send "yes\r";
}
expect "Do you want to continue (yes/no)?" {
    send "yes\r";
}
expect eof
EOF

#gitlab-ctl reconfigure
gitlab-ctl restart

----------------------------
原文链接:https://blog.51cto.com/wanghaipeng1124/2486964

程序猿的技术大观园:www.javathinker.net



[这个贴子最后由 flybird 在 2020-05-15 17:08:17 重新编辑]
  Java面向对象编程-->类的生命周期
  JavaWeb开发-->Web运作原理(Ⅲ)
  JSP与Hibernate开发-->持久化层的映射类型
  Java网络编程-->XML数据处理
  精通Spring-->绑定表单
  Vue3开发-->创建综合购物网站应用
  Netty初识
  微服务架构集大成者—Spring Cloud简明教程
  Spring API中JAVA反射—工具类ReflectionUtils
  孙卫琴系列Java书籍的QQ交流读者群
  重新理解响应式编程
  Spring MVC处理异步请求
  深入Redis客户端(redis客户端属性、redis缓冲区、关闭redis客...
  解析配置文件 redis.conf、Redis持久化RDB、Redis的主从复制
  Spring Cloud构建微服务架构的服务注册与发现
  【Web服务开发】基于Java开发代驾定位系统,2天完成脚手架
  一睹Web服务真面目,有商业价值的Web服务是这样的
  Spring MVC:切面的应用
  从响应式编程到“好莱坞”
  springboot —— 多数据源
  大话微服务」深入聊聊SpringCloud之客户端负载均衡机制
  更多...
 IPIP: 已设置保密
楼主      
1页 0条记录 当前第1
发表一个新主题 开启一个新投票 回复文章


中文版权所有: JavaThinker技术网站 Copyright 2016-2026 沪ICP备16029593号-2
荟萃Java程序员智慧的结晶,分享交流Java前沿技术。  联系我们
如有技术文章涉及侵权,请与本站管理员联系。