aboutsummaryrefslogtreecommitdiffstats
path: root/bin/crypted-backups
blob: e2dc887d81e47626b321b98b9e4488c0a0fabc3e (plain)
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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
#!/usr/bin/env bash
set -euo pipefail
IFS=$'\n\t'

. /etc/crypted-backups
user_mode=""
source_mode=""
verbose=''

function notification_source_to_destination () {
  local source_directory=$1
  local destination_directory=$2
  if [ $source_directory != "" -a $destination_directory != "" ];then
    echo "$source_directory -> $destination_directory"
    return 0
  else
    echo "Source ($source_directory) or destination ($destination_directory) directory not given!"
    return 1
  fi
}

function generate_timestamp () {
  local timestamp="$(date +"%Y%m%d-%H%M%S")-"
  echo ${timestamp}
  return 0
}

# Adds trailing slash, if missing
# Fails when path is not absolute, or not within user home, when not root
function sanitize_pathname () {
  local path=$1
  if [ ${#path} -ne 0 ]; then
    if [ ${path:0:1} = "/" -o ${path:0:2} = "~/" -a $(id -u) -ne 0 ]; then
      if [ ${path:${#path}-1} != "/" ]; then
        echo "$path/"
        return 0
      else
        echo "$path"
        return 0
      fi
    else
      echo "Directory must be absolute!"
      return 1
    fi
  else
    echo "Directory can not be empty!"
    return 1
  fi
}

function check_database_server () {
  if [ ! -x /usr/bin/mysql ]; then
    echo "/usr/bin/mysql is not available. Is MariaDB or MySQL actually installed?"
    return 1
  elif [ !$(systemctl is-active mysqld) = "active" ]; then
    echo "No MariaDB or MySQL service is currently running. Start it with 'systemctl start mysqld'."
    return 1
  fi
}

function check_database_settings () {
  if [ -z "$database_destination" ]; then
    echo "The \"database_destination\" variable can not be empty."
    return 1
  elif [ -z "$database_user" ]; then
    echo "The \"database_user\" variable can not be empty."
    return 1
  elif [ -z "$database_password" ]; then
    echo "The \"database_password\" variable can not be empty."
    return 1
  fi
  return 0
}

function check_directory_exists () {
  local destination=$1
  if [ ! -d $destination ]; then
    echo "Directory \"$destination\" does not exist yet. Creating..."
    mkdir -p $destination
  else
    return 0
  fi
}

function check_directory_permission_root () {
  if [ -w $1 ]; then
    return 0
  else
    echo "Directory not writable: $1."
    return 1
  fi
}

function check_directory_writable_user () {
  if [ -w $1 ]; then
    return 0
  else
    echo "Directory not writable: $1."
    return 1
  fi
}

function check_directory_owner_user() {
  if [ ! -O $1 ]; then
    echo "Directory not owned by user $(whoami): $1"
    return 1
  else
    return 0
  fi
}

function check_user_directory () {
  local directory=$1
  check_directory_exists $directory
  check_directory_writable_user $directory
  return 0
}

function check_root_directory () {
  local directory=$1
  check_directory_exists $directory
  check_directory_permission_root $directory
  return 0
}

function get_parent_directory () {
  local directory=$1
  local parent=""
  parent=$(dirname $directory)
  parent=$(sanitize_pathname $parent)
  echo "$parent"
  return 0
}

function get_basename_directory () {
  local directory=$1
  local base=$(basename $directory)
  echo "$base"
  return 0
}

function compress_to_tmp_file () {
  local source_file=$1
  local tmp_file=$2
  echo "Compressing source ($source_file) to temporary file ($tmp_file)."
  case $tar_suffix in
    ".tar.tbz")
      tar cfj "$tmp_file" $source_file
      ;;
    ".tar.tgz")
      tar cfz "$tmp_file" $source_file
      ;;
    ".tar.tlz")
      tar --lzma -cf "$tmp_file" $source_file
      ;;
    ".tar.xz")
      tar cfJ "$tmp_file" $source_file
      ;;
    *)
      echo "Using \"$tar_suffix\" as \$tar_suffix is not supported."
      return 1
      ;;
  esac
  return 0
}

function encrypt_tmp_file () {
  local tmp_file=$1
  local destination_file=$2
  echo "Encrypting $tmp_file to $destination_file."
  #TODO: Failover for still deleting $tmp_file, if encryption fails
  gpg -e \
    -r "$gpg_public_key" \
    -o "$destination_file" \
    "$tmp_file"
  echo "Removing $tmp_file."
  rm -f "$tmp_file"
  return 0
}

function backup_single_directory () {
  local source_directory=$1
  local source_directory_basename=$(get_basename_directory $source_directory)
  local source_parent_directory=$(get_parent_directory $source_directory)
  local tmp_directory=$(sanitize_pathname $tmp)
  local destination_directory=$(sanitize_pathname $2)
  local timestamp=$(generate_timestamp)
  local tmp_file="$tmp_directory$timestamp$source_directory_basename$tar_suffix"
  local destination_file="$destination_directory$timestamp$source_directory_basename$tar_suffix$gpg_suffix"
  notification_source_to_destination "$source_parent_directory$source_directory_basename" $destination_file
  echo "Going to $source_directory_basename's parent directory: $source_parent_directory."
  cd $source_parent_directory
  #TODO: check if directory, not for existence?
  check_directory_exists $source_directory
  "check_"$user_mode"_directory" $tmp_directory
  "check_"$user_mode"_directory" $destination_directory
  compress_to_tmp_file $source_directory_basename $tmp_file
  encrypt_tmp_file $tmp_file $destination_file
}

