前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >实现Shell脚本自动备份Gitlab档案并同步到远程

实现Shell脚本自动备份Gitlab档案并同步到远程

作者头像
shaonbean
发布2018-01-02 11:48:59
2.2K0
发布2018-01-02 11:48:59
举报
文章被收录于专栏:运维前线

实现脚本自动备份Gitlab档案并同步到远程

参考:Gitlab的备份与恢复

Gitlab服务器与备份服务器进行密钥配对,免密使用scp传输Gitlab备份文件

Gitlab Server: 192.168.0.55 Bakcup SWerver: 192.168.0.222

  • 在Gitlab服务器上生产密钥对
代码语言:javascript
复制
ssh-keygen -t rsa
  • 拷贝Gitlab上的公钥上传到备份服务器的指定目录(~/.ssh/authorized_keys):默认为当前用户的.ssh/authorized_keys文件内,笔者使用的是root账户
代码语言:javascript
复制
# 在gitlab上执行,如果端口不是默认的22,采用scp的方式拷贝gitlab公钥
ssh-copy-id root@192.168.0.222

创建shell定时备份Gitlab档案到远程脚本

代码语言:javascript
复制
#!/bin/bash
##########################################################################
# Script Name: auto_backup_gitlabdata_to_remote.sh
# Author: wanghui
# Email: yunwei@aniu.tv
# Created Time: Thu 07 Sep 2017 08:59:26 PM CST
#########################################################################
# Blog address: http://blog.csdn.net/wh211212
#########################################################################
# Functions:  #
# 
# Define some variables:  #
# Gitlab 档案备份路径
LocalBackDir=/var/opt/gitlab/backups

# Backup server 存储路径
RemoteBackDir=/mnt/backups/gitlab_backups/remote

# 远程备份使用用户及端口
RemoteUser=root
RemotePort=22

# 备份服务器IP
RemoteIP=192.168.0.222

# 备份时间戳
Date=`date +"%F-%T"`

# 备份日志文件
LogFile=$LocalBackDir/remote_backup.log

# 查找本地备份目录下一天以内且后缀为.tar的Gitlab备份文件
Backfile_Send_To_Remote=$(find /var/opt/gitlab/backups -type f -mtime -1 -name '*.tar')

#Backfile_Send_To_Remote=`find $LocalBackDir -type f -mtime -1 -name '*.tar'`

# 新建备份日志文件
touch $LogFile

# 记录备份日志
echo "${Date} Gitlab auto backup to remote server." >> $LogFile
echo "--------------------------------------------" >> $LogFile

# 打印每次备份的档案名
echo "The files need send to remote server is: $Backfile_Send_To_Remote" >> $LogFile

# 本地传输Gitlab备份档案到远程
scp -P $RemotePort $Backfile_Send_To_Remote $RemoteUser@$RemoteIP:$RemoteBackDir

# 备份结果追加到备份日志
if [ $? -eq 0 ];then
  echo ""
  echo "$Date Gitlab Remote Backup Succeed!" >> $LogFile
else
  echo "$Date Gitlab Remote Backup Failed!" >> $LogFile
fi
  • 创建Gitlab档案备份脚本
代码语言:javascript
复制
#!/bin/bash
##########################################################################
# Script Name: auto_backup_gitlabdata.sh
# Author: wanghui
# Email: yunwei@aniu.tv
# Created Time: Thu 07 Sep 2017 08:59:26 PM CST
#########################################################################
# Blog address: http://blog.csdn.net/wh211212
#########################################################################
# Functions: auto backup gitlab data#
# backup gitlab config
tar cfz /secret/gitlab/backups/$(date "+etc-gitlab-\%s.tgz") -C / etc/gitlab
# backup gitlab os
#/opt/gitlab/bin/gitlab-rake gitlab:backup:create
# gitlab本地备份路径
LocalBackDir=/var/opt/gitlab/backups

# 备份时间戳
Date=`date +"%F-%T"`

# 邮件写入的文件
MailDir=$LocalBackDir/mail
[ -d $MailDir ] || mkdir -p $MailDir
MailContent=$LocalBackDir/mail/mailcontent_$Date

# 邮件发送给谁
MailToUser1=wang@aniu.tv
MailToUser2=jiang@aniu.tv
MailToUser2=shen@aniu.tv

# 备份日志目录
LogDir=$LocalBackDir/log
[ -d $LogDir ] || mkdir -p $LogDir

