gitlab 备份

来源:互联网 发布:2017网络主播热唱歌曲 编辑:程序博客网 时间:2024/06/07 01:11

前提

之前休假的时候,公司里搭建的gitlab服务器莫名宕机,为此还被召唤回公司。到公司以后鼓捣了一下,发现是硬盘损坏,这下没辙了只能放弃。 
休假完以后到公司重新更换硬盘、重装系统、安装gitlab,让大家重新申请账号、提交版本库。整个过程耗时耗力,回头想想当时要是要是做了双硬盘备份,也就不会有这么麻烦的事情了。 
花费了时间把自动备份功能完成,在此做个记录。

gitlab手动备份

当完成gitlab搭建,所有用户完成备份以后,修改/etc/gitlab/gitlab.rb里面的默认存放备份文件的目录:

<code class="hljs bash has-numbering" style="display: block; padding: 0px; background: transparent; color: inherit; box-sizing: border-box; font-family: "Source Code Pro", monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal;">gitlab_rails[<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'backup_path'</span>] = <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'/home/backup'</span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right: 1px solid rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;">1</li></ul>

修改完以后保存,使用如下命令来重载配置文件:

<code class="hljs bash has-numbering" style="display: block; padding: 0px; background: transparent; color: inherit; box-sizing: border-box; font-family: "Source Code Pro", monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal;"><span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">sudo</span> gitlab-ctl reconfigure</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right: 1px solid rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;">1</li></ul>

当完成以上操作以后,我们用下面的命令来完成备份:

<code class="hljs css has-numbering" style="display: block; padding: 0px; background: transparent; color: inherit; box-sizing: border-box; font-family: "Source Code Pro", monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal;"><span class="hljs-tag" style="color: rgb(0, 0, 0); box-sizing: border-box;">gitlab-rake</span> <span class="hljs-tag" style="color: rgb(0, 0, 0); box-sizing: border-box;">gitlab</span><span class="hljs-pseudo" style="color: rgb(0, 0, 0); box-sizing: border-box;">:backup</span><span class="hljs-pseudo" style="color: rgb(0, 0, 0); box-sizing: border-box;">:create</span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right: 1px solid rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;">1</li></ul>

等待命令完成,我们可以到/home/backup目录下查看,会发现多了一个”**.tar”的文件。

gitlab自动备份?

当我们完成上面的操作以后,已经可以手动备份gitlab了。但是不可能每天都去服务器敲一下命令,备份一次。我们需要一个自动备份的机制,这里我们就需要用到linux的crontab工具。

crontab

在ubuntu版本下,crontab工具是默认安装的,配置文件在/etc/crontab里,用编辑器打开,我们看到如下的东西:

