Merge pull request #265 from asm89/backup

Introduce `backup` plugin to export/import settings
This commit is contained in:
rhy-jot
2013-11-19 21:07:20 -08:00
8 changed files with 143 additions and 0 deletions

View File

@@ -0,0 +1,9 @@
#!/bin/bash
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"

View File

@@ -0,0 +1,14 @@
#!/bin/bash
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
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

73
plugins/backup/commands Executable file
View File

@@ -0,0 +1,73 @@
#!/bin/bash
CURRENT_BACKUP_VERSION=1
set -e; case "$1" in
backup:export)
OUTPUT_FILE="$2"
BACKUP_DIR="$DOKKU_ROOT"
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 -
pushd $BACKUP_DIR > /dev/null
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
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
# 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""$DOKKU_ROOT"
TARGET_DIR="$DOKKU_ROOT"
# 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 "$BACKUP_TMP_DIR/.dokku_backup_apps"
rm -rf $BACKUP_TMP_DIR
;;
help)
cat && cat<<EOF
backup:export [file] Export dokku configuration files
backup:import [file] Import dokku configuration files
EOF
;;
esac
cat

5
plugins/config/backup-export Executable file
View File

@@ -0,0 +1,5 @@
#!/bin/bash
VERSION="$1"
BASE_DIR="$2"
cat; find $BASE_DIR -regex ".*/ENV"

9
plugins/config/backup-import Executable file
View File

@@ -0,0 +1,9 @@
#!/bin/bash
VERSION="$1"
IMPORT_DIR="$2"
TARGET_DIR="$3"
cd $IMPORT_DIR
for file in `find . -regex ".*/ENV"`; do
cp $file "$TARGET_DIR/$file"
done

17
plugins/git/backup-import Executable file
View File

@@ -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 <<EOF
#!/usr/bin/env bash
set -e; set -o pipefail;
cat | DOKKU_ROOT="$DOKKU_ROOT" dokku git-hook $APP
EOF
chmod +x $PRERECEIVE_HOOK
done < $APPS > /dev/null

View File

@@ -0,0 +1,5 @@
#!/bin/bash
VERSION="$1"
BASE_DIR="$2"
cat; find $BASE_DIR -regex ".*ssl/server\(.crt\|.key\)";

View File

@@ -0,0 +1,11 @@
#!/bin/bash
VERSION="$1"
IMPORT_DIR="$2"
TARGET_DIR="$3"
cd $IMPORT_DIR
for file in `find . -regex ".*ssl/server\(.crt\|.key\)"`; do
mkdir -p $(dirname $TARGET_DIR$file)
cp $file $TARGET_DIR$file
done