1.为了能够备份和恢复,请确保你的系统上安装了Rsync
#Debian/Ubauntusudo apt-get install rsync# RHEL/Centossudo yum install rsync
#Debian/Ubauntu
sudo apt-get install rsync
# RHEL/Centos
sudo yum install rsync
2.配置了与备份目标机器之间的免密认证
vim /etc/gitlab/gitlab.rb
#指定备份后数据存放的路径、权限、时间配置gitlab_rails['manage_backup_path'] = true #292行 开启备份功能gitlab_rails['backup_path'] = "/opt/gitlab_backups" #293行 指定备份的路径gitlab_rails['backup_archive_permissions'] = 0644 #296行 备份文件的权限gitlab_rails['backup_keep_time'] = 7776000 #301行 备份保留时间(保留90天 单位:秒
#指定备份后数据存放的路径、权限、时间配置
gitlab_rails['manage_backup_path'] = true #292行 开启备份功能
gitlab_rails['backup_path'] = "/opt/gitlab_backups" #293行 指定备份的路径
gitlab_rails['backup_archive_permissions'] = 0644 #296行 备份文件的权限
gitlab_rails['backup_keep_time'] = 7776000 #301行 备份保留时间(保留90天 单位:秒
gitlab-ctl reconfigure
mkdir /opt/gitlab_backups && chown -R git.git /opt/gitlab_backups/
[root@gitlabdev ~]# gitlab-backup create2021-06-15 10:37:09 +0800 -- Dumping database ...Dumping PostgreSQL database gitlabhq_production ... [DONE]2021-06-15 10:37:12 +0800 -- done2021-06-15 10:37:12 +0800 -- Dumping repositories ... * eda_groups/naura_eda (@hashed/3f/db/3fdba35f04dc8c462986c992bcf875546257113072a909c162f7e470e581e278) ... * eda_groups/naura_eda (@hashed/3f/db/3fdba35f04dc8c462986c992bcf875546257113072a909c162f7e470e581e278) ... [DONE] * eda_groups/naura_eda.wiki (@hashed/3f/db/3fdba35f04dc8c462986c992bcf875546257113072a909c162f7e470e581e278.wiki) ... * eda_groups/naura_eda.wiki (@hashed/3f/db/3fdba35f04dc8c462986c992bcf875546257113072a909c162f7e470e581e278.wiki) ... [EMPTY] [SKIPPED] * eda_groups/naura_eda.design (@hashed/3f/db/3fdba35f04dc8c462986c992bcf875546257113072a909c162f7e470e581e278.design) ... * eda_groups/naura_eda.design (@hashed/3f/db/3fdba35f04dc8c462986c992bcf875546257113072a909c162f7e470e581e278.design) ... [EMPTY] [SKIPPED]2021-06-15 10:37:15 +0800 -- done2021-06-15 10:37:15 +0800 -- Dumping uploads ...2021-06-15 10:37:15 +0800 -- done2021-06-15 10:37:15 +0800 -- Dumping builds ...2021-06-15 10:37:15 +0800 -- done2021-06-15 10:37:15 +0800 -- Dumping artifacts ...2021-06-15 10:37:15 +0800 -- done2021-06-15 10:37:15 +0800 -- Dumping pages ...2021-06-15 10:37:15 +0800 -- done2021-06-15 10:37:15 +0800 -- Dumping lfs objects ...2021-06-15 10:37:15 +0800 -- done2021-06-15 10:37:15 +0800 -- Dumping container registry images ...2021-06-15 10:37:15 +0800 -- [DISABLED]Creating backup archive: 1623724635_2021_06_15_13.12.3_gitlab_backup.tar ... doneUploading backup archive to remote storage ... skippedDeleting tmp directories ... donedonedonedonedonedonedonedoneDeleting old backups ... done. (0 removed)Warning: Your gitlab.rb and gitlab-secrets.json files contain sensitive dataand are not included in this backup. You will need these files to restore a backup.Please back them up manually.Backup task is done.
[root@gitlabdev ~]# gitlab-backup create
2021-06-15 10:37:09 +0800 -- Dumping database ...
Dumping PostgreSQL database gitlabhq_production ... [DONE]
2021-06-15 10:37:12 +0800 -- done
2021-06-15 10:37:12 +0800 -- Dumping repositories ...
* eda_groups/naura_eda (@hashed/3f/db/3fdba35f04dc8c462986c992bcf875546257113072a909c162f7e470e581e278) ...
* eda_groups/naura_eda (@hashed/3f/db/3fdba35f04dc8c462986c992bcf875546257113072a909c162f7e470e581e278) ... [DONE]
* eda_groups/naura_eda.wiki (@hashed/3f/db/3fdba35f04dc8c462986c992bcf875546257113072a909c162f7e470e581e278.wiki) ...
* eda_groups/naura_eda.wiki (@hashed/3f/db/3fdba35f04dc8c462986c992bcf875546257113072a909c162f7e470e581e278.wiki) ... [EMPTY] [SKIPPED]
* eda_groups/naura_eda.design (@hashed/3f/db/3fdba35f04dc8c462986c992bcf875546257113072a909c162f7e470e581e278.design) ...
* eda_groups/naura_eda.design (@hashed/3f/db/3fdba35f04dc8c462986c992bcf875546257113072a909c162f7e470e581e278.design) ... [EMPTY] [SKIPPED]
2021-06-15 10:37:15 +0800 -- done
2021-06-15 10:37:15 +0800 -- Dumping uploads ...
2021-06-15 10:37:15 +0800 -- Dumping builds ...
2021-06-15 10:37:15 +0800 -- Dumping artifacts ...
2021-06-15 10:37:15 +0800 -- Dumping pages ...
2021-06-15 10:37:15 +0800 -- Dumping lfs objects ...
2021-06-15 10:37:15 +0800 -- Dumping container registry images ...
2021-06-15 10:37:15 +0800 -- [DISABLED]
Creating backup archive: 1623724635_2021_06_15_13.12.3_gitlab_backup.tar ... done
Uploading backup archive to remote storage ... skipped
Deleting tmp directories ... done
done
Deleting old backups ... done. (0 removed)
Warning: Your gitlab.rb and gitlab-secrets.json files contain sensitive data
and are not included in this backup. You will need these files to restore a backup.
Please back them up manually.
Backup task is done.
ll -sh /opt/gitlab_backups/
编写备份脚本,结合crontab实施自动定时备份,比如每天0点、6点、12点、18点各备份一次
#!/usr/bin/bash#获取当前时间locale_date=`date +%Y-%m-%d.%H.%M.%S`#远端IP备份主机ipbackup_host=192.168.101.133#本地备份路径backup_path=/opt/gitlab_backups#日志路径backup_log=/opt/gitlab_backups/gitlab_back.log#CRON=1 环境变量CRON=1的作用是如果没有任何错误发生时, 抑制备份脚本的所有进度输出#BACKUP=${locale_date}改变backup文件名称 例: 2021-06-15_11:22:52_gitlab_backup.tar/opt/gitlab/bin/gitlab-backup create BACKUP=${locale_date} CRON=1if [ $? -eq 0 ];then echo "${locale_date} ${backup_path}_gitlab_backup.tar 备份创建成功." >> ${backup_log}else echo "${locale_date} ${backup_path}_gitlab_backup.tar 备份创建失败." >> ${backup_log} exit 1fi#判断/opt/gitlab_backups目录是否存在,否则创建if [ ! -d ${backup_path} ]; then mkdir ${backup_path}fi#拷贝配置文件至本地备份目录/opt/gitlab_backupscp -af /etc/gitlab/gitlab-secrets.json ${backup_path}/${locale_date}_gitlab-secrets.json >> ${backup_log}cp -af /etc/gitlab/gitlab.rb ${backup_path}/${locale_date}_gitlab.rb >> ${backup_log}#同步本地 /opt/gitlab_backups目录到远端/opt/目录下rsync -avzPr --delete /opt/gitlab_backups root@${backup_host}:/opt/ >> ${backup_log}
#!/usr/bin/bash
#获取当前时间
locale_date=`date +%Y-%m-%d.%H.%M.%S`
#远端IP备份主机ip
backup_host=192.168.101.133
#本地备份路径
backup_path=/opt/gitlab_backups
#日志路径
backup_log=/opt/gitlab_backups/gitlab_back.log
#CRON=1 环境变量CRON=1的作用是如果没有任何错误发生时, 抑制备份脚本的所有进度输出
#BACKUP=${locale_date}改变backup文件名称 例: 2021-06-15_11:22:52_gitlab_backup.tar
/opt/gitlab/bin/gitlab-backup create BACKUP=${locale_date} CRON=1
if [ $? -eq 0 ];then
echo "${locale_date} ${backup_path}_gitlab_backup.tar 备份创建成功." >> ${backup_log}
else
echo "${locale_date} ${backup_path}_gitlab_backup.tar 备份创建失败." >> ${backup_log}
exit 1
fi
#判断/opt/gitlab_backups目录是否存在,否则创建
if [ ! -d ${backup_path} ]; then
mkdir ${backup_path}
#拷贝配置文件至本地备份目录/opt/gitlab_backups
cp -af /etc/gitlab/gitlab-secrets.json ${backup_path}/${locale_date}_gitlab-secrets.json >> ${backup_log}
cp -af /etc/gitlab/gitlab.rb ${backup_path}/${locale_date}_gitlab.rb >> ${backup_log}
#同步本地 /opt/gitlab_backups目录到远端/opt/目录下
rsync -avzPr --delete /opt/gitlab_backups root@${backup_host}:/opt/ >> ${backup_log}
[root@gitlabdev ~]# chmod +x /opt/gitlab_backups/gitlab_back.sh
crontab -e #添加定时任务crontab -l #查看已添加定时任务[root@gitlabdev ~]# crontab -l0 0,6,12,18 * * * /bin/bash /opt/gitlab_backups/gitlab_back.sh > /dev/null 2>&1
crontab -e #添加定时任务
crontab -l #查看已添加定时任务
[root@gitlabdev ~]# crontab -l
0 0,6,12,18 * * * /bin/bash /opt/gitlab_backups/gitlab_back.sh > /dev/null 2>&1
原文链接:http://www.cnblogs.com/afei654138148/p/14884439.html
本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728