实现脚本自动备份Gitlab档案并同步到远程
参考:Gitlab的备份与恢复
Gitlab服务器与备份服务器进行密钥配对,免密使用scp传输Gitlab备份文件
Gitlab Server: 192.168.0.55
Bakcup SWerver: 192.168.0.222
- 在Gitlab服务器上生产密钥对
ssh-keygen@H_502_20@ -t@H_502_20@ rsa
# 在gitlab上执行,如果端口不是默认的22,采用scp的方式拷贝gitlab公钥@H_502_20@
ssh-copy@H_502_20@-id@H_502_20@ root@192.168@H_502_20@.0@H_502_20@.222@H_502_20@
创建shell定时备份Gitlab档案到远程脚本
#!/bin/bash@H_502_20@
######@H_502_20@######@H_502_20@######@H_502_20@######@H_502_20@######@H_502_20@######@H_502_20@######@H_502_20@######@H_502_20@######@H_502_20@######@H_502_20@######@H_502_20@######@H_502_20@##@H_502_20@
# Script Name: auto_backup_gitlabdata_to_remote.sh@H_502_20@
# Author: wanghui@H_502_20@
# Email: yunwei@aniu.tv@H_502_20@
# Created Time: Thu 07 Sep 2017 08:59:26 PM CST@H_502_20@
######@H_502_20@######@H_502_20@######@H_502_20@######@H_502_20@######@H_502_20@######@H_502_20@######@H_502_20@######@H_502_20@######@H_502_20@######@H_502_20@######@H_502_20@######@H_502_20@#@H_502_20@
# Blog address: http://blog.csdn.net/wh211212@H_502_20@
######@H_502_20@######@H_502_20@######@H_502_20@######@H_502_20@######@H_502_20@######@H_502_20@######@H_502_20@######@H_502_20@######@H_502_20@######@H_502_20@######@H_502_20@######@H_502_20@#@H_502_20@
# Functions: #@H_502_20@
# @H_502_20@
# Define some variables: #@H_502_20@
# Gitlab 档案备份路径@H_502_20@
LocalBackDir=/var@H_502_20@/opt/gitlab/backups
# Backup server 存储路径@H_502_20@
RemoteBackDir=/mnt/backups/gitlab_backups/remote
# 远程备份使用用户及端口@H_502_20@
RemoteUser=root
RemotePort=22@H_502_20@
# 备份服务器IP@H_502_20@
RemoteIP=192.168@H_502_20@.0@H_502_20@.222@H_502_20@
# 备份时间戳@H_502_20@
Date=`date +"%F-%T"@H_502_20@@H_502_20@`
# 备份日志文件@H_502_20@
LogFile=$LocalBackDir/remote_backup.log
# 查找本地备份目录下一天以内且后缀为.tar的Gitlab备份文件@H_502_20@
Backfile_Send_To_Remote=$(find /var@H_502_20@/opt/gitlab/backups -type f -mtime -1@H_502_20@ -name '*.tar'@H_502_20@)
#Backfile_Send_To_Remote=`find $LocalBackDir -type f -mtime -1 -name '*.tar'`@H_502_20@
# 新建备份日志文件@H_502_20@
touch $LogFile
# 记录备份日志@H_502_20@
echo "${Date} Gitlab auto backup to remote server."@H_502_20@ >> $LogFile
echo "--------------------------------------------"@H_502_20@ >> $LogFile
# 打印每次备份的档案名@H_502_20@
echo "The files need send to remote server is: $Backfile_Send_To_Remote"@H_502_20@ >> $LogFile
# 本地传输Gitlab备份档案到远程@H_502_20@
scp -P $RemotePort $Backfile_Send_To_Remote $RemoteUser@$RemoteIP@H_502_20@:$RemoteBackDir
# 备份结果追加到备份日志@H_502_20@
if@H_502_20@ [ $? -eq 0@H_502_20@ ];then@H_502_20@
echo ""@H_502_20@
echo "$Date Gitlab Remote Backup Succeed!"@H_502_20@ >> $LogFile
else@H_502_20@
echo "$Date Gitlab Remote Backup Failed!"@H_502_20@ >> $LogFile
fi
- 创建Gitlab档案备份脚本
#!/bin/bash@H_502_20@
##########################################################################@H_502_20@
# Script Name: auto_backup_gitlabdata.sh@H_502_20@
# Author: wanghui@H_502_20@
# Email: yunwei@aniu.tv@H_502_20@
# Created Time: Thu 07 Sep 2017 08:59:26 PM CST@H_502_20@
#########################################################################@H_502_20@
# Blog address: http://blog.csdn.net/wh211212@H_502_20@
#########################################################################@H_502_20@
# Functions: auto backup gitlab data#@H_502_20@
# backup gitlab config@H_502_20@
tar cfz /secret/gitlab/backups/$(date "+etc-gitlab-\%s.tgz"@H_502_20@) -C / etc/gitlab
# backup gitlab os@H_502_20@
#/opt/gitlab/bin/gitlab-rake gitlab:backup:create@H_502_20@
# gitlab本地备份路径@H_502_20@
LocalBackDir=/var/opt/gitlab/backups
# 备份时间戳@H_502_20@
Date=`date +"%F-%T"@H_502_20@`
# 邮件写入的文件@H_502_20@
MailDir=$LocalBackDir@H_502_20@/mail
[ -d@H_502_20@ $MailDir@H_502_20@ ] || mkdir -p $MailDir@H_502_20@
MailContent=$LocalBackDir@H_502_20@/mail/mailcontent_$Date@H_502_20@
# 邮件发送给谁@H_502_20@
MailToUser1=hwang@aniu.tv
MailToUser2=yjiang@aniu.tv
MailToUser2=crshen@aniu.tv
# 备份日志目录@H_502_20@
LogDir=$LocalBackDir@H_502_20@/log
[ -d@H_502_20@ $LogDir@H_502_20@ ] || mkdir -p $LogDir@H_502_20@
# 新建日志文件@H_502_20@
LogFile=$LocalBackDir@H_502_20@/log/backup_$Date@H_502_20@.log
touch $LogFile@H_502_20@
# 追加日志到日志文件@H_502_20@
echo@H_502_20@ "Gitlab auto backup at local server,start at $(date +"%Y-%m-%d %H:%M:%S")@H_502_20@"@H_502_20@ > $LogFile@H_502_20@
echo@H_502_20@ "--------------------------------------------------------------------------"@H_502_20@ >> $LogFile@H_502_20@
# 执行gitlab本地备份功能@H_502_20@
#/opt/gitlab/bin/gitlab-rake gitlab:backup:create@H_502_20@
# $?符号显示上一条命令的返回值,如果为0则代表执行成功,其他表示失败@H_502_20@
if@H_502_20@ [ $? -eq@H_502_20@ 0@H_502_20@ ];then@H_502_20@
#追加日志到日志文件@H_502_20@
echo@H_502_20@ "--------------------------------Success!-------------------------------"@H_502_20@ >> $LogFile@H_502_20@
echo@H_502_20@ "Gitlab auto backup at local server,end at $(date +"%Y-%m-%d %H:%M:%S")@H_502_20@"@H_502_20@ >> $LogFile@H_502_20@
#写Email的正文内容@H_502_20@
> "$MailContent@H_502_20@"@H_502_20@
echo@H_502_20@ "GitLab Backup Daily Report,backup at local server Success ! Please Check your Email and read the following log file"@H_502_20@ >> $MailContent@H_502_20@
#读取mailcontent内容当做邮件正文 ,附件为Log文件@H_502_20@
#cat $MailContent | mail -s "Congratulation! GitLab backup at local server Success Report." $MailToUser1 < $LogFile@H_502_20@
cat $MailContent@H_502_20@ | mail -s@H_502_20@ "Congratulation! GitLab backup at local server Success Report."@H_502_20@ -a@H_502_20@ $LogFile@H_502_20@ $MailToUser1@H_502_20@ -c $MailToUser2@H_502_20@ $MailToUser3@H_502_20@
else@H_502_20@
#追加日志到日志文件@H_502_20@
echo@H_502_20@ "--------------------------------Failed!----------------------------------"@H_502_20@ >> $LogFile@H_502_20@
echo@H_502_20@ "Gitlab auto backup at local server Failed at $(date +"%Y-%m-%d %H:%M:%S")@H_502_20@"@H_502_20@ >> $LogFile@H_502_20@
#写Email的正文内容@H_502_20@
> "$MailContent@H_502_20@"@H_502_20@
echo@H_502_20@ "GitLab Backup Daily Report,Backup at local server Failed Failed ! Please Check your Email and read the following log file !"@H_502_20@ >> $MailContent@H_502_20@
#读取mailcontent内容当做邮件正文附件为Log文件@H_502_20@
cat $MailContent@H_502_20@ | mail -s@H_502_20@ "Warning! GitLab Backup at local server Failed Report."@H_502_20@ -a@H_502_20@ $LogFile@H_502_20@ $MailToUser1@H_502_20@ -c $MailToUser2@H_502_20@ $MailToUser3@H_502_20@
fi@H_502_20@
定时执行脚本(crontab)
- 编辑/etc/crontab
# 在crontab文件中添加对应定时任务@H_502_20@
# Example of job definition:@H_502_20@
# .---------------- minute (0 - 59)@H_502_20@
# | .------------- hour (0 - 23)@H_502_20@
# | | .---------- day of month (1 - 31)@H_502_20@
# | | | .------- month (1 - 12) OR jan,feb,mar,apr ...@H_502_20@
# | | | | .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat@H_502_20@
# | | | | |@H_502_20@
# * * * * * user-name command to be executed@H_502_20@
# add by wanghui,每天凌晨两点备份Gitlab@H_502_20@
#m h d m w@H_502_20@
0@H_502_20@ 2@H_502_20@ * * * root /opt/gitlab/bin/gitlab-rake gitlab:backup:create
# 礼拜一到礼拜五的凌晨4点15分备份gitlab的配置文件,并压缩 @H_502_20@
15@H_502_20@ 04@H_502_20@ * * 2@H_502_20@-6@H_502_20@ umask 0077@H_502_20@; tar cfz /secret/gitlab/backups/$(date "+etc-gitlab-\%s.tgz"@H_502_20@) -C / etc/gitlab
# add by wanghui 每天凌晨三点,执行备份Gitlab档案到远程服务器脚本@H_502_20@
0@H_502_20@ 3@H_502_20@ * * * root /bin/bash /home/yunwei/scripts/auto_backup_gitlabdata_to_remote.sh
- 重启cron任务
/etc/init.d@H_502_20@/crond reload
/etc/init.d@H_502_20@/crond restart
#!/bin/bash@H_502_20@
######@H_502_20@######@H_502_20@######@H_502_20@######@H_502_20@######@H_502_20@######@H_502_20@######@H_502_20@######@H_502_20@######@H_502_20@######@H_502_20@######@H_502_20@######@H_502_20@##@H_502_20@
# Script Name: auto_remove_old_backupdata.sh@H_502_20@
# Author: wanghui@H_502_20@
# Email: yunwei@aniu.tv@H_502_20@
# Created Time: Thu 07 Sep 2017 08:59:26 PM CST@H_502_20@
######@H_502_20@######@H_502_20@######@H_502_20@######@H_502_20@######@H_502_20@######@H_502_20@######@H_502_20@######@H_502_20@######@H_502_20@######@H_502_20@######@H_502_20@######@H_502_20@#@H_502_20@
# Blog address: http://blog.csdn.net/wh211212@H_502_20@
######@H_502_20@######@H_502_20@######@H_502_20@######@H_502_20@######@H_502_20@######@H_502_20@######@H_502_20@######@H_502_20@######@H_502_20@######@H_502_20@######@H_502_20@######@H_502_20@#@H_502_20@
# Functions: auto remove old gitlab data#@H_502_20@
GitlabBackDir=/mnt/backups/gitlab_backups/remote
# 查找远程备份路径下,超过30天的Gitlab备份档案,然后删除@H_502_20@
find $GitlabBackDir -type f -mtime +30@H_502_20@ -name '*.tar'@H_502_20@ -exec rm {} \;
- 脚本发送邮件
# echo “邮件正文” | mail -s 邮件主题 收件地址@H_502_20@
yum install -y mailx
# man mail@H_502_20@