運行環境
- docker
- gitlab
- crontab
- rsync
- sshpass
文件層級關系
- /home
- deployment
- backup.sh
- sync.sh
- restore.sh
backup.sh
# 創建備份
gitlab-rake gitlab:backup:create
# 獲取最新備份的文件名
backupName=$(ls -lt /home/gitlab/backup | grep gitlab_backup.tar | head -n 1 |awk '{print $9}');
# 獲取文件名的前28位
backupName=${backupName:0:28}
# 調用同步腳本
sh /home/deployment/sync.sh $backupName
# 調用恢復腳本
sh /home/deployment/restore.sh $backupName
sync.sh
# 同步到雲
# 備份文件名
backupName=$1
sshpass -p "密碼" rsync --progress "/home/gitlab/backup/"$backupName"_gitlab_backup.tar" root@服務器ip:"/root/gitlab/"$backupName"_gitlab_backup.tar"
restore.sh
# 備份名稱 例:backupName="1590975087_2020_06_01_11.9.8";
backupName=$1;
# 備份文件全名
backupFullName=$backupName"_gitlab_backup.tar"
# 復制到目錄文件下
cp "/home/gitlab/backup/"$backupFullName /home/deployment/gitlab/data/backups/
# 設置權限
chmod 777 "/home/deployment/gitlab/data/backups/"$backupFullName
# 執行恢復
yes yes|docker exec -i gitlab gitlab-rake gitlab:backup:restore BACKUP=""$backupName
crontab
# 編輯
crontab -e
# 每天 23:00 執行
00 23 * * * /bin/sh /home/deployment/backup.sh
# 查看
crontab -l
# 重啟 crontab
/bin/systemctl restart crond.service
工作示意圖
- GitLab [宿主機]
- ⌚23:00調用
backup.sh
restore.sh
本地備份並恢復到docker備用gitlab上
sync.sh
同步指定服務器