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)