|
| 1 | +#!/usr/bin/env bash |
| 2 | + |
| 3 | +# ----------------------------------------------------------------------------------------------------- |
| 4 | +# Gitlab 操作脚本 |
| 5 | +# 支持操作: |
| 6 | +# 备份 Gitlab |
| 7 | +# 恢复 Gitlab |
| 8 | +# @author: Zhang Peng |
| 9 | +# ----------------------------------------------------------------------------------------------------- |
| 10 | + |
| 11 | +# ------------------------------------------------------------------------------ env |
| 12 | + |
| 13 | +# Regular Color |
| 14 | +export ENV_COLOR_BLACK="033円[0;30m" |
| 15 | +export ENV_COLOR_RED="033円[0;31m" |
| 16 | +export ENV_COLOR_GREEN="033円[0;32m" |
| 17 | +export ENV_COLOR_YELLOW="033円[0;33m" |
| 18 | +export ENV_COLOR_BLUE="033円[0;34m" |
| 19 | +export ENV_COLOR_MAGENTA="033円[0;35m" |
| 20 | +export ENV_COLOR_CYAN="033円[0;36m" |
| 21 | +export ENV_COLOR_WHITE="033円[0;37m" |
| 22 | +# Bold Color |
| 23 | +export ENV_COLOR_B_BLACK="033円[1;30m" |
| 24 | +export ENV_COLOR_B_RED="033円[1;31m" |
| 25 | +export ENV_COLOR_B_GREEN="033円[1;32m" |
| 26 | +export ENV_COLOR_B_YELLOW="033円[1;33m" |
| 27 | +export ENV_COLOR_B_BLUE="033円[1;34m" |
| 28 | +export ENV_COLOR_B_MAGENTA="033円[1;35m" |
| 29 | +export ENV_COLOR_B_CYAN="033円[1;36m" |
| 30 | +export ENV_COLOR_B_WHITE="033円[1;37m" |
| 31 | +# Reset Color |
| 32 | +export ENV_COLOR_RESET="$(tput sgr0)" |
| 33 | + |
| 34 | +# status |
| 35 | +export ENV_YES=0 |
| 36 | +export ENV_NO=1 |
| 37 | +export ENV_SUCCEED=0 |
| 38 | +export ENV_FAILED=1 |
| 39 | + |
| 40 | +# ------------------------------------------------------------------------------ functions |
| 41 | + |
| 42 | +# 显示打印日志的时间 |
| 43 | +SHELL_LOG_TIMESTAMP=$(date "+%Y-%m-%d %H:%M:%S") |
| 44 | +# 那个用户在操作 |
| 45 | +USER=$(whoami) |
| 46 | + |
| 47 | +redOutput() { |
| 48 | + echo -e "${ENV_COLOR_RED} $@${ENV_COLOR_RESET}" |
| 49 | +} |
| 50 | +greenOutput() { |
| 51 | + echo -e "${ENV_COLOR_B_GREEN} $@${ENV_COLOR_RESET}" |
| 52 | +} |
| 53 | +yellowOutput() { |
| 54 | + echo -e "${ENV_COLOR_YELLOW} $@${ENV_COLOR_RESET}" |
| 55 | +} |
| 56 | +blueOutput() { |
| 57 | + echo -e "${ENV_COLOR_BLUE} $@${ENV_COLOR_RESET}" |
| 58 | +} |
| 59 | +magentaOutput() { |
| 60 | + echo -e "${ENV_COLOR_MAGENTA} $@${ENV_COLOR_RESET}" |
| 61 | +} |
| 62 | +cyanOutput() { |
| 63 | + echo -e "${ENV_COLOR_CYAN} $@${ENV_COLOR_RESET}" |
| 64 | +} |
| 65 | +whiteOutput() { |
| 66 | + echo -e "${ENV_COLOR_WHITE} $@${ENV_COLOR_RESET}" |
| 67 | +} |
| 68 | + |
| 69 | +printInfo() { |
| 70 | + echo -e "${ENV_COLOR_B_GREEN}[INFO] $@${ENV_COLOR_RESET}" |
| 71 | +} |
| 72 | +printWarn() { |
| 73 | + echo -e "${ENV_COLOR_B_YELLOW}[WARN] $@${ENV_COLOR_RESET}" |
| 74 | +} |
| 75 | +printError() { |
| 76 | + echo -e "${ENV_COLOR_B_RED}[ERROR] $@${ENV_COLOR_RESET}" |
| 77 | +} |
| 78 | + |
| 79 | +callAndLog () { |
| 80 | + $* |
| 81 | + if [[ $? -eq ${ENV_SUCCEED} ]]; then |
| 82 | + printInfo "$@" |
| 83 | + return ${ENV_SUCCEED} |
| 84 | + else |
| 85 | + printError "$@ EXECUTE FAILED" |
| 86 | + return ${ENV_FAILED} |
| 87 | + fi |
| 88 | +} |
| 89 | + |
| 90 | +# ------------------------------------------------------------------------------ env |
| 91 | + |
| 92 | +# Gilab 操作的环境变量,使用方法: |
| 93 | +# 可以在执行本脚本之前,export 以下环境变量,否则将按照默认配置执行 |
| 94 | + |
| 95 | +# Gitlab 备份文件最大数量(默认为 7 天) |
| 96 | +ENV_BACKUP_MAX_NUM=${ENV_BACKUP_MAX_NUM:-2} |
| 97 | +# 备份路径 |
| 98 | +ENV_GITLAB_BACKUP_DIR="${ENV_GITLAB_BACKUP_DIR:-/var/opt/gitlab/backups}" |
| 99 | +# 备份日志路径 |
| 100 | +ENV_LOG_PATH="${ENV_GITLAB_BACKUP_DIR}/gitlab-backup.log" |
| 101 | + |
| 102 | +magentaOutput "------------------------------------------------------------------------------" |
| 103 | +magentaOutput "Gitlab 脚本操作环境变量:" |
| 104 | +magentaOutput "ENV_BACKUP_MAX_NUM:${ENV_BACKUP_MAX_NUM}" |
| 105 | +magentaOutput "ENV_GITLAB_BACKUP_DIR:${ENV_GITLAB_BACKUP_DIR}" |
| 106 | +magentaOutput "ENV_LOG_PATH:${ENV_LOG_PATH}" |
| 107 | +magentaOutput "------------------------------------------------------------------------------" |
| 108 | + |
| 109 | + |
| 110 | +# ------------------------------------------------------------------------------ functions |
| 111 | + |
| 112 | +# 安装 Gitlab |
| 113 | +installGitlab() { |
| 114 | + # 官方安裝參考:https://about.gitlab.com/install/#centos-7 |
| 115 | + printInfo ">>>> install gitlab on Centos7" |
| 116 | + printInfo ">>>> Install and configure the necessary dependencies" |
| 117 | + yum install -y curl policycoreutils-python openssh-server |
| 118 | + systemctl enable sshd |
| 119 | + systemctl start sshd |
| 120 | + printInfo ">>>> open http, https and ssh access in the system firewall" |
| 121 | + sudo firewall-cmd --permanent --add-service=http |
| 122 | + sudo firewall-cmd --permanent --add-service=https |
| 123 | + sudo systemctl reload firewalld |
| 124 | + printInfo ">>>> install postfix" |
| 125 | + yum install postfix |
| 126 | + systemctl enable postfix |
| 127 | + systemctl start postfix |
| 128 | + printInfo ">>>> Add the GitLab package repository and install the package" |
| 129 | + curl -o- https://packages.gitlab.com/install/repositories/gitlab/gitlab-ce/script.rpm.sh | bash |
| 130 | + EXTERNAL_URL="http://gitlab.transwarp.io" yum install -y gitlab-ce |
| 131 | +} |
| 132 | + |
| 133 | +# 备份 Gitlab |
| 134 | +backupGitlab() { |
| 135 | + |
| 136 | + #时间戳 |
| 137 | + local beginTime=$(date +'%Y-%m-%d %H:%M:%S') |
| 138 | + |
| 139 | + #备份所有数据库 |
| 140 | + printInfo ">>>> 备份 Gitlab 开始" |
| 141 | + gitlab-rake gitlab:backup:create >> ${ENV_LOG_PATH}; |
| 142 | + |
| 143 | + #检查备份结果是否成功 |
| 144 | + if [[ "$?" != ${ENV_SUCCEED} ]]; then |
| 145 | + printError "<<<< 备份 Gitlab 失败" |
| 146 | + return ${ENV_FAILED} |
| 147 | + fi |
| 148 | + |
| 149 | + # 压缩备份sql文件,删除旧的备份文件 |
| 150 | + cd "${ENV_GITLAB_BACKUP_DIR}" |
| 151 | + |
| 152 | + #只保存期限内的备份文件,其余删除 |
| 153 | + find "${ENV_GITLAB_BACKUP_DIR} -name *_gitlab_backup.tar -type f -mtime +${ENV_BACKUP_MAX_NUM} -exec rm -rf {} \;" > /dev/null 2>&1 |
| 154 | + |
| 155 | + local endTime=$(date +'%Y-%m-%d %H:%M:%S') |
| 156 | + local beginSeconds=$(date --date="${beginTime}" +%s) |
| 157 | + local endSeconds=$(date --date="${endTime}" +%s) |
| 158 | + printInfo "本次备份执行时间:$((endSeconds-beginSeconds)) s" |
| 159 | + printInfo "<<<< 备份 Gitlab 成功\n" |
| 160 | + return ${ENV_SUCCEED} |
| 161 | +} |
| 162 | + |
| 163 | +# 恢复 Mysql |
| 164 | +recoveryGitlab() { |
| 165 | + |
| 166 | + local version=1ドル |
| 167 | + if [[ !version ]]; then |
| 168 | + printError "<<<< 未指定恢复版本" |
| 169 | + return ${ENV_FAILED} |
| 170 | + fi |
| 171 | + |
| 172 | + #创建备份目录及日志文件 |
| 173 | + mkdir -p ${ENV_GITLAB_BACKUP_DIR} |
| 174 | + if [[ ! -f ${ENV_LOG_PATH} ]]; then |
| 175 | + touch ${ENV_LOG_PATH} |
| 176 | + fi |
| 177 | + |
| 178 | + printInfo ">>>> 恢复 Gitlab 开始" |
| 179 | + |
| 180 | + gitlab-ctl stop unicorn |
| 181 | + gitlab-ctl stop sidekiq |
| 182 | + |
| 183 | + gitlab-rake gitlab:backup:restore BACKUP=${version} |
| 184 | + if [[ "$?" != 0 ]]; then |
| 185 | + printError "<<<< 恢复 Gitlab 失败" |
| 186 | + return ${ENV_FAILED} |
| 187 | + fi |
| 188 | + |
| 189 | + printInfo "<<<< 恢复 Gitlab 成功\n" |
| 190 | + return ${ENV_SUCCEED} |
| 191 | +} |
0 commit comments