<code class="hljs applescript has-numbering" style="display: block; padding: 0px; background: transparent; color: inherit; box-sizing: border-box; font-family: "Source Code Pro", monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal;"><span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;"># m h dom mon dow user command</span><span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">17</span> *    * * *   root    cd / && <span class="hljs-command" style="box-sizing: border-box;">run</span>-parts <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">--report /etc/cron.hourly </span><span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">25</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">6</span>    * * *   root    test -x /usr/sbin/anacron || ( cd / && <span class="hljs-command" style="box-sizing: border-box;">run</span>-parts <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">--report /etc/cron.daily ) </span><span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">47</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">6</span>    * * <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">7</span>   root    test -x /usr/sbin/anacron || ( cd / && <span class="hljs-command" style="box-sizing: border-box;">run</span>-parts <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">--report /etc/cron.weekly ) </span><span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">52</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">6</span>    <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span> * *   root    test -x /usr/sbin/anacron || ( cd / && <span class="hljs-command" style="box-sizing: border-box;">run</span>-parts <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">--report /etc/cron.monthly )</span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right: 1px solid rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li></ul>

在crontab文件里面,每一行代表一项任务,每行的每个字段代表一项设置,它的格式共分为六个字段,前五段是时间设定段,第六段是要执行的命令段,每个字段之间用空格分割,没用的段用*代替,格式如下:

<code class="hljs livecodeserver has-numbering" style="display: block; padding: 0px; background: transparent; color: inherit; box-sizing: border-box; font-family: "Source Code Pro", monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal;">m h dom mon dow user <span class="hljs-command" style="box-sizing: border-box;"><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">command</span></span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right: 1px solid rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;">1</li></ul>

其中:

  • m: 表示分钟,可以是从0到59之间的任何整数。
  • h:表示小时,可以是从0到23之间的任何整数。
  • dom:表示日期,可以是从1到31之间的任何整数。
  • mon:表示月份,可以是从1到12之间的任何整数。
  • dow:表示星期几,可以是从0到7之间的任何整数,这里的0或7代表星期日。
  • user : 表示执行的用户。
  • command:要执行的命令,可以是系统命令,也可以是自己编写的脚本文件(如shell文件)。

我们在/home/backup目录下创建auto_backup.sh文件,同时赋予执行权限,操作如下:

<code class="hljs bash has-numbering" style="display: block; padding: 0px; background: transparent; color: inherit; box-sizing: border-box; font-family: "Source Code Pro", monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal;"><span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">sudo</span> chmod +x auto_backup.sh<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">sudo</span> vim auto_backup.sh</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right: 1px solid rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li></ul>

进入编辑界面,添加如下代码:

<code class="hljs css has-numbering" style="display: block; padding: 0px; background: transparent; color: inherit; box-sizing: border-box; font-family: "Source Code Pro", monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal;"><span class="hljs-tag" style="color: rgb(0, 0, 0); box-sizing: border-box;">gitlab-rake</span> <span class="hljs-tag" style="color: rgb(0, 0, 0); box-sizing: border-box;">gitlab</span><span class="hljs-pseudo" style="color: rgb(0, 0, 0); box-sizing: border-box;">:backup</span><span class="hljs-pseudo" style="color: rgb(0, 0, 0); box-sizing: border-box;">:create</span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right: 1px solid rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;">1</li></ul>

然后保存退出。我们可以执行一遍该脚本,可以发现在bakcup目录下又创建了一个备份。 
修改crontab文件,添加一行:

<code class="hljs markdown has-numbering" style="display: block; padding: 0px; background: transparent; color: inherit; box-sizing: border-box; font-family: "Source Code Pro", monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal;">0 20 <span class="hljs-bullet" style="box-sizing: border-box;">* *</span> * root /home/backup/auto_backup.sh -D 1</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right: 1px solid rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;">1</li></ul>

上面的命令就是让crontab每天的20:00自动执行auto_backup.sh脚本。

find和rm

当上面的操作完成以后,我们已经完成了自动备份的功能,但是每天备份一次,备份的文件大概300M,一、两个月以后我们的硬盘就没有剩余空间了。所以我们还需要添加删除过期文件的功能。 
首先我们要查找出备份目录下面过期的文件,然后在删除,指令如下,具体的find指令介绍见这里:

<code class="hljs lasso has-numbering" style="display: block; padding: 0px; background: transparent; box-sizing: border-box; font-family: "Source Code Pro", monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal;"><span style="color:rgba(0, 0, 0, 0);">find </span><span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"/home/backup/"</span><span style="color:rgba(0, 0, 0, 0);"> </span><span class="hljs-attribute" style="color: inherit; box-sizing: border-box;">-name</span><span style="color:rgba(0, 0, 0, 0);"> </span><span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"*.tar"</span><span style="color:rgba(0, 0, 0, 0);"> </span><span class="hljs-attribute" style="color: inherit; box-sizing: border-box;">-ctime</span><span style="color:rgba(0, 0, 0, 0);"> </span><span class="hljs-subst" style="color: rgb(0, 0, 0); box-sizing: border-box;">+</span><span class="hljs-subst" style="box-sizing: border-box;"><span style="color:#006666;">10</span></span><span style="color:rgba(0, 0, 0, 0);"> </span><span class="hljs-attribute" style="color: inherit; box-sizing: border-box;">-type</span><span style="color:rgba(0, 0, 0, 0);"> f </span><span class="hljs-attribute" style="color: inherit; box-sizing: border-box;">-exec</span><span style="color:rgba(0, 0, 0, 0);"> rm </span><span class="hljs-attribute" style="color: inherit; box-sizing: border-box;">-rf</span><span style="color:rgba(0, 0, 0, 0);"> {}</span></code><ul class="pre-numbering" style="color: rgb(51, 51, 51); box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right: 1px solid rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;">1</li></ul>

上面指令的意思就是查找备份目录下面后缀名是tar的所有普通文件,判断他们的创建时间,如果是10天前创建的,那么就删除。

双备份机制

好了,到这里我们想要的功能才完成了10%(什么?才10%?),因为我们最重要的功能没有做,双备份(想想开头,为什么要重新搭建服务器,因为我们的硬盘彻底坏了)。

rsync -avz --delete ${gitbackup_base_dir}  remote:remote_path${gitbackup_base_dir}


0 0
原创粉丝点击