From 8981b9e613fe4e4e9c79d9264bb51aedb53ee214 Mon Sep 17 00:00:00 2001 From: Alexander Date: Sat, 26 Oct 2013 23:46:38 +0200 Subject: [PATCH 1/3] Introduce `backup` plugin to export/import settings --- plugins/00_dokku-standard/backup-export | 8 +++ plugins/00_dokku-standard/backup-import | 13 +++++ plugins/backup/commands | 77 +++++++++++++++++++++++++ plugins/config/backup-export | 5 ++ plugins/config/backup-import | 9 +++ plugins/nginx-vhosts/backup-export | 5 ++ plugins/nginx-vhosts/backup-import | 11 ++++ 7 files changed, 128 insertions(+) create mode 100755 plugins/00_dokku-standard/backup-export create mode 100755 plugins/00_dokku-standard/backup-import create mode 100755 plugins/backup/commands create mode 100755 plugins/config/backup-export create mode 100755 plugins/config/backup-import create mode 100755 plugins/nginx-vhosts/backup-export create mode 100755 plugins/nginx-vhosts/backup-import diff --git a/plugins/00_dokku-standard/backup-export b/plugins/00_dokku-standard/backup-export new file mode 100755 index 000000000..58d4ac43c --- /dev/null +++ b/plugins/00_dokku-standard/backup-export @@ -0,0 +1,8 @@ +#!/bin/bash +VERSION="$1" +BASE_DIR="$2" + +cat; +[[ -f $BASE_DIR/.ssh/authorized_keys ]] && echo "$BASE_DIR/.ssh/authorized_keys" +[[ -f $BASE_DIR/HOSTNAME ]] && echo "$BASE_DIR/HOSTNAME" +[[ -f $BASE_DIR/VHOST ]] && echo "$BASE_DIR/VHOST" diff --git a/plugins/00_dokku-standard/backup-import b/plugins/00_dokku-standard/backup-import new file mode 100755 index 000000000..2fa31c13f --- /dev/null +++ b/plugins/00_dokku-standard/backup-import @@ -0,0 +1,13 @@ +#!/bin/bash +VERSION="$1" +IMPORT_DIR="$2" +TARGET_DIR="$3" + +if [[ -f $IMPORT_DIR/.ssh/authorized_keys ]]; then + mkdir $TARGET_DIR/.ssh + mv $IMPORT_DIR/.ssh/authorized_keys $TARGET_DIR/.ssh/authorized_keys + chmod 0700 $TARGET_DIR/.ssh + chmod 0600 $TARGET_DIR/.ssh/* +fi +[[ -f $IMPORT_DIR/HOSTNAME ]] && mv $IMPORT_DIR/HOSTNAME $TARGET_DIR/HOSTNAME +[[ -f $IMPORT_DIR/VHOST ]] && mv $IMPORT_DIR/VHOST $TARGET_DIR/VHOST diff --git a/plugins/backup/commands b/plugins/backup/commands new file mode 100755 index 000000000..f0cbeb6ed --- /dev/null +++ b/plugins/backup/commands @@ -0,0 +1,77 @@ +#!/bin/bash +set -e; case "$1" in + backup:export) + OUTPUT_FILE="$2" + BACKUP_DIR=/home/git + + if [[ ! -f $BACKUP_DIR/.dokku_backup_version ]]; then + echo "Unable to determine backup version" + exit 1 + fi + + BACKUP_TMP_DIR=$(mktemp -d) + BACKUP_TMP_FILE="$BACKUP_TMP_DIR/backup.tar" + + pluginhook backup-export 1 $BACKUP_DIR | tar -cf $BACKUP_TMP_FILE --files-from - + + # we want to insert the version file in the root of the file + pushd $BACKUP_DIR > /dev/null + tar --append -f $BACKUP_TMP_FILE .dokku_backup_version + ls -d */ > "$BACKUP_TMP_DIR/.dokku_backup_apps" + popd > /dev/null + + # we want to insert the apps file in the root of the file + pushd $BACKUP_TMP_DIR > /dev/null + tar --append -f $BACKUP_TMP_FILE .dokku_backup_apps + popd > /dev/null + + # if no file specified, output to stdout + if [[ -z $OUTPUT_FILE ]]; then + cat $BACKUP_TMP_FILE + else + mv $BACKUP_TMP_FILE $OUTPUT_FILE + fi + + rm -rf $BACKUP_TMP_DIR + ;; + + backup:import) + INPUT_FILE="$2" + [[ -z $INPUT_FILE ]] && INPUT_FILE="-" + + BACKUP_TMP_DIR=$(mktemp -d) + + tar xf $INPUT_FILE --directory=$BACKUP_TMP_DIR + + if [[ ! -f $BACKUP_TMP_DIR/.dokku_backup_version ]]; then + echo "Unable to determine backup version" + exit 1 + fi + + VERSION=$(< $BACKUP_TMP_DIR/.dokku_backup_version) + if [[ $VERSION -ne 1 ]]; then + echo "Unknown format version $VERSION" + exit 1 + fi + + BACKUP_ROOT="$BACKUP_TMP_DIR/home/git" + TARGET_DIR="/home/git" + + # create all the app repositories + while read app; do git init --bare "$TARGET_DIR/$app"; done < $BACKUP_TMP_DIR/.dokku_backup_apps > /dev/null + + # have the plugins import their stuff + pluginhook backup-import $VERSION "$BACKUP_ROOT" $TARGET_DIR + + rm -rf $BACKUP_TMP_DIR + ;; + + help) + cat && cat< Date: Mon, 28 Oct 2013 08:11:41 +0100 Subject: [PATCH 2/3] Specify current backup version at the top of the script --- plugins/backup/commands | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/plugins/backup/commands b/plugins/backup/commands index f0cbeb6ed..312e5cea6 100755 --- a/plugins/backup/commands +++ b/plugins/backup/commands @@ -1,27 +1,23 @@ #!/bin/bash +CURRENT_BACKUP_VERSION=1 set -e; case "$1" in backup:export) OUTPUT_FILE="$2" BACKUP_DIR=/home/git - if [[ ! -f $BACKUP_DIR/.dokku_backup_version ]]; then - echo "Unable to determine backup version" - exit 1 - fi - BACKUP_TMP_DIR=$(mktemp -d) BACKUP_TMP_FILE="$BACKUP_TMP_DIR/backup.tar" pluginhook backup-export 1 $BACKUP_DIR | tar -cf $BACKUP_TMP_FILE --files-from - - # we want to insert the version file in the root of the file pushd $BACKUP_DIR > /dev/null - tar --append -f $BACKUP_TMP_FILE .dokku_backup_version ls -d */ > "$BACKUP_TMP_DIR/.dokku_backup_apps" popd > /dev/null - # we want to insert the apps file in the root of the file + # we want to insert the files in the root of the tar pushd $BACKUP_TMP_DIR > /dev/null + echo $CURRENT_BACKUP_VERSION > .dokku_backup_version + tar --append -f $BACKUP_TMP_FILE .dokku_backup_version tar --append -f $BACKUP_TMP_FILE .dokku_backup_apps popd > /dev/null From 2219044114c9b8ca0a046a90c3dc52f5e3f20559 Mon Sep 17 00:00:00 2001 From: Alexander Date: Thu, 14 Nov 2013 23:51:35 +0100 Subject: [PATCH 3/3] Introduce DOKKU_ROOT and import now in git plugin --- plugins/00_dokku-standard/backup-export | 1 + plugins/00_dokku-standard/backup-import | 1 + plugins/backup/commands | 14 +++++++------- plugins/config/backup-import | 2 +- plugins/git/backup-import | 17 +++++++++++++++++ 5 files changed, 27 insertions(+), 8 deletions(-) create mode 100755 plugins/git/backup-import diff --git a/plugins/00_dokku-standard/backup-export b/plugins/00_dokku-standard/backup-export index 58d4ac43c..102ef082c 100755 --- a/plugins/00_dokku-standard/backup-export +++ b/plugins/00_dokku-standard/backup-export @@ -3,6 +3,7 @@ VERSION="$1" BASE_DIR="$2" cat; +[[ -f $BASE_DIR/.sshcommand ]] && echo "$BASE_DIR/.sshcommand" [[ -f $BASE_DIR/.ssh/authorized_keys ]] && echo "$BASE_DIR/.ssh/authorized_keys" [[ -f $BASE_DIR/HOSTNAME ]] && echo "$BASE_DIR/HOSTNAME" [[ -f $BASE_DIR/VHOST ]] && echo "$BASE_DIR/VHOST" diff --git a/plugins/00_dokku-standard/backup-import b/plugins/00_dokku-standard/backup-import index 2fa31c13f..a3f691d9d 100755 --- a/plugins/00_dokku-standard/backup-import +++ b/plugins/00_dokku-standard/backup-import @@ -3,6 +3,7 @@ VERSION="$1" IMPORT_DIR="$2" TARGET_DIR="$3" +[[ -f $IMPORT_DIR/.sshcommand ]] && mv $IMPORT_DIR/.sshcommand $TARGET_DIR/.sshcommand if [[ -f $IMPORT_DIR/.ssh/authorized_keys ]]; then mkdir $TARGET_DIR/.ssh mv $IMPORT_DIR/.ssh/authorized_keys $TARGET_DIR/.ssh/authorized_keys diff --git a/plugins/backup/commands b/plugins/backup/commands index 312e5cea6..635b44a8e 100755 --- a/plugins/backup/commands +++ b/plugins/backup/commands @@ -3,7 +3,7 @@ CURRENT_BACKUP_VERSION=1 set -e; case "$1" in backup:export) OUTPUT_FILE="$2" - BACKUP_DIR=/home/git + BACKUP_DIR="$DOKKU_ROOT" BACKUP_TMP_DIR=$(mktemp -d) BACKUP_TMP_FILE="$BACKUP_TMP_DIR/backup.tar" @@ -11,7 +11,7 @@ set -e; case "$1" in pluginhook backup-export 1 $BACKUP_DIR | tar -cf $BACKUP_TMP_FILE --files-from - pushd $BACKUP_DIR > /dev/null - ls -d */ > "$BACKUP_TMP_DIR/.dokku_backup_apps" + ls -d */ | grep -oE '[^/]+' > "$BACKUP_TMP_DIR/.dokku_backup_apps" popd > /dev/null # we want to insert the files in the root of the tar @@ -50,14 +50,14 @@ set -e; case "$1" in exit 1 fi - BACKUP_ROOT="$BACKUP_TMP_DIR/home/git" - TARGET_DIR="/home/git" + BACKUP_ROOT="$BACKUP_TMP_DIR""$DOKKU_ROOT" + TARGET_DIR="$DOKKU_ROOT" - # create all the app repositories - while read app; do git init --bare "$TARGET_DIR/$app"; done < $BACKUP_TMP_DIR/.dokku_backup_apps > /dev/null + # create all the app directories + while read app; do mkdir "$TARGET_DIR/$app"; done < $BACKUP_TMP_DIR/.dokku_backup_apps > /dev/null # have the plugins import their stuff - pluginhook backup-import $VERSION "$BACKUP_ROOT" $TARGET_DIR + pluginhook backup-import $VERSION "$BACKUP_ROOT" $TARGET_DIR "$BACKUP_TMP_DIR/.dokku_backup_apps" rm -rf $BACKUP_TMP_DIR ;; diff --git a/plugins/config/backup-import b/plugins/config/backup-import index c3b20e58a..7face8b95 100755 --- a/plugins/config/backup-import +++ b/plugins/config/backup-import @@ -5,5 +5,5 @@ TARGET_DIR="$3" cd $IMPORT_DIR for file in `find . -regex ".*/ENV"`; do - cp $file $TARGET_DIR$file + cp $file "$TARGET_DIR/$file" done diff --git a/plugins/git/backup-import b/plugins/git/backup-import new file mode 100755 index 000000000..b7b0505b6 --- /dev/null +++ b/plugins/git/backup-import @@ -0,0 +1,17 @@ +#!/bin/bash +VERSION="$1" +IMPORT_DIR="$2" +TARGET_DIR="$3" +APPS="$4" + +while read APP; do + APP_PATH="$TARGET_DIR/$APP"; + git init --bare $APP_PATH > /dev/null + PRERECEIVE_HOOK="$APP_PATH/hooks/pre-receive" + cat > $PRERECEIVE_HOOK < /dev/null