function backup_multiple_directories () {
  local source_directory=$(sanitize_pathname $1)
  local destination_directory=$(sanitize_pathname $2)
  local layered=0
  if [ ${#@} -gt 2 ]; then
    layered=$3
    echo "Multi-layered"
  fi
  "check_"$user_mode"_directory" $destination_directory
  for sub_directory in $source_directory*
  do
    if [ -d $sub_directory ];then
      "check_"$user_mode"_directory" $sub_directory
      if [ $layered -eq 1 ];then
        backup_multiple_directories $sub_directory $destination_directory$(get_basename_directory $sub_directory)
      else
        backup_single_directory $sub_directory $destination_directory
      fi
    fi
  done

}

function dump_database () {
  local db=$1
  local tmp_file=$2
  mysqldump --force \
    --opt \
    -u$database_user \
    -p$database_password \
    --databases $db > $tmp_file
}

function backup_all_databases () {
  check_database_server
  check_database_settings
  local databases=( )
  local destination=$(sanitize_pathname $database_destination)
  local database
  set +eu
  databases=$(mysql -u$database_user \
    -p$database_password \
    -e "SHOW DATABASES;" \
    | grep -Ev "(Database|information_schema|performance_schema|tmp)")
  set -eu
  if [ ${#databases} -eq 0 ];then
    echo "There are actually no databases on this server. If you've set wrong user or password variables MariaDB/ MySQL will by now have complained about it."
    return 1
  else
    echo "Databases for which backups will be created: ${databases[@]}"
    for database in $databases; do
      backup_database $database $destination
    done
  fi
}

function backup_database () {
  local db=$1
  local destination_directory=$2
  local timestamp=$(generate_timestamp)
  local tmp_directory=$(sanitize_pathname $tmp)
  local sql_file="$timestamp$db$sql_suffix"
  local tmp_file="$tmp_directory$sql_file$tar_suffix"
  local destination_file="$destination_directory$timestamp$db$sql_suffix$tar_suffix$gpg_suffix"
  "check_"$user_mode"_directory" $tmp_directory
  "check_"$user_mode"_directory" $destination_directory
  echo "Going to temporary directory ($tmp_directory)."
  cd $tmp_directory
  dump_database $db $sql_file
  compress_to_tmp_file $sql_file $tmp_file
  encrypt_tmp_file $tmp_file $destination_file
}

function set_user_mode () {
  if [ $(id -u) -eq 0 ]; then
    user_mode="root"
  else
    user_mode="user"
  fi
  return 0
}

function check_gpg_set () {
  if [ -z "$gpg_public_key" ];then
    echo "Error. \"gpg_public_key\" not set!"
    exit 1
  fi
}

function print_help () {
  echo "help"
  exit 0
}

#TODO: Add function to delete compressed data in working directory (also after fail)
#TODO: Add function to cleanup backups
#TODO: Add function to mirror backups
#TODO: Add function to automatically add key to keyring, if not found
#TODO: Create logic to distinguish between different backups
#TODO: Add verbose flag


check_gpg_set
set_user_mode

if [ ${#@} -gt 0 ]; then
  while getopts 'c:hr:s:v' flag; do
    case "${flag}" in
      c)
        echo "Cleanup"
        ;;
      h)
        print_help
        ;;
      r)
        echo "recall"
        ;;
      s)
        source_mode="${OPTARG}"
        ;;
      v)
        verbose='true'
        ;;
      *)
        echo "Error. Unrecognized option: ${flag}."
        exit 1
        ;;
    esac
  done
else
  print_help
fi

if [ -n "$source_mode" ];then
  case $source_mode in
    aura)
      backup_single_directory $aura_source $aura_destination
      ;;
    bitlbee)
      backup_single_directory $bitlbee_source $bitlbee_destination
      ;;
    etc)
      backup_single_directory $etc_source $etc_destination
      ;;
    git)
      backup_multiple_directories $git_source $git_destination
      ;;
    mail)
      if [ "$mail_domains_as_folders" = "yes" ]; then
        backup_multiple_directories $mail_source $mail_destination "1"
      elif [ "$mail_domains_as_folders" = "no" ]; then
        backup_multiple_directories $mail_source $mail_destination
      else
        echo "Setting \"mail_domains_as_folders\" to $mail_domains_as_folders is not supported!"
        exit 1
      fi
      ;;
    mailman)
      backup_single_directory $mailman_source $mailman_destination
      ;;
    databases)
      backup_all_databases
      ;;
    logs)
      backup_single_directory $logs_source $logs_destination
      ;;
    websites)
      backup_multiple_directories $websites_source $websites_destination
      ;;
    firefox)
      backup_single_directory $firefox_source $firefox_destination
      ;;
    thunderbird)
      backup_single_directory $thunderbird_source $thunderbird_destination
      ;;
    weechat)
      backup_single_directory $weechat_source $weechat_destination
      ;;
    *)
      echo "Error. $source_mode is not a valid backup option."
      exit 1
  esac
fi

exit 0