Skip to content

Instantly share code, notes, and snippets.

@slowprog
Last active December 31, 2022 11:40
Show Gist options
  • Save slowprog/4aff0a2c839a78a9c342d88facd061f0 to your computer and use it in GitHub Desktop.
Save slowprog/4aff0a2c839a78a9c342d88facd061f0 to your computer and use it in GitHub Desktop.
#!/bin/bash
#
### Description: Script for GitLab files backup. For files moving use https://rclone.org/.
### Example: bash gitlab_backup.sh
RCLONE_NAME=${1:-yandex}
DIST_DIR=${2:-gitlab}
SOURCE_DIR="/var/opt/gitlab/backups"
LOG_FILE="/var/log/gitlab_backup.log"
BACKUP_DATE=$(date +"%m-%d-%y")
BACKUP_AGE=${3:-7} # delete config files older that this value (days)
## check result of operation
operation_result()
{
# arguments:
# $1 - return code
# $2 - action name
if [ ! "$1" -eq 0 ]; then
printf " * There was a problem during [%s] \nSee [%s] for more information!\n" "$2" "$LOG_FILE"
printf "%s: backup job %s failed.\n" "$BACKUP_DATE" >> "$LOG_FILE"
exit 1
fi
printf " * [%s] - Done!\n" "$2" | tee -a "$LOG_FILE"
}
rm $LOG_FILE
## Delete all trashed files
rclone cleanup $RCLONE_NAME:
## Start backup process
gitlab-ctl backup-etc && cd /etc/gitlab/config_backup &>> "$LOG_FILE"
operation_result $? "Backup config files"
## Delete local old config backup
find /etc/gitlab/config_backup/ -type f -mtime +$BACKUP_AGE -name '*.tar' -delete
operation_result $? "Deleted local old config backup"
## Create gitlab backup
/usr/bin/gitlab-rake gitlab:backup:create STRATEGY=copy | tee -a "$LOG_FILE"
operation_result $? "Backup GitLab data via internal backup tool"
## Move config backup
rclone copy /etc/gitlab/config_backup $RCLONE_NAME:$DIST_DIR/config/
operation_result $? "Moved config backup"
## Move GitLab backup
rclone copy /var/opt/gitlab/backups $RCLONE_NAME:$DIST_DIR/backup/
operation_result $? "Moved GitLab backup"
## Delete old config backup
rclone delete $RCLONE_NAME:$DIST_DIR/config/ --min-age $(($BACKUP_AGE + 1))d -v
operation_result $? "Deleted old config backup"
## Delete old GitLab backup
rclone delete $RCLONE_NAME:$DIST_DIR/backup/ --min-age $(($BACKUP_AGE + 1))d -v
operation_result $? "Deleted old GitLab backup"
## Delete all trashed files
rclone cleanup $RCLONE_NAME:
## Writes results in log
printf "Backup job done successfully.\n" | tee -a "$LOG_FILE"
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment