-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
entrypoint.sh
executable file
·79 lines (74 loc) · 2.02 KB
/
entrypoint.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#!/bin/sh
set -eu
if [ $# -eq 0 ]; then
echo "Please run with outliner [export|import|sync] arguments"
exit
fi
setup_git() {
if [ -f "$HOME/.ssh/id_rsa" ]; then
# This is required because Kubernetes secret mounts can't
# have file permissions set
chmod 0400 "$HOME/.ssh/id_rsa"
if [ ! -d "$HOME/.ssh/id_rsa.pub" ]; then
ssh-keygen -y -f "$HOME/.ssh/id_rsa" > "$HOME/.ssh/id_rsa.pub"
fi
echo "[+] Using SSH key for git pushes"
else
echo "[E] Git credentials not available, quitting"
exit 1
fi
eval $(ssh-agent)
ssh-add "$HOME/.ssh/id_rsa"
}
update_git_config() {
EMAIL=${GIT_EMAIL:-outliner@example.invalid}
git config --global user.email "$EMAIL"
git config --global user.name "Outliner Backup"
git remote add origin "$GIT_REMOTE_URL"
}
case $1 in
export)
shift
bundle exec outliner-export "$@"
;;
import)
shift
bundle exec outliner-import "$@"
;;
sync)
BRANCH=${GIT_BRANCH:-master}
old_git_dir=$(mktemp -d)
fresh_export_dir=$(mktemp -d)
if [ -z "$GIT_REMOTE_URL" ]; then
echo "[E] GIT_REMOTE_URL not set"
exit 1
else
git clone --branch "$BRANCH" "$GIT_REMOTE_URL" "$old_git_dir"
setup_git
update_git_config
echo "[+] Exporting data from Outline"
bundle exec outliner-export "$fresh_export_dir"
echo "[+] Resetting git repository"
cd "$old_git_dir"
# We update so that git forgets all files
git ls-files -z |xargs -n1 -0 git rm
# Then we copy across the files from the new export
cd "$fresh_export_dir"
echo "[+] Updating git repository"
rsync -av . "$old_git_dir"
cd "$old_git_dir"
echo "[+] Committing to git"
git add .
git commit --message "Backup: $(date)" > /dev/null
git status
echo "[+] Pushing to git remote"
git push origin "HEAD:$BRANCH"
echo "[+] Cleaning up"
rm -rf "$old_git_dir"
rm -rf "$fresh_export_dir"
fi
;;
*)
echo "Invalid command, please check README"
;;
esac