2019-07-24 15:50:26 +00:00
|
|
|
#!/bin/sh
|
2019-08-13 12:31:11 +00:00
|
|
|
set -eu
|
2019-07-24 15:50:26 +00:00
|
|
|
if [ $# -eq 0 ]; then
|
2019-08-14 09:39:11 +00:00
|
|
|
echo "Please run with outliner [export|import|sync] arguments"
|
2019-07-24 15:50:26 +00:00
|
|
|
exit
|
|
|
|
fi
|
|
|
|
|
2019-08-12 13:57:32 +00:00
|
|
|
setup_git() {
|
2019-08-13 04:59:25 +00:00
|
|
|
if [ -f "$HOME/.ssh/id_rsa" ]; then
|
2019-08-14 09:39:11 +00:00
|
|
|
# This is required because Kubernetes secret mounts can't
|
|
|
|
# have file permissions set
|
2019-08-13 12:31:11 +00:00
|
|
|
chmod 0400 "$HOME/.ssh/id_rsa"
|
2019-08-12 13:57:32 +00:00
|
|
|
|
2019-08-13 04:59:25 +00:00
|
|
|
if [ ! -d "$HOME/.ssh/id_rsa.pub" ]; then
|
|
|
|
ssh-keygen -y -f "$HOME/.ssh/id_rsa" > "$HOME/.ssh/id_rsa.pub"
|
2019-08-12 13:57:32 +00:00
|
|
|
fi
|
|
|
|
echo "[+] Using SSH key for git pushes"
|
|
|
|
else
|
|
|
|
echo "[E] Git credentials not available, quitting"
|
2019-08-13 12:31:11 +00:00
|
|
|
exit 1
|
2019-08-12 13:57:32 +00:00
|
|
|
fi
|
2019-08-13 12:31:11 +00:00
|
|
|
|
|
|
|
eval $(ssh-agent)
|
|
|
|
ssh-add "$HOME/.ssh/id_rsa"
|
2019-08-12 13:57:32 +00:00
|
|
|
}
|
|
|
|
|
2019-08-13 12:31:11 +00:00
|
|
|
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"
|
2019-08-12 13:57:32 +00:00
|
|
|
}
|
|
|
|
|
2019-07-24 15:50:26 +00:00
|
|
|
case $1 in
|
|
|
|
export)
|
|
|
|
shift
|
2019-08-12 13:57:32 +00:00
|
|
|
bundle exec outliner-export "$@"
|
2019-07-24 15:50:26 +00:00
|
|
|
;;
|
|
|
|
import)
|
|
|
|
shift
|
2019-08-12 13:57:32 +00:00
|
|
|
bundle exec outliner-import "$@"
|
2019-07-24 15:50:26 +00:00
|
|
|
;;
|
2019-08-13 12:31:11 +00:00
|
|
|
sync)
|
2020-04-26 00:44:36 +00:00
|
|
|
BRANCH=${GIT_BRANCH:-master}
|
|
|
|
old_git_dir=$(mktemp -d)
|
|
|
|
fresh_export_dir=$(mktemp -d)
|
2019-08-13 12:31:11 +00:00
|
|
|
if [ -z "$GIT_REMOTE_URL" ]; then
|
|
|
|
echo "[E] GIT_REMOTE_URL not set"
|
|
|
|
exit 1
|
|
|
|
else
|
2020-04-26 00:44:36 +00:00
|
|
|
git clone --branch "$BRANCH" "$GIT_REMOTE_URL" "$old_git_dir"
|
2019-08-13 12:31:11 +00:00
|
|
|
setup_git
|
|
|
|
update_git_config
|
2020-04-26 00:44:36 +00:00
|
|
|
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"
|
2019-08-13 12:31:11 +00:00
|
|
|
git add .
|
2019-08-14 09:39:11 +00:00
|
|
|
git commit --message "Backup: $(date)" > /dev/null
|
|
|
|
git status
|
2020-04-26 00:44:36 +00:00
|
|
|
echo "[+] Pushing to git remote"
|
|
|
|
git push origin "HEAD:$BRANCH"
|
|
|
|
echo "[+] Cleaning up"
|
|
|
|
rm -rf "$old_git_dir"
|
|
|
|
rm -rf "$fresh_export_dir"
|
2019-08-13 12:31:11 +00:00
|
|
|
fi
|
|
|
|
;;
|
2019-07-24 15:50:26 +00:00
|
|
|
*)
|
|
|
|
echo "Invalid command, please check README"
|
|
|
|
;;
|
|
|
|
esac
|