# 新建日志文件
LogFile=$LocalBackDir/log/backup_$Date.log
touch $LogFile

# 追加日志到日志文件
echo "Gitlab auto backup at local server, start at  $(date +"%Y-%m-%d %H:%M:%S")" >  $LogFile
echo "--------------------------------------------------------------------------" >> $LogFile

# 执行gitlab本地备份功能
#/opt/gitlab/bin/gitlab-rake gitlab:backup:create

# $?符号显示上一条命令的返回值,如果为0则代表执行成功,其他表示失败
if [ $? -eq 0 ];then
   #追加日志到日志文件
   echo "--------------------------------Success!-------------------------------" >> $LogFile
   echo "Gitlab auto backup at local server, end at $(date +"%Y-%m-%d %H:%M:%S")" >> $LogFile

   #写Email的正文内容
   > "$MailContent"
   echo "GitLab Backup Daily Report,backup at local server Success ! Please Check your Email and read the following log file" >> $MailContent

   #读取mailcontent内容当做邮件正文 ,附件为Log文件
   #cat $MailContent | mail -s "Congratulation! GitLab backup at local server Success Report." $MailToUser1 < $LogFile
   cat $MailContent | mail -s "Congratulation! GitLab backup at local server Success Report." -a $LogFile $MailToUser1 -c $MailToUser2 $MailToUser3
else
   #追加日志到日志文件
   echo "--------------------------------Failed!----------------------------------" >> $LogFile
   echo "Gitlab auto backup at local server failed at $(date +"%Y-%m-%d %H:%M:%S")" >> $LogFile

   #写Email的正文内容
   > "$MailContent"
   echo "GitLab Backup Daily Report,Backup at local server failed Failed !  Please Check your Email and read the following log file !" >> $MailContent

   #读取mailcontent内容当做邮件正文附件为Log文件
   cat $MailContent | mail -s "Warning! GitLab Backup at local server Failed Report." -a $LogFile $MailToUser1 -c $MailToUser2 $MailToUser3
fi

定时执行脚本(crontab)

  • 编辑/etc/crontab
代码语言:javascript
复制
# 在crontab文件中添加对应定时任务
# Example of job definition:
# .---------------- minute (0 - 59)
# |  .------------- hour (0 - 23)
# |  |  .---------- day of month (1 - 31)
# |  |  |  .------- month (1 - 12) OR jan,feb,mar,apr ...
# |  |  |  |  .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
# |  |  |  |  |
# *  *  *  *  * user-name command to be executed

# add by wanghui,每天凌晨两点备份Gitlab
#m h d m w
0 2 * * * root /opt/gitlab/bin/gitlab-rake gitlab:backup:create

# 礼拜一到礼拜五的凌晨4点15分备份gitlab的配置文件,并压缩 
15 04 * * 2-6 umask 0077; tar cfz /secret/gitlab/backups/$(date "+etc-gitlab-\%s.tgz") -C / etc/gitlab

# add by wanghui 每天凌晨三点,执行备份Gitlab档案到远程服务器脚本
0 3 * * * root /bin/bash /home/yunwei/scripts/auto_backup_gitlabdata_to_remote.sh
  • 重启cron任务
代码语言:javascript
复制
/etc/init.d/crond reload
/etc/init.d/crond restart 
  • 定时删除备份服务器上的备份文件
代码语言:javascript
复制
#!/bin/bash
##########################################################################
# Script Name: auto_remove_old_backupdata.sh
# Author: wanghui
# Email: yunwei@aniu.tv
# Created Time: Thu 07 Sep 2017 08:59:26 PM CST
#########################################################################
# Blog address: http://blog.csdn.net/wh211212
#########################################################################
# Functions: auto remove old gitlab data#
GitlabBackDir=/mnt/backups/gitlab_backups/remote

# 查找远程备份路径下,超过30天的Gitlab备份档案,然后删除
find $GitlabBackDir -type f -mtime +30 -name '*.tar' -exec rm {} \;
  • 脚本发送邮件
代码语言:javascript
复制
# echo “邮件正文” | mail -s 邮件主题 收件地址
yum install -y mailx
# man mail
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 实现脚本自动备份Gitlab档案并同步到远程
    • Gitlab服务器与备份服务器进行密钥配对,免密使用scp传输Gitlab备份文件
      • 创建shell定时备份Gitlab档案到远程脚本
        • 定时执行脚本(crontab)
        领券
        问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档