-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathdropbox_backup
executable file
·89 lines (78 loc) · 2.07 KB
/
dropbox_backup
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
80
81
82
83
84
85
86
87
88
89
#!/usr/bin/env bash
set -eo pipefail
DROPBOX_DIR="$HOME/Dropbox"
if ! [ -d "$DROPBOX_DIR" ]; then
echo "Could not find Dropbox folder at $DROPBOX_DIR. Please set up Dropbox first."
exit 1
fi
if ! which openssl > /dev/null 2>&1; then
echo "openssl is required! Please install packages first."
exit 1
fi
function usage() {
\cat <<HELP >&2
Usage: $0
-d <directory> Run for a specific directory (ssh, convox, etc.)
-s Encrypt and save ~/.{ssh,convox} to Dropbox
-r Decrypt and restore ~/.{ssh,convox} from Dropbox
HELP
}
if [ -z "$1" ]; then
usage
exit 1
fi
DROPBOX_FOLDER="$DROPBOX_DIR/Config"
function save_files() {
mkdir -p "$DROPBOX_FOLDER"
CONFIG_DIR="$1"
CONFIG_PATH="$HOME/.$CONFIG_DIR"
if ! [ -d "$CONFIG_PATH" ]; then return; fi
ENCRYPTED_FILES_PATH="$DROPBOX_FOLDER/$CONFIG_DIR.tar.gz.encrypted"
echo "=> Encrypting $CONFIG_PATH and saving to ${ENCRYPTED_FILES_PATH}..."
cd "$CONFIG_PATH"
tar cz * | openssl enc -aes-256-cbc -md md5 -e > "${ENCRYPTED_FILES_PATH}"
}
function restore_files() {
CONFIG_DIR="$1"
ENCRYPTED_FILES_PATH="$DROPBOX_FOLDER/$CONFIG_DIR.tar.gz.encrypted"
if ! [ -f "$ENCRYPTED_FILES_PATH" ]; then
echo "Could not find encrypted files at ${ENCRYPTED_FILES_PATH}"
return
fi
echo "=> Decrypting and extracting ~/.$CONFIG_DIR from ${ENCRYPTED_FILES_PATH}..."
openssl aes-256-cbc -d -md md5 -in "${ENCRYPTED_FILES_PATH}" -out /tmp/"$CONFIG_DIR".tar.gz
rm -rf "$HOME/.$CONFIG_DIR"
mkdir -p "$HOME/.$CONFIG_DIR"
cd "$HOME/.$CONFIG_DIR"
tar xvf /tmp/"$CONFIG_DIR".tar.gz
rm -rf /tmp/"$CONFIG_DIR".tar.gz
}
function set_default_dirs() {
if [ -z "$CONFIG_DIRS" ]; then
#CONFIG_DIRS="ssh convox"
CONFIG_DIRS="ssh convox"
fi
}
while getopts "d:srh" opt; do
case "$opt" in
d)
CONFIG_DIRS="$OPTARG";;
s)
set_default_dirs
for DIR in $CONFIG_DIRS; do
save_files "$DIR"
done;;
r)
set_default_dirs
for DIR in $CONFIG_DIRS; do
restore_files "$DIR"
done;;
h)
usage
exit;;
*)
echo "Unknown option: -$opt" >&2
return 1;;
esac
done
shift $((OPTIND -1))