-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbackup.sh
137 lines (118 loc) · 3.84 KB
/
backup.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#!/bin/bash
#####################################
## CONFIG
#####################################
# The type of encryption to use, options:
# - unencrypted
# - symmetric (keyfile)
# - asymmetric (public/private)
ENCRYPTION_MODE="symmetric"
# The key file path, defaults to a supplied parameter
ENCRYPTION_KEY="$2"
# A temporary directory to store the backup
BACKUP_TMP="/tmp/"
# Modes:
# - attach: Attach the encrypted backup as a file
BACKUP_MODE="attach"
#####################################
## PROCESS
#####################################
# No configuration below here
NL=$'\n'
NOW=$(date +"%Y-%m-%d_%H-%M-%S")
# If the file .backupconfig.sh exists then we will include it
# You can override the configuration here!
BACKUP_CONFIG=$(dirname "$0")/.backupconfig.sh
if [[ -f "$BACKUP_CONFIG" ]]; then
source "$BACKUP_CONFIG"
fi
function do_encrypt {
if [[ "$ENCRYPTION_MODE" == "symmetric" ]]; then
cat - | openssl enc -aes-256-cbc -kfile "$1" -z
elif [[ "$ENCRYPTION_MODE" == "asymmetric" ]]; then
cat - | gzip | openssl smime -encrypt -aes256 -binary -outform DEM "$1"
elif [[ "$ENCRYPTION_MODE" == "unencrypted" ]]; then
cat - | gzip
fi
}
function trim() {
local var="$*"
var="${var#"${var%%[![:space:]]*}"}" # remove leading whitespace characters
var="${var%"${var##*[![:space:]]}"}" # remove trailing whitespace characters
echo -n "$var"
}
function do_backup {
BACKUP_FILE="$BACKUP_TMP$1-$NOW.gz"
if [[ "$ENCRYPTION_MODE" != "unencrypted" ]]; then
BACKUP_FILE="$BACKUP_FILE.enc"
fi
BACKUP_EMAIL="$2"
BACKUP_SUBJECT="$3 - $NOW"
if [[ -f "$BACKUP_FILE" ]]; then
rm "$BACKUP_FILE"
fi
do_encrypt "$4" > "$BACKUP_FILE"
STATUS=$?
if [[ $STATUS != "0" ]]; then
echo "Backup Failed"
echo "Backup Failed" | mutt -s "[FAIL] $BACKUP_SUBJECT" -- "$BACKUP_EMAIL"
elif [[ "$1" == "output" ]] ; then
echo "Backup File: $BACKUP_FILE"
elif [[ "$5" == "attach" ]]; then
echo "Backup Complete: $NOW" | mutt -a "$BACKUP_FILE" -s "[OK] $BACKUP_SUBJECT" -- "$BACKUP_EMAIL"
else
FUNC="upload_$5"
if [[ -f $(dirname "$0")"/upload/$FUNC.sh" ]]; then
source $(dirname "$0")"/upload/$FUNC.sh"
fi
BACKUP_LINK=$(trim $(eval ${FUNC} "$BACKUP_FILE"))
if [[ "z$BACKUP_LINK" == "z" ]]; then
echo "Backup Failed"
echo "Backup Upload Failed: ${NOW}" | mutt -s "[FAIL] $BACKUP_SUBJECT" -- "$BACKUP_EMAIL"
else
BACKUP_SIZE=$(du -hs "$BACKUP_FILE" | awk '{print $1}')
echo "Backup Complete"
echo "Backup Complete: ${NOW}${NL}Backup Link:${BACKUP_LINK} (${BACKUP_SIZE})" | mutt -s "[OK] $BACKUP_SUBJECT" -- "$BACKUP_EMAIL"
fi
fi
}
function do_decrypt {
if [[ "$ENCRYPTION_MODE" == "symmetric" ]]; then
cat - | openssl enc -aes-256-cbc -d -kfile "$1" -z
elif [[ "$ENCRYPTION_MODE" == "asymmetric" ]]; then
cat - | openssl smime -decrypt -binary -inform DEM -inkey "$1" | gzip -d
elif [[ "$ENCRYPTION_MODE" == "unencrypted" ]]; then
cat - | gzip -d
fi
}
function create_keypair {
openssl req -x509 -nodes -newkey rsa:2048 -keyout "$1" -out "$2"
}
case $1 in
"backup")
echo "Starting Backup"
do_backup "$3" "$4" "$5" "$ENCRYPTION_KEY" "$BACKUP_MODE"
;;
"decrypt")
do_decrypt "$ENCRYPTION_KEY"
;;
"keypair")
create_keypair "backup.key" "backup.pem"
echo "Keypair created as backup.key and backup.pem"
;;
*)
echo "Encrypted Backup Script"
echo ""
echo "Usage:"
echo "./backup.sh backup [KEY] [FILENAME] [EMAIL] [SUBJECT]"
echo "Description: Take the backup supplied via stdin, compress, encrypt and email to [email protected]"
echo "Example: take_backup | ./backup.sh backup ~/backup.key backup.sql [email protected] \"Database Backup\""
echo ""
echo "cat file.sql.gz.enc | ./backup.sh decrypt [KEY] > file.sql"
echo "Description: Decrypt the file file.sql.gz.enc to file.sql"
echo ""
echo "./backup.sh keypair"
echo "Description: Create a keypair suitable for use with the asymmetric encryption method"
echo ""
;;
esac