Skip to content

Instantly share code, notes, and snippets.

@duruyao
Last active May 17, 2024 02:36
Show Gist options
  • Save duruyao/3d82cac5ce4041e27023d7644fc3878d to your computer and use it in GitHub Desktop.
Save duruyao/3d82cac5ce4041e27023d7644fc3878d to your computer and use it in GitHub Desktop.
Automatically backup Omnibus GitLab packages to local and remote periodically.
#!/usr/bin/env bash
## date: 2024-02-29
## author: duruyao@vimicro.com
## desc: backup 'omnibus' gitlab packages to local and remote
set -euo pipefail
current_date() {
date +"%Y-%m-%d"
}
current_time() {
date +"%Y-%m-%d %T"
}
local_ip="10.0.13.134"
local_gitlab_backups_dir="/opt/gitlab-data/gitlab-backups"
local_gitlab_backups_log_path="${local_gitlab_backups_dir}/backup.log"
local_gitlab_backups_data_dir="${local_gitlab_backups_dir}/data"
local_gitlab_backups_conf_dir="${local_gitlab_backups_dir}/conf"
local_gitlab_test_dir="/opt/gitlab-data/gitlab-test"
## backup gitlab to local
{
printf "\n\n============================== %s ==============================\n\n\n" "$(current_date)"
printf "[INFO] backup gitlab to %s:%s starting from [%s]\n" "${local_ip}" "${local_gitlab_backups_dir}" "$(current_time)"
sudo mkdir -p "${local_gitlab_backups_dir}"
sudo mkdir -p "${local_gitlab_backups_conf_dir}"
sudo mkdir -p "${local_gitlab_backups_data_dir}"
sudo chown git.git "${local_gitlab_backups_dir}"
sudo rsync -arv /etc/gitlab/* "${local_gitlab_backups_conf_dir}"/
sudo gitlab-backup create STRATEGY=copy BACKUP=dump GZIP_RSYNCABLE=yes GITLAB_BACKUP_MAX_CONCURRENCY=32 GITLAB_BACKUP_MAX_STORAGE_CONCURRENCY=4
printf "[INFO] backup gitlab to %s:%s end at [%s]\n" "${local_ip}" "${local_gitlab_backups_dir}" "$(current_time)"
if [ -n "$(command -v tree)" ]; then
printf "[INFO] %s:" "${local_ip}"
tree -L 3 "${local_gitlab_backups_dir}"
fi
} &>>"${local_gitlab_backups_log_path}"
remote_ip="10.0.13.120"
remote_gitlab_backups_dir="/opt/gitlab-data/gitlab-backups"
remote_gitlab_test_dir="/opt/gitlab-data/gitlab-test"
remote_user="duruyao"
remote_pwd="duruyao"
## backup gitlab to remote
{
printf "[INFO] backup gitlab to %s:%s starting from [%s]\n" "${remote_ip}" "${remote_gitlab_backups_dir}" "$(current_time)"
sudo sshpass -p "${remote_pwd}" rsync -arvz "${local_gitlab_backups_dir}"/* "${remote_user}"@"${remote_ip}":"${remote_gitlab_backups_dir}"/
printf "[INFO] backup gitlab to %s:%s end at [%s]\n" "${remote_ip}" "${remote_gitlab_backups_dir}" "$(current_time)"
printf "[INFO] backup gitlab to %s:%s starting from [%s]\n" "${remote_ip}" "${remote_gitlab_test_dir}" "$(current_time)"
sudo sshpass -p "${remote_pwd}" rsync -arvz "${local_gitlab_test_dir}"/* "${remote_user}"@"${remote_ip}":"${remote_gitlab_test_dir}"/
printf "[INFO] backup gitlab to %s:%s end at [%s]\n" "${remote_ip}" "${remote_gitlab_test_dir}" "$(current_time)"
} &>>"${local_gitlab_backups_log_path}"
## append log to remote
sudo sshpass -p "${remote_pwd}" rsync -arvz "${local_gitlab_backups_log_path}" "${remote_user}"@"${remote_ip}":"${remote_gitlab_backups_dir}"/
#!/usr/bin/env bash
## date: 2021-11-11
## author: duruyao@vimicro.com
## desc: automatically backup omnibus gitlab packages to local and remote at 2:00
set -euo pipefail
script_path="/home/duruyao/bin/gitlab-tools/backup-gitlab.sh"
auto_tasks=("0 2 * * * root ${script_path} CRON=1")
for task in "${auto_tasks[@]}"; do
if ! grep -Fxq "${task}" /etc/crontab; then
sudo echo "${task}" >>/etc/crontab
fi
done
sudo service cron restart
@duruyao
Copy link
Author

duruyao commented Nov 11, 2021

Priority reference Back up and restore GitLab.

Scripts for restoring GitLab is in https://gist.github.com/duruyao/auto-restore-gitlab.sh.

Change some values of variables before you using my scripts, such as remote_user, remote_pwd, script_path and more.

$ chmod +x backup-gitlab.sh auto-backup-gitlab.sh
$ sudo bash auto-backup-gitlab.sh

All logs are stored in ${local_gitlab_backups_log_path}.

$ tail -f /opt/gitlab-data/gitlab-backups/backups.log

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment