实现脚本自动备份Gitlab档案并同步到远程
参考:Gitlab的备份与恢复
Gitlab服务器与备份服务器进行密钥配对,免密使用scp传输Gitlab备份文件
Gitlab Server: 192.168.0.55
Bakcup SWerver: 192.168.0.222
- 在Gitlab服务器上生产密钥对
ssh-keygen -t rsa
# 在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=hwang@aniu.tv
MailToUser2=yjiang@aniu.tv
MailToUser2=crshen@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