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

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

参考:Gitlab的备份与恢复

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

Gitlab Server: 192.168.0.55 Bakcup SWerver: 192.168.0.222

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

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

#!/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档案备份脚本
#!/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
# 在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任务
/etc/init.d/crond reload
/etc/init.d/crond restart 
  • 定时删除备份服务器上的备份文件
#!/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 {} \;
  • 脚本发送邮件
# echo “邮件正文” | mail -s 邮件主题 收件地址
yum install -y mailx
# man mail

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

发表于

我来说两句

0 条评论
登录 后参与评论

相关文章

来自专栏陈仁松博客

ASP.NET Core 'Microsoft.Win32.Registry' 错误修复

今天在发布Asp.net Core应用到Azure的时候出现错误InvalidOperationException: Cannot find compilati...

5308
来自专栏Golang语言社区

【Golang语言社区】GO1.9 map并发安全测试

var m sync.Map //全局 func maintest() { // 第一个 YongHuomap := make(map[st...

5548
来自专栏大内老A

The .NET of Tomorrow

Ed Charbeneau(http://developer.telerik.com/featured/the-net-of-tomorrow/) Exciti...

39510
来自专栏一个会写诗的程序员的博客

Spring Reactor 项目核心库Reactor Core

Non-Blocking Reactive Streams Foundation for the JVM both implementing a Reactiv...

2902
来自专栏杨龙飞前端

scrollto 到指定位置

2994
来自专栏张善友的专栏

Silverlight + Model-View-ViewModel (MVVM)

     早在2005年,John Gossman写了一篇关于Model-View-ViewModel模式的博文,这种模式被他所在的微软的项目组用来创建Expr...

3358
来自专栏张善友的专栏

Miguel de Icaza 细说 Mix 07大会上的Silverlight和DLR

Mono之父Miguel de Icaza 详细报道微软Mix 07大会上的Silverlight和DLR ,上面还谈到了Mono and Silverligh...

3037
来自专栏我和未来有约会

Kit 3D 更新

Kit3D is a 3D graphics engine written for Microsoft Silverlight. Kit3D was inita...

2956
来自专栏一个爱瞎折腾的程序猿

sqlserver使用存储过程跟踪SQL

USE [master] GO /****** Object: StoredProcedure [dbo].[sp_perfworkload_trace_s...

3070
来自专栏pangguoming

Spring Boot集成JasperReports生成PDF文档

由于工作需要,要实现后端根据模板动态填充数据生成PDF文档,通过技术选型,使用Ireport5.6来设计模板,结合JasperReports5.6工具库来调用渲...

1.5K7

扫码关注云+社区