01 前言
最近成功从架构组拿到了Gitlab的管理权限,第一件事就是想着如何备份,以防数据丢失背大锅,于是在网上搜索一番,发现一段非常赞的备份脚本,记录照着操作一下:
参考文章:https://blog.csdn.net/wh211212/article/details/77895801 《实现Shell脚本自动备份Gitlab档案并同步到远程》
https://blog.csdn.net/ouyang_peng/article/details/77070977 《git学习------> Gitlab如何进行备份恢复与迁移?》
尤其是第二篇文章博主,有非常多的好文章,非常值得学习推荐
02 备份过程记录
Gitlab服务器与备份服务器进行密钥配对,免密使用scp传输Gitlab备份文件
例如:Gitlab server(Gitlab服务器):181.137.128.32
Bakcup server(远程备份服务器): 181.137.128.103
在Gitlab服务器上生成密钥对
ssh-keygen -t rsa
拷贝Gitlab上的公钥上传到备份服务器的指定目录(~/.ssh/authorized_keys):默认为当前用户的.ssh/authorized_keys文件内,为避免权限问题,直接用root账户
# 在gitlab上执行,如果端口不是默认的22,采用scp的方式拷贝gitlab公钥
ssh-copy-id root@181.137.128.103
创建Gitlab档案备份脚本(本地备份)
#!/bin/bash
# Script Name: auto-backup-gitlabdata.sh
# Functions: auto backup gitlab data
# backup gitlab config
tar -czvf /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
# 备份时间戳
BackupDate=`date +"%F-%T"`
# 备份日志目录
LogDir=$LocalBackDir/log
[ -d $LogDir ] || mkdir -p $LogDir
# 新建日志文件
LogFile=$LocalBackDir/log/backup_${ BackupDate }.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
if [ $? -eq 0 ]
then
echo "--------------------------------Success!-------------------------------" >> $LogFile
echo "Gitlab auto backup at local server, end at $(date "+%Y-%m-%d %H:%M:%S")" >> $LogFile
else
echo "--------------------------------Failed!----------------------------------" >> $LogFile
echo "Gitlab auto backup at local server failed at $(date "+%Y-%m-%d %H:%M:%S")" >> $LogFile
fi
创建shell定时备份Gitlab档案到远程脚本(远程备份)
#!/bin/bash
# Script Name: auto-backup-gitlabdata-to-remote.sh
# Gitlab 档案备份路径(本地备份路径)
LocalBackDir=/var/opt/gitlab/backups
# Backup server 存储路径(远程备份路径)
RemoteBackDir=/data/software-data-backup/gitlab
# 远程备份使用用户及端口
RemoteUser=root
RemotePort=22
# 备份服务器IP
RemoteIP=181.137.128.103
# 备份时间戳
BackupDate=` date "+%F-%T" `
# 备份日志文件
LogFile=$LocalBackDir/auto-backup-gitlabdata.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 "${ BackupDate } 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 "$ ${ BackupDate } Gitlab Remote Backup Succeed!" >> $LogFile
else
echo "$ ${ BackupDate } Gitlab Remote Backup Failed!" >> $LogFile
fi
定时执行脚本(crontab)
crontab -e
# 在crontab文件中添加对应定时任务
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
GitlabBackDir=/data/software-data-backup/gitlab
查找远程备份路径下,超过30天的Gitlab备份档案,然后删除
find $GitlabBackDir -type f -mtime +30 -name '*.tar' -exec rm {} \;
同样的道理,把这个脚本加入定时任务中
03 总结
个人基础不是特别扎实,以至于依葫芦画瓢还是出了很多问题,所以还是要多学习啊…
参考链接 :
Linux篇:Shell脚本实现Gitlab双备份 : https://www.jianshu.com/p/9f1e9a14450e