Skip to content

Commit

Permalink
Merge pull request #36 from sund/develop
Browse files Browse the repository at this point in the history
Merge in Develop to Master for Milestone 1.6
  • Loading branch information
sund committed Feb 25, 2016
2 parents 8b45310 + 8081c16 commit 8e65963
Show file tree
Hide file tree
Showing 5 changed files with 122 additions and 71 deletions.
4 changes: 3 additions & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,6 @@ _Unless you hear different, fork and MR._
## Things to keep in mind:
- keep this in ```bash```
- keep it neat and (fairly) easy to read
- Merge Requests should be feature-focused
- Merge requests will be merged to ```develop``` to fix typos and/or bugs.
- Merge requests with a feature-focus will be merged into a different develop branch for testing. (as much as I can... your environment/OS may be different.)
- Try and keep typo and bugs fixes sepereate from feature MRs.
58 changes: 43 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,18 +1,22 @@
## auto gitlab Backup
# Auto GitLab Backup

http://sund.la/glup
[![AGB Logo](https://raw.githubusercontent.com/sund/auto-gitlab-backup/develop/agb_logo.png)](http://sund.la/glup)

----

## Synopsis

A script to use omnibus-gitlab's own backup ```gitlab-rake``` command on a cron schedule and rsync to another server, if wanted. There is also a restore script available (see below.)

It can backup and copy the Gitlab-CI DB, if configured.

This script is now more omnibus-gitlab centric. Compare your config file with the template! Usage with a source install is possible but not expressly shown here.

#### Clone
## Installation

### Prerequisites

clone to your directory of choice. I usually use ```/usr/local/sbin```
Deploy a working GitLab installation and verify you can back it up with the rake task as documented in the [GitLab Documents](http://doc.gitlab.com/ce/raketasks/backup_restore.html).

#### Set up gitlab to expire backups

Expand All @@ -33,7 +37,19 @@ If you use the CI server, enable CI Backup expiration
gitlab_ci['backup_keep_time'] = 604800
```

#### Configure the script for remote copy
### Installation

Clone to your directory of choice. I usually use ```/usr/local/sbin```

```
git clone [email protected]:sund/auto-gitlab-backup.git
```

### Updates

Compare the ```auto-gitlab-backup.conf.sample``` file with your own copy. Make changes as needed to ensure no errors are encountered.

### Configure

```bash
cp auto-gitlab-backup.conf.sample auto-gitlab-backup.conf
Expand All @@ -54,14 +70,21 @@ remoteServer=""
sshKeyPath=""

## $remoteServer path for gitlab backups
remoteDest=""

## Using the CI server?
# change to true or 1 to enable CI backups
enableCIBackup="0"

## $remoteServer dest for CI backups on remote
ciRemoteDest="/var/opt/gitlab/ci-backups"
remoteDest="/var/opt/gitlab/backups"

## set $localConfDir
# blank disables conf backups
# you can create /var/opt/gitlab/backups/configBackups --
# gitlab doesn't seem to complain with a subfolder
# in there. Plus it will rsync up with the backup.
# So you won't need to enable a separate rsync run
localConfDir="/var/opt/gitlab/backups/configBackups"

## set $remoteServer path for gitlab configs
# blank disables remote copy
# unless $localConfDir is outside /var/opt/gitlab/backups/configBackups
# you can leave this blank
remoteConfDest=""

## ssh port or 873 for rsyncd port
remotePort=22
Expand All @@ -78,20 +101,25 @@ RVM_envPath=""
remoteModule=""
rsync_password_file=""

## only change if configs are in different locations. (unlikely)
localConfig="/etc/gitlab"
localsshkeys="/var/opt/gitlab/.ssh"

## Check remote quota
# change to true or 1 to enable
checkQuota="0"

```

#### cron settings
### cron settings

Example for crontab to run at 5:05am everyday.

```bash
5 5 * * * /usr/local/sbin/auto-gitlab-backup/auto-gitlab-backup.sh
```

## restore a backup
## Restore

*Still under development but useful*

Expand Down
Binary file added agb_logo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
33 changes: 27 additions & 6 deletions auto-gitlab-backup.conf.sample
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,23 @@ sshKeyPath=""
## $remoteServer path for gitlab backups
remoteDest="/var/opt/gitlab/backups"

## Using the CI server?
# change to true or 1 to enable CI backups
enableCIBackup="0"

## $remoteServer dest for CI backups on remote
ciRemoteDest="/var/opt/gitlab/ci-backups"
## backup gitlab configs
# change to 1 to enable
backupConfigs="0"

## set $localConfDir
# blank disables conf backups
# you can create /var/opt/gitlab/backups/configBackups --
# gitlab doesn't seem to complain with a subfolder
# in there. Plus it will rsync up with the backup.
# So you won't need to enable a separate rsync run
localConfDir="/var/opt/gitlab/backups/configBackups"

## set $remoteServer path for gitlab configs
# blank disables remote copy
# unless $localConfDir is outside /var/opt/gitlab/backups/configBackups
# you can leave this blank
remoteConfDest=""

## ssh port or 873 for rsyncd port
remotePort=22
Expand All @@ -34,6 +45,16 @@ RVM_envPath=""
remoteModule=""
rsync_password_file=""

## localbackup directories
# only change if gitlab_rails['backup_path'] in gitlab.rb is set
# other than $gitHome/[backups|ci-backups]
gitRakeBackups="/var/opt/gitlab/backups"
gitRakeCIBackups="/var/opt/gitlab/ci-backups"

## only change if configs are in different locations. (unlikely)
localConfig="/etc/gitlab"
localsshkeys="/var/opt/gitlab/.ssh"

## Check remote quota
# change to true or 1 to enable
checkQuota="0"
98 changes: 49 additions & 49 deletions auto-gitlab-backup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,10 @@ export PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:$PATH"
gitHome="$(awk -F: -v v="git" '{if ($1==v) print $6}' /etc/passwd)"
gitlabHome="$gitHome/gitlab"
gitlab_rails="/opt/gitlab/embedded/service/gitlab-rails"
gitRakeBackups="/var/opt/gitlab/backups"
gitRakeCIBackups="/var/opt/gitlab/ci-backups"
PDIR=$(dirname $(readlink -f $0))
dateStamp=`date +"%F %H:%m:%S"`
confFile="$PDIR/auto-gitlab-backup.conf"
rakeBackup="gitlab-rake gitlab:backup:create"
rakeCIBackup="gitlab-ci-rake backup:create"

###
## Functions
Expand All @@ -67,41 +65,42 @@ checkSize() {
echo "Total disk space used for backup storage.."
echo "Size - Location"
echo `du -hs "$gitRakeBackups"`
if [[ $enableCIBackup == "true" || $enableCIBackup = 1 ]]
then
echo `du -hs "$gitRakeCIBackups"`
fi
echo
}

archiveConfig() {
echo ===== Archiving Configs =====
if [[ -w $localConfDir && $backupConfigs = 1 ]]
then
tar -czf "$localConfDir/gitlabConf-$dateStamp.tgz" $localConfig $localsshkeys

# remove files not within 3 days
find $localConfDir -type f -mtime +3 -exec rm -v {} \;

else
echo "Local config backup aren't enabled or $localConfDir is not writable."
fi
}

rakeBackup() {
echo ===== raking a backup =====
cd $gitRakeBackups
$rakeBackup
}

rakeCIBackup() {
if [[ $enableCIBackup == "true" || $enableCIBackup = 1 ]]
then
echo ===== raking a CI backup =====
cd $gitRakeCIBackups
$rakeCIBackup
fi
}

rsyncUp() {
# rsync up with default key
echo =============================================================
echo -e "Start rsync to \n$remoteServer:$remoteDest\ndefault key\n"
rsync -Cavz --delete-after -e "ssh -p$remotePort" $gitRakeBackups/ $remoteUser@$remoteServer:$remoteDest

# rsync CI backup
if [[ $enableCIBackup == "true" || $enableCIBackup = 1 ]]
# config rsync
if [ ! -z $remoteConfDest ]
then
echo ===== rsync a CI backup =====
echo ===== rsync a config backup =====
echo =============================================================
echo -e "Start rsync to \n$remoteServer:$ciRemoteDest\ndefault key\n"
rsync -Cavz --delete-after -e "ssh -p$remotePort" $gitRakeCIBackups/ $remoteUser@$remoteServer:$ciRemoteDest
echo -e "Start rsync to \n$remoteServer:$remoteConfDest\ndefault key\n"
rsync -Cavz --delete-after -e "ssh -p$remotePort" $localConfDir/ $remoteUser@$remoteServer:$remoteConfDest
fi
}

Expand All @@ -111,13 +110,13 @@ rsyncUp_dryrun() {
echo -e "Start dry run rsync to \n$remoteServer:$remoteDest\ndefault key\n"
rsync --dry-run -Cavz --delete-after -e "ssh -p$remotePort" $gitRakeBackups/ $remoteUser@$remoteServer:$remoteDest

# rsync CI backup
if [[ $enableCIBackup == "true" || $enableCIBackup = 1 ]]
# config rsync
if [ ! -z $remoteConfDest ]
then
echo ===== rsync a CI backup =====
echo ===== rsync a config backup =====
echo =============================================================
echo -e "Start rsync to \n$remoteServer:$ciRemoteDest\ndefault key\n"
rsync --dry-run -Cavz --delete-after -e "ssh -p$remotePort" $gitRakeCIBackups/ $remoteUser@$remoteServer:$ciRemoteDest
echo -e "Start dry run rsync to \n$remoteServer:$remoteConfDest\ndefault key\n"
rsync --dry-run -Cavz --delete-after -e "ssh -p$remotePort" $localConfDir/ $remoteUser@$remoteServer:$remoteConfDest
fi
}

Expand All @@ -127,12 +126,13 @@ rsyncKey() {
echo -e "Start rsync to \n$remoteServer:$remoteDest\nwith specific key\n"
rsync -Cavz --delete-after -e "ssh -i $sshKeyPath -p$remotePort" $gitRakeBackups/ $remoteUser@$remoteServer:$remoteDest

# rsync CI backup
if [[ $enableCIBackup == "true" || $enableCIBackup = 1 ]]
# config rsync
if [ ! -z $remoteConfDest ]
then
echo ===== rsync a CI backup =====
echo -e "Start rsync to \n$remoteServer:$ciRemoteDest\nwith specific key\n"
rsync -Cavz --delete-after -e "ssh -i $sshKeyPath -p$remotePort" $gitRakeCIBackups/ $remoteUser@$remoteServer:$ciRemoteDest
echo ===== rsync a config backup =====
echo =============================================================
echo -e "Start rsync to \n$remoteServer:$remoteConfDest\ndefault key\n"
rsync -Cavz --delete-after -e "ssh -p$remotePort" $localConfDir/ $remoteUser@$remoteServer:$remoteConfDest
fi
}

Expand All @@ -142,12 +142,13 @@ rsyncKey_dryrun() {
echo -e "Start dry run rsync to \n$remoteServer:$remoteDest\nwith specific key\n"
rsync --dry-run -Cavz --delete-after -e "ssh -i $sshKeyPath -p$remotePort" $gitRakeBackups/ $remoteUser@$remoteServer:$remoteDest

# rsync CI backup
if [[ $enableCIBackup == "true" || $enableCIBackup = 1 ]]
# config rsync
if [ ! -z $remoteConfDest ]
then
echo ===== rsync a CI backup =====
echo -e "Start rsync to \n$remoteServer:$ciRemoteDest\nwith specific key\n"
rsync --dry-run -Cavz --delete-after -e "ssh -i $sshKeyPath -p$remotePort" $gitRakeCIBackups/ $remoteUser@$remoteServer:$ciRemoteDest
echo ===== rsync a config backup =====
echo =============================================================
echo -e "Start rsync to \n$remoteServer:$remoteConfDest\ndefault key\n"
rsync --dry-run -Cavz --delete-after -e "ssh -p$remotePort" $localConfDir/ $remoteUser@$remoteServer:$remoteConfDest
fi
}

Expand All @@ -158,15 +159,14 @@ rsyncDaemon() {
echo -e "Start rsync to \n$remoteUser@$remoteServer:$remoteModule\nin daemon mode\n"
rsync -Cavz --port=$remotePort --password-file=$rsync_password_file --delete-after /$gitRakeBackups/ $remoteUser@$remoteServer::$remoteModule

# rsync CI backup
if [[ $enableCIBackup == "true" || $enableCIBackup = 1 ]]
# config rsync
if [ ! -z $remoteConfDest ]
then
echo ===== rsync a CI backup =====
echo ===== rsync a config backup =====
echo =============================================================
echo -e "Start rsync to \n$remoteUser@$remoteServer:$remoteCIModule\nin daemon mode\n"
rsync -Cavz --port=$remotePort --password-file=$rsync_password_file --delete-after /$gitRakeCIBackups/ $remoteUser@$remoteServer::$remoteCIModule
echo -e "Start rsync to \n$remoteServer:$remoteConfDest\ndefault key\n"
rsync -Cavz --delete-after -e "ssh -p$remotePort" $localConfDir/ $remoteUser@$remoteServer:$remoteConfDest
fi

}

rsyncDaemon_dryrun() {
Expand All @@ -175,15 +175,14 @@ rsyncDaemon_dryrun() {
echo -e "Start rsync to \n$remoteUser@$remoteServer:$remoteModule\nin daemon mode\n"
rsync --dry-run -Cavz --port=$remotePort --password-file=$rsync_password_file --delete-after /$gitRakeBackups/ $remoteUser@$remoteServer::$remoteModule

# rsync CI backup
if [[ $enableCIBackup == "true" || $enableCIBackup = 1 ]]
# config rsync
if [ ! -z $remoteConfDest ]
then
echo ===== rsync a CI backup =====
echo ===== rsync a config backup =====
echo =============================================================
echo -e "Start rsync to \n$remoteUser@$remoteServer:$remoteCIModule\nin daemon mode\n"
rsync --dry-run -Cavz --port=$remotePort --password-file=$rsync_password_file --delete-after /$gitRakeCIBackups/ $remoteUser@$remoteServer::$remoteCIModule
echo -e "Start rsync to \n$remoteServer:$remoteConfDest\ndefault key\n"
rsync --dry-run -Cavz --delete-after -e "ssh -p$remotePort" $localConfDir/ $remoteUser@$remoteServer:$remoteConfDest
fi

}


Expand Down Expand Up @@ -263,6 +262,7 @@ case $1 in
-d|--dry-run )
areWeRoot $1
confFileExist
archiveConfig
##test ssh and rsync functions
if [[ $remoteModule != "" ]]
then
Expand All @@ -286,7 +286,7 @@ case $1 in
confFileExist
# perform backup
rakeBackup
rakeCIBackup
archiveConfig
checkSize
# go back to where we came from
cd $PDIR
Expand Down

0 comments on commit 8e65963

Please sign in to comment.