-
Notifications
You must be signed in to change notification settings - Fork 6
/
duplicity_backup.sh
executable file
·240 lines (210 loc) · 6.57 KB
/
duplicity_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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
#!/bin/bash
# vim: ai ts=4 sts=4 et sw=4 fileencoding=utf-8
# maintainer:ukanga
MYSQL_USER=
MYSQL_PASS=
MYSQL_DB=
MYSQL_BACKUP=
MYSQL_BACKUP_SOURCE=
MYSQL_BACKUP_TARGET=
COUCHDB_USER=
COUCHDB_PASS=
COUCHDB_DB=
COUCHDB_BACKUP=
COUCHDB_BACKUP_SOURCE=
COUCHDB_BACKUP_TARGET=
MONGO_DUMP=
MONGO_BACKUP_TARGET=
RESTORE_MONGO_BACKUP=
RESTORE_MYSQL_BACKUP=
POSTGRES_USER=
POSTGRES_DB=
POSTGRES_HOST=
POSTGRES_BACKUP_FOLDER=
POSTGRES_BACKUP_TARGET=
ENCRYPT_KEY=
# GNU Passphrase
PASSPHRASE=
AWS_ACCESS_KEY_ID=
AWS_SECRET_ACCESS_KEY=
DUP_LOG=/tmp/duplicity.log
if [ -r ~/.backup_config ]; then
source ~/.backup_config
fi
export AWS_ACCESS_KEY_ID=$AWS_ACCESS_KEY_ID
export AWS_SECRET_ACCESS_KEY=$AWS_SECRET_ACCESS_KEY
export PASSPHRASE=$PASSPHRASE
POSTGRES_BACKUP_FILE="${POSTGRES_BACKUP_FOLDER}/${POSTGRES_DB}.sql"
postgres_backup()
{
if [[ -z "$POSTGRES_DB" || -z "$POSTGRES_USER" || -z "$POSTGRES_BACKUP_FOLDER" ]]; then
echo "POSTGRES_DB, POSTGRES_USER and POSTGRES_BACKUP_FOLDER variables need to be set in ~/.backup_config"
exit
fi
if [ ! -z "$POSTGRES_HOST" ]; then
pg_dump -U $POSTGRES_USER -h $POSTGRES_HOST $POSTGRES_DB -f $POSTGRES_BACKUP_FILE -c
else
pg_dump -U $POSTGRES_USER $POSTGRES_DB -f $POSTGRES_BACKUP_FILE -c
fi
}
cleanup_postgres()
{
rm $POSTGRES_BACKUP_FILE
}
postgres_duplicity_backup()
{
if [[ -z "$POSTGRES_BACKUP_FOLDER" || -z "$POSTGRES_BACKUP_TARGET" || -z "$ENCRYPT_KEY" ]]; then
echo "ENCRYPT_KEY, POSTGRES_BACKUP_FOLDER and POSTGRES_BACKUP_TARGET needs to be set in ~/.backup_config"
exit
fi
duplicity --s3-use-new-style --full-if-older-than 7D --log-file $DUP_LOG --encrypt-key $ENCRYPT_KEY $POSTGRES_BACKUP_FOLDER $POSTGRES_BACKUP_TARGET
}
restore_postgres()
{
if [[ -z "$POSTGRES_BACKUP_FOLDER" || -z "$POSTGRES_BACKUP_TARGET" || -z "$ENCRYPT_KEY" ]]; then
echo "ENCRYPT_KEY, POSTGRES_BACKUP_FOLDER and POSTGRES_BACKUP_TARGET needs to be set in ~/.backup_config"
exit
fi
if [ -z "$RESTORE_POSTGRES_BACKUP" ]; then
RESTORE_POSTGRES_BACKUP=$POSTGRES_BACKUP_FOLDER
fi
duplicity restore --s3-use-new-style --hidden-encrypt-key $ENCRYPT_KEY $POSTGRES_BACKUP_TARGET $RESTORE_POSTGRES_BACKUP
}
mysql_backup()
{
mysqldump -u $MYSQL_USER -p$MYSQL_PASS $MYSQL_DB > $MYSQL_BACKUP
}
cleanup_mysql()
{
rm $MYSQL_BACKUP
}
mysql_duplicity_backup()
{
duplicity --s3-use-new-style --full-if-older-than 7D --log-file $DUP_LOG --encrypt-key $ENCRYPT_KEY $MYSQL_BACKUP_SOURCE $MYSQL_BACKUP_TARGET
}
restore_mysql()
{
duplicity restore --s3-use-new-style --hidden-encrypt-key $ENCRYPT_KEY $MYSQL_BACKUP_TARGET $RESTORE_MYSQL_BACKUP
}
couchdb_backup()
{
if [[ -z "$COUCHDB_USER" || -z "$COUCHDB_PASS" || -z "$COUCHDB_DB" || -z "$COUCHDB_BACKUP_SOURCE" || -z "$COUCHDB_BACKUP" ]]; then
echo "COUCHDB_USER, COUCHDB_PASS, COUCHDB_DB, COUCHDB_BACKUP_SOURCE, and COUCHDB_BACKUP variables need to be set in ~/.backup_config"
exit
fi
mkdir -p $COUCHDB_BACKUP_SOURCE
cdbdump -u $COUCHDB_USER -p $COUCHDB_PASS -d $COUCHDB_DB | gzip > $COUCHDB_BACKUP_SOURCE/$COUCHDB_BACKUP
}
cleanup_couchdb()
{
if [[ -z "$COUCHDB_BACKUP_SOURCE" || -z "$COUCHDB_BACKUP" ]]; then
echo "COUCHDB_BACKUP_SOURCE, and COUCHDB_BACKUP variables need to be set in ~/.backup_config"
exit
fi
rm $COUCHDB_BACKUP_SOURCE/$COUCHDB_BACKUP
}
couchdb_duplicity_backup()
{
if [[ -z "$COUCHDB_BACKUP_SOURCE" || -z "$COUCHDB_BACKUP_TARGET" ]]; then
echo "COUCHDB_BACKUP_SOURCE, and COUCHDB_BACKUP_TARGET variables need to be set in ~/.backup_config"
exit
fi
duplicity --s3-use-new-style --full-if-older-than 7D --log-file $DUP_LOG --encrypt-key $ENCRYPT_KEY $COUCHDB_BACKUP_SOURCE $COUCHDB_BACKUP_TARGET
}
restore_couchdb()
{
if [[ -z "$COUCHDB_BACKUP_SOURCE" ]]; then
echo "COUCHDB_BACKUP_SOURCE variable needs to be set in ~/.backup_config"
exit
fi
if [ -z "$RESTORE_COUCHDB_BACKUP" ]; then
RESTORE_COUCHDB_BACKUP=$COUCHDB_BACKUP_SOURCE
fi
mkdir -p $RESTORE_COUCHDB_BACKUP
duplicity restore --s3-use-new-style --hidden-encrypt-key $ENCRYPT_KEY $COUCHDB_BACKUP_TARGET $RESTORE_COUCHDB_BACKUP
}
mongo_backup()
{
mongodump --out $MONGO_DUMP
}
cleanup_mongo()
{
rm -rf $MONGO_DUMP
}
mongo_duplicity_backup()
{
duplicity --s3-use-new-style --full-if-older-than 7D --log-file $DUP_LOG --encrypt-key $ENCRYPT_KEY $MONGO_DUMP $MONGO_BACKUP_TARGET
}
restore_mongo()
{
duplicity restore --s3-use-new-style --hidden-encrypt-key $ENCRYPT_KEY $MONGO_BACKUP_TARGET $RESTORE_MONGO_BACKUP
}
folder_backup()
{
duplicity --s3-use-new-style --log-file $DUP_LOG --encrypt-key $ENCRYPT_KEY $FOLDER_TO_BACKUP $FOLDER_BACKUP_TARGET
}
restore_folder()
{
duplicity restore --s3-use-new-style --hidden-encrypt-key $ENCRYPT_KEY $FOLDER_TO_RESTORE $FOLDER_RESTORE_TARGET
}
if [ "$1" = "mysql" ]; then
mysql_backup
mysql_duplicity_backup
cleanup_mysql
elif [ "$1" = "postgres" ]; then
postgres_backup
postgres_duplicity_backup
cleanup_postgres
elif [ "$1" = "mongo" ]; then
mongo_backup
mongo_duplicity_backup
cleanup_mongo
elif [ "$1" = "couchdb" ]; then
couchdb_backup
couchdb_duplicity_backup
cleanup_couchdb
elif [ "$1" = "folder" ]; then
if [ $# -gt 2 ]; then
FOLDER_TO_BACKUP=$2
FOLDER_BACKUP_TARGET=$3
folder_backup
else
echo "Expecting:"
echo "./duplicity_backup folder /path/to/backup file:///path/of/where/to/backup"
echo "or"
echo "./duplicity_backup folder /path/to/backup s3:///path/of/where/to/backup"
fi
elif [[ "$1" = "restore" && "$2" = "mysql" ]]; then
if [ $# -gt 2 ]; then
RESTORE_MYSQL_BACKUP=$3
fi
restore_mysql
elif [[ "$1" = "restore" && "$2" = "postgres" ]]; then
if [ $# -gt 2 ]; then
RESTORE_POSTGRES_BACKUP=$3
fi
restore_postgres
elif [[ "$1" = "restore" && "$2" = "mongo" ]]; then
if [ $# -gt 2 ]; then
RESTORE_MONGO_BACKUP=$3
fi
restore_mongo
elif [[ "$1" = "restore" && "$2" = "couchdb" ]]; then
if [ $# -gt 2 ]; then
RESTORE_COUCHDB_BACKUP=$3
fi
restore_couchdb
elif [[ "$1" = "restore" && "$2" = "folder" ]]; then
if [ $# -gt 3 ]; then
FOLDER_TO_RESTORE=$3
FOLDER_RESTORE_TARGET=$4
restore_folder
else
echo "./duplicity_backup restore folder file:///path/of/backup /path/of/where/to/restore"
fi
else
echo "duplicity_backup - Encrypted incremental backup to local or remote storage, mongo or mysql or postgres"
fi
unset AWS_ACCESS_KEY_ID
unset AWS_SECRET_ACCESS_KEY
unset PASSPHRASE