由于前期部署了gerrit代码审核系统,开发调整后的线上代码都放到gerrit上,这就要求我们要保证代码的安全。所以,对gerrit代码的备份至关重要!

备份的策略是:

1)先首次将gerrit项目代码git clone下来,然后后续备份前进行git pull

2)编写备份脚本,crontab定时执行

3)除了本机备份外,还可以备份到远程

下面是操作记录:

比如,备份gerrit上的下列项目代码

gerrit代码简单备份方案分享_git

查看各个项目的gerrit代码下载地址(ssh方式):

gerrit代码简单备份方案分享_android_02

1)首先在本地将gerrit项目代码下载下来(本地的wangshibo用户下的公钥已经放到gerrit上,在wangshibo用户下git clone)
[root@115r gerrit.bak]# su - wangshibo
[root@115r gerrit.bak]$ pwd
/home/wangshibo/gerrit.bak
[root@115r gerrit.bak]$ git clone ssh://wangshibo@103.110.186.130:29418/kevin_ios && scp -p -P 29418 wangshibo@103.110.186.130:hooks/commit-msg kevin_ios/.git/hooks/
[root@115r gerrit.bak]$ git clone ssh://wangshibo@103.110.186.130:29418/kevin_android && scp -p -P 29418 wangshibo@103.110.186.130:hooks/commit-msg fangfull_kevin/.git/hooks/
[root@115r gerrit.bak]$ git clone ssh://wangshibo@103.110.186.130:29418/kevin_old_android && scp -p -P 29418 wangshibo@103.110.186.130:hooks/commit-msg kevin_old_android/.git/hooks/
[root@115r gerrit.bak]$ git clone ssh://wangshibo@103.110.186.130:29418/kevin_old_ios && scp -p -P 29418 wangshibo@103.110.186.130:hooks/commit-msg kevin_old_ios/.git/hooks/
[root@115r gerrit.bak]$ git clone ssh://wangshibo@103.110.186.130:29418/static-kevin && scp -p -P 29418 wangshibo@103.110.186.130:hooks/commit-msg static-kevin/.git/hooks/

[root@115r gerrit.bak]$ ll
总用量 32
drwxr-xr-x. 5 wangshibo wangshibo 4096 9月 14 13:10 kevin_ios
drwxr-xr-x. 5 wangshibo wangshibo 4096 8月 24 09:49 kevin_android
drwxr-xr-x. 4 wangshibo wangshibo 4096 8月 24 09:49 kevin_old_android
drwxr-xr-x. 4 wangshibo wangshibo 4096 8月 24 09:49 kevin_old_ios
drwxr-xr-x. 10 wangshibo wangshibo 4096 8月 29 11:29 static-kevin

[root@115r gerrit.bak]$ cd kevin_old_ios/
[root@115r kevin_old_ios]$ git branch -av //查看发现git分支也已经下载下来了
* master fd4c4f6 90
remotes/origin/HEAD -> origin/master
remotes/origin/dev a5bdc2c 提交客户resetCookie
remotes/origin/kevin_old_ios e00aa7e DEL:rm readme file into fangfull_old_ios
remotes/origin/master fd4c4f6 90

[root@115r gerrit.bak]$ cat /home/wangshibo/gerrit-bak.sh
#!/bin/bash
cd /home/wangshibo/gerrit.bak/kevin_android && /usr/local/bin/git pull
cd /home/wangshibo/gerrit.bak/kevin_ios && /usr/local/bin/git pull
cd /home/wangshibo/gerrit.bak/kevin_old_android && /usr/local/bin/git pull
cd /home/wangshibo/gerrit.bak/kevin_old_ios && /usr/local/bin/git pull
cd /home/wangshibo/gerrit.bak/static-kevin && /usr/local/bin/git pull

然后crontab计划任务制定,每小时备份一次
[root@Zabbix-server gerrit.bak]$ crontab -l
10 * * * * /bin/bash -x /home/wangshibo/gerrit-bak.sh > /dev/null 2>&1

2)第一步只是在本地备份,可以将本地备份的git做副本放到远程机器上
下面是远程机器上的操作(远程机器已和本地机器做了ssh信任关系)(192.168.1.15是本地机器)
[root@bastion-IDC ~]# cd /home/backup/
[root@bastion-IDC backup]# ls
192.168.1.15-gerrit gerritbak.sh
[root@bastion-IDC backup]# cat gerritbak.sh
#/bin/bash
/usr/bin/rsync -e "ssh -p22" -avpgolr wangshibo@192.168.1.15:/home/wangshibo/gerrit.bak /home/backup/192.168.1.15-gerrit/`date +gerrit-%Y-%m-%d`/

[root@bastion-IDC backup]# cd 192.168.1.15-gerrit/
[root@bastion-IDC 192.168.1.15-gerrit]# ls
gerrit-2016-9-17
[root@bastion-IDC 192.168.1.15-gerrit]# cd gerrit-2016-9-17/

[root@bastion-IDC gerrit-2016-9-17]# ls

gerrit.bak
[root@bastion-IDC gerrit-2016-9-17]# ll gerrit.bak
total 32
drwxr-xr-x. 5 512 512 4096 Aug 24 09:49 kevin_android
drwxr-xr-x. 4 512 512 4096 Aug 24 09:49 kevin_ios
drwxr-xr-x. 4 512 512 4096 Aug 24 09:49 kevin_old_android
drwxr-xr-x. 4 512 512 4096 Aug 24 09:49 kevin_old_ios
drwxr-xr-x. 10 512 512 4096 Aug 29 11:29 static-kevin

[root@bastion-IDC 192.168.1.15-gerrit]# cd kevin_old_ios/
[wangshibo@bastion-IDC kevin_old_ios]$ git branch -av //查看,说明git分支也已经备份下来了
* master fd4c4f6 90
remotes/origin/HEAD -> origin/master
remotes/origin/dev a5bdc2c 提交客户resetCookie
remotes/origin/kevin_old_ios e00aa7e DEL:rm readme file into fangfull_old_ios
remotes/origin/master fd4c4f6 90

制定计划任务,每30分钟备份过来一次
[root@bastion-IDC backup]# crontab -l
*/30 * * * * /bin/bash -x /home/backup/gerritbak.sh > /dev/null 2>&1


*************** 当你发现自己的才华撑不起野心时,就请安静下来学习吧!***************