diff options
Diffstat (limited to 'run.sh')
-rwxr-xr-x | run.sh | 51 |
1 files changed, 51 insertions, 0 deletions
@@ -0,0 +1,51 @@ | |||
1 | #!/bin/bash | ||
2 | |||
3 | export PYTHONPATH=$(cd $(dirname "${BASH_SOURCE[0]}") && pwd) | ||
4 | |||
5 | source $(dirname "$PYTHONPATH")/venv/bin/activate | ||
6 | |||
7 | WORKERS=2 | ||
8 | BACKUP_KEY="$HOME/.private/backupkey.pem" | ||
9 | BIND_PATH="127.0.0.1:9000" | ||
10 | [[ -z "$2" ]] || BIND_PATH="$2" | ||
11 | |||
12 | MAX_KEEP=`python -c "import wikiconfig; print wikiconfig.Config.backup_set_keep" 2>/dev/null` | ||
13 | S3_BUCKET=`python -c "import wikiconfig; print wikiconfig.Config.backup_s3_bucket" 2>/dev/null` | ||
14 | |||
15 | APPLICATION="MoinMoin.wsgiapp:application" | ||
16 | [[ -z "$DEBUG" ]] || APPLICATION="wsgi:application" | ||
17 | |||
18 | if [[ "$1" == "serve" ]]; then | ||
19 | gunicorn --timeout=300 --preload -w $WORKERS -b $BIND_PATH $APPLICATION | ||
20 | elif [[ "$1" == "cron" ]]; then | ||
21 | moin maint makecache | ||
22 | elif [[ "$1" == "reindex" ]]; then | ||
23 | moin index build --mode=rebuild | ||
24 | elif [[ "$1" == "backup" ]]; then | ||
25 | if [[ -z "$S3_BUCKET" || -z "$MAX_KEEP" || ! -r $BACKUP_KEY ]]; then | ||
26 | echo "Not properly configured" | ||
27 | exit 1 | ||
28 | fi | ||
29 | |||
30 | FILENAME="$HOME/backup-`date +%Y%m%d%H%M%S`.bak" | ||
31 | |||
32 | # Backup first | ||
33 | # | ||
34 | # Place CACHEDIR.TAG files in cache directories to prevent backup. | ||
35 | # This is needed because thumbnail images in caches can cause lots of | ||
36 | # wasted backup space. | ||
37 | find "$HOME/wiki-data/" -type d -name 'cache' -exec bash -c 'echo "Signature: 8a477f597d28d172789f06886806bc55" > "{}/CACHEDIR.TAG"' \; | ||
38 | |||
39 | ( cd $HOME && tar -cj --exclude-caches wiki-data/ | openssl aes-256-cbc -pass "file:$BACKUP_KEY" > $FILENAME ) | ||
40 | s3cmd put --no-progress $FILENAME $S3_BUCKET | ||
41 | rm $FILENAME | ||
42 | |||
43 | # Then prune the backup set to a maximum of N files | ||
44 | ALL_FILES=( $(s3cmd ls $S3_BUCKET | awk '{ print $4 }' | sort -nr) ) | ||
45 | |||
46 | for (( i=$MAX_KEEP ; i < ${#ALL_FILES[@]}; i++ )); do | ||
47 | s3cmd del ${ALL_FILES[i]} | ||
48 | done | ||
49 | else | ||
50 | moin $* | ||
51 | fi | ||