diff --git a/README.md b/README.md index 6c28939..5fc778e 100644 --- a/README.md +++ b/README.md @@ -5,6 +5,7 @@ [![Docker Pulls](https://img.shields.io/docker/pulls/pgollor/gitbucket.svg)](https://hub.docker.com/r/pgollor/gitbucket/) [![](https://images.microbadger.com/badges/image/pgollor/gitbucket.svg)](https://microbadger.com/images/pgollor/gitbucket "Get your own image badge on microbadger.com") [![](https://images.microbadger.com/badges/version/pgollor/gitbucket.svg)](https://microbadger.com/images/pgollor/gitbucket "Get your own version badge on microbadger.com") +![](https://img.shields.io/github/tag-date/gitbucket/gitbucket.svg?label=gitbucket%20latest) This docker container of [gitbucket](https://github.com/gitbucket/gitbucket.git) is in beta state! @@ -66,6 +67,7 @@ ### Update Update your gitbucket image in three steps. But first of all: **MAKE A BACKUP!!!** +Without backup the host ssh keys will be lost. #### from 4.19.3 to 4.20.0 You have to backup your `gitbucket.conf` because this config does not exist in the repository any more. @@ -86,7 +88,12 @@ 1. Commit your local changes. Changes in `gitbucket.conf` will be ignored! -2. get the new image +2. Backup +``` +./backup.sh backup all +``` + +3. get the new image Shutdown and remove your images. This will not delete your mysql database volume. ``` docker-compose down @@ -97,12 +104,17 @@ docker-compose up -d --remove-orphans ``` -3. cleanup your docker environment +4. cleanup your docker environment This step is optional. Please do this only if you understand the next line. ``` docker rmi -f $(docker images -f "dangling=true" -q) ``` +5. restore ssh keys +``` +./backup.sh restore sshkeys +``` + ### Plugins To use plugins download the plugin and move it into `data/plugins`. @@ -110,6 +122,16 @@ ### Backup + +Since 4.30.0 the backupplugin is included. +Automatic backup for repository works, but the E-Mail notification does not work yet. +If you want a full backup, please use `backup.sh` like: + +``` +./backup.sh backup all +``` + +#### Full Backup via `backup.sh` For backuping the mysql database and the repositories you could use the `backup.sh` script and combine it with a daily cronjob. This script will create a compressed backup and keep the files 10 days in the backup directory. All files which are older then 10 days will be deleted. diff --git a/backup.sh b/backup.sh index c54f3b0..4809798 100755 --- a/backup.sh +++ b/backup.sh @@ -3,46 +3,126 @@ # backup directory backupDir=./backup + +# current date +currentDate=$(date +"%Y-%m-%d_%H-%M-%S") + +# check for first parameter +if [[ ! ${1} =~ (backup|restore) ]]; then + echo "First parameter needs to be 'backup' or 'restore'" + exit 1 +fi + +if [[ ${1} == "backup" && ! ${2} =~ (db|repos|confs|gist|data|sshkeys|all) ]]; then + echo "Second parameter needs to be 'db', 'repos', 'confs', 'gist', 'data', 'sshkeys' or 'all'" + exit 1 +fi + + +# create backup directories mkdir -p ${backupDir} mkdir -p ${backupDir}/db mkdir -p ${backupDir}/conf +mkdir -p ${backupDir}/repositories if [ -d "data/data" ]; then mkdir -p ${backupDir}/data fi if [ -d "data/gist" ]; then mkdir -p ${backupDir}/gist fi -mkdir -p ${backupDir}/repositories - -# current date -currentDate=$(date +"%Y-%m-%d_%H-%M-%S") -# backup mysql -dbFile="${backupDir}/db/${currentDate}.sql" -docker-compose exec mysql-gitbucket sh -c 'exec mysqldump --lock-tables --default-character-set=utf8mb4 -uroot -p"${MYSQL_ROOT_PASSWORD}" ${MYSQL_DATABASE}' > ${dbFile} -sed -i "/^mysqldump: \\[Warning\\]/d" ${dbFile} -tar -cj ${dbFile} -f "${dbFile}.tbz2" -rm ${dbFile} +function backup() { -# backup repositories -repoFile="${backupDir}/repositories/${currentDate}.tbz2" -tar -cj data/repositories -f ${repoFile} + IDmain=$(docker ps -qf name=main-gitbucket) + IDdb=$(docker ps -qf name=mysql-gitbucket) -# backup config files -tar -cj data/conf -f "${backupDir}/conf/${currentDate}.tbz2" + while (( "$#" )); do + case "$1" in + db|all) -# backup gist repositories if existing -if [ -d "data/gist" ]; then - repoFile="${backupDir}/gist/${currentDate}.tbz2" - tar -cj data/gist -f ${repoFile} + echo "backup database" + dbFile="${backupDir}/db/${currentDate}.sql" + docker exec $IDdb sh -c 'exec mysqldump --lock-tables --default-character-set=utf8mb4 -uroot -p"${MYSQL_ROOT_PASSWORD}" ${MYSQL_DATABASE}' > ${dbFile} + sed -i "/^mysqldump: \\[Warning\\]/d" ${dbFile} + tar -cj ${dbFile} -f "${dbFile}.tbz2" + rm ${dbFile} + + ;;& + repos|all) + + echo "backup repositories" + repoFile="${backupDir}/repositories/${currentDate}.tbz2" + tar -cj data/repositories -f ${repoFile} + + # copy auto backup folder + if [ -d "data/backup" ]; then + cp -R data/backup "${backupDir}/autoBackup" + fi + + ;;& + confs|all) + + echo "backup config files" + tar -cj data/conf gitbucket.conf -f "${backupDir}/conf/${currentDate}.tbz2" + + ;;& + gist|all) + + if [ -d "data/gist" ]; then + echo "backup gist directory" + repoFile="${backupDir}/gist/${currentDate}.tbz2" + tar -cj data/gist -f ${repoFile} + fi + + ;;& + data|all) + + if [ -d "data/data" ]; then + echo "backup data directory" + repoFile="${backupDir}/data/${currentDate}.tbz2" + tar -cj data/data -f ${repoFile} + fi + + ;;& + sshkeys|all) + + echo "try to backup ssh keys if available" + docker cp ${IDmain}:/srv/gitbucket/gitbucket.ser "${backupDir}/gitbucket.ser" 2> /dev/null + + ;; + esac + shift + done + + # delete all files older 10 days + #find ${backupDir} -iname "*.tbz2" -type f -mtime +10 -exec rm {} \; > /dev/null +} + +function restore() { + while (( "$#" )); do + case "$1" in + sshkeys|all) + + if [ ! -f "${backupDir}/gitbucket.ser" ]; then + echo "${backupDir}/gitbucket.ser does not exist" + exit 1 + fi + + IDmain=$(docker ps -qf name=main-gitbucket) + + echo "restore ssh keys" + docker cp "${backupDir}/gitbucket.ser" ${IDmain}:/srv/gitbucket/gitbucket.ser + + ;; + esac + shift + done +} + +if [[ ${1} == "backup" ]]; then + backup ${@,,} +elif [[ ${1} == "restore" ]]; then + restore ${@,,} fi -# backup data if existing -if [ -d "data/data" ]; then - repoFile="${backupDir}/data/${currentDate}.tbz2" - tar -cj data/data -f ${repoFile} -fi - -# delete all files older 10 days -find ${backupDir} -iname "*.tbz2" -type f -mtime +10 -exec rm {} \; > /dev/null diff --git a/data/conf/backup.conf b/data/conf/backup.conf new file mode 100644 index 0000000..b2fcfb4 --- /dev/null +++ b/data/conf/backup.conf @@ -0,0 +1,20 @@ +# Backup timing (Required) +# For details, see http://www.quartz-scheduler.org/documentation/quartz-2.x/tutorials/crontrigger.html +# This example, backup 12am every day +akka { + quartz { + schedules { + Backup { + expression = "0 0 0 * * ?" + } + } + } +} + +backup { + archive-destination = """/srv/gitbucket/backup""" + + # Maximum number of backup archives to keep (if 0 or negative value, keep unlimited) (Optional) + # If not specified, keep unlimited + archive-limit = 10 +} diff --git a/docker-compose.yml b/docker-compose.yml index fe41368..f02ef95 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -2,7 +2,7 @@ services: main-gitbucket: - image: pgollor/gitbucket:latest + image: pgollor/gitbucket:develop mem_limit: 2g restart: always depends_on: @@ -20,8 +20,9 @@ - ./data/data/:/srv/gitbucket/data/ - ./data/gist/:/srv/gitbucket/gist/ - ./data/plugins/:/srv/gitbucket/plugins/ - #- ./data/tmp/:/srv/gitbucket/tmp/ + - ./data/backup/:/srv/gitbucket/backup/ - ./data/conf/gitbucket/gitbucket.conf:/srv/gitbucket/gitbucket.conf + - ./data/conf/backup.conf:/srv/gitbucket/backup.conf tmpfs: - /tmp ports: diff --git a/docker/Dockerfile b/docker/Dockerfile index 753de94..2d561cb 100644 --- a/docker/Dockerfile +++ b/docker/Dockerfile @@ -2,20 +2,16 @@ LABEL maintainer "Pascal Gollor " # ports -EXPOSE 29418 -EXPOSE 8080 +EXPOSE 29418/tcp +EXPOSE 8080/tcp # environment variables ENV GITBUCKET_HOST 0.0.0.0 ENV GITBUCKET_PORT 8080 ENV GITBUCKET_HOME /srv/gitbucket -# mark volumes -VOLUME $GITBUCKET_HOME/repositories -VOLUME $GITBUCKET_HOME/data -VOLUME $GITBUCKET_HOME/gist -VOLUME $GITBUCKET_HOME/plugins -#VOLUME $GITBUCKET_HOME/tmp +# create home +RUN mkdir -p $GITBUCKET_HOME # update mirror RUN echo http://ftp.halifax.rwth-aachen.de/alpine/latest-stable/main > /etc/apk/repositories; \ @@ -28,10 +24,23 @@ # get gitbucket RUN wget https://github.com/gitbucket/gitbucket/releases/download/4.30.1/gitbucket.war -O $GITBUCKET_HOME/latest.war +# create plugin dir +RUN mkdir $GITBUCKET_HOME/plugins + +# get backup plugin +RUN wget https://github.com/jyuch/gitbucket-backup-plugin/releases/download/1.2.1/gitbucket-backup-plugin-gitbucket_4.29.0-1.2.1.jar -O $GITBUCKET_HOME/gitbucket-backup-plugin-gitbucket_4.29.0-1.2.1.jar + # clean RUN apk del wget RUN rm -rf /var/cache/apk/* +# mark volumes +VOLUME $GITBUCKET_HOME/repositories +VOLUME $GITBUCKET_HOME/data +VOLUME $GITBUCKET_HOME/gist +VOLUME $GITBUCKET_HOME/plugins +VOLUME $GITBUCKET_HOME/backup + # set environment WORKDIR $GITBUCKET_HOME diff --git a/docker/docker-entrypoint.sh b/docker/docker-entrypoint.sh index c82801f..6606dfd 100644 --- a/docker/docker-entrypoint.sh +++ b/docker/docker-entrypoint.sh @@ -19,4 +19,9 @@ sed -i "s/GITBUCKET_DATABASE_PASSWORD/${GITBUCKET_DATABASE_PASSWORD}/" $dbfile sed -i "s/GITBUCKET_DATABASE_USER/${GITBUCKET_DATABASE_USER}/" $dbfile +# download backup plugin if not present +if [ ! -f "$GITBUCKET_HOME/plugins/gitbucket-backup-plugin-gitbucket_4.29.0-1.2.1.jar" ]; then + mv $GITBUCKET_HOME/gitbucket-backup-plugin-gitbucket_4.29.0-1.2.1.jar $GITBUCKET_HOME/plugins/gitbucket-backup-plugin-gitbucket_4.29.0-1.2.1.jar +fi + exec "$@"