Bob Mottram 9 anni fa
parent
commit
a8eb9c5360
1 ha cambiato i file con 58 aggiunte e 35 eliminazioni
  1. 58
    35
      src/freedombone-backup-remote

+ 58
- 35
src/freedombone-backup-remote Vedi File

@@ -379,6 +379,62 @@ function backup_mariadb {
379 379
     fi
380 380
 }
381 381
 
382
+# Returns the filename of a key share
383
+function get_key_share {
384
+    no_of_shares=$1
385
+    USERNAME="$2"
386
+    REMOTE_DOMAIN="$3"
387
+
388
+    # Get a share index based on the supplied domain name
389
+    # This ensures that the same share is always given to the same domain
390
+    sharenumstr=$(md5sum <<< "$REMOTE_DOMAIN")
391
+    share_index=$(echo $((0x${sharenumstr%% *} % ${no_of_shares})) | tr -d -)
392
+
393
+    # get the filename
394
+    share_files=(/home/$USERNAME/.gnupg_fragments/keyshare.asc.*)
395
+    share_filename=${share_files[share_index]}
396
+
397
+    echo "$share_filename"
398
+}
399
+
400
+function disperse_key_shares {
401
+    USERNAME=$1
402
+    REMOTE_DOMAIN=$2
403
+    REMOTE_SSH_PORT=$3
404
+    REMOTE_PASSWORD=$4
405
+    REMOTE_SERVER=$5
406
+
407
+    if [ -d /home/$USERNAME/.gnupg_fragments ]; then
408
+        if [ $REMOTE_DOMAIN ]; then
409
+            cd /home/$USERNAME/.gnupg_fragments
410
+            no_of_shares=$(ls -afq keyshare.asc.* | wc -l)
411
+            if (( no_of_shares > 1 )); then
412
+                share_filename=$(get_key_share $no_of_shares "$USERNAME" "$REMOTE_DOMAIN")
413
+
414
+                # create a temp directory containing the share
415
+                temp_key_share_dir=/home/$USERNAME/tempkey
416
+                temp_key_share_fragments=$temp_key_share_dir/.gnupg_fragments_${USERNAME}
417
+                mkdir -p $temp_key_share_fragments
418
+                cp $share_filename $temp_key_share_fragments/
419
+
420
+                # copy the fragments directory to the remote server
421
+                /usr/bin/sshpass -p "$REMOTE_PASSWORD" scp -r -P $REMOTE_SSH_PORT $temp_key_share_fragments $REMOTE_SERVER
422
+                if [ ! "$?" = "0" ]; then
423
+                    # Send a warning email
424
+                    echo "Key share to $REMOTE_SERVER failed" | mail -s "${PROJECT_NAME} social key management" $MY_EMAIL_ADDRESS
425
+                fi
426
+
427
+                # remove the temp file/directory
428
+                shred -zu $temp_key_share_fragments/*
429
+                rm -rf $temp_key_share_dir
430
+
431
+                # Send a confirmation email
432
+                echo "Key shared to $REMOTE_SERVER" | mail -s "${PROJECT_NAME} social key management" $MY_EMAIL_ADDRESS
433
+            fi
434
+        fi
435
+    fi
436
+}
437
+
382 438
 backup_configuration
383 439
 backup_users
384 440
 backup_letsencrypt
@@ -418,44 +474,11 @@ do
418 474
         for d in /home/*/ ; do
419 475
             USERNAME=$(echo "$d" | awk -F '/' '{print $3}')
420 476
             if [[ $USERNAME != "git" ]]; then
421
-                if [ -d /home/$USERNAME/.gnupg_fragments ]; then
422
-                    if [ $REMOTE_DOMAIN ]; then
423
-                        cd /home/$USERNAME/.gnupg_fragments
424
-                        no_of_shares=$(ls -afq keyshare.asc.* | wc -l)
425
-                        if (( no_of_shares > 0 )); then
426
-                            # Pick a share index based on the domain name
427
-                            # This ensures that the same share is always given to the same domain
428
-                            sharenumstr=$(md5sum <<< "$REMOTE_DOMAIN")
429
-                            share_index=$(echo $((0x${sharenumstr%% *} % ${no_of_shares})) | tr -d -)
430
-
431
-                            # get the share filename
432
-                            share_files=(/home/$USERNAME/.gnupg_fragments/keyshare.asc.*)
433
-                            share_filename=${share_files[share_index]}
434
-
435
-                            # create a temp directory containing the share
436
-                            mkdir -p /home/$USERNAME/tempkey/.gnupg_fragments_$USERNAME
437
-                            cp $share_filename /home/$USERNAME/tempkey/.gnupg_fragments_$USERNAME/
438
-
439
-                            # copy the fragments directory to the remote server
440
-                            /usr/bin/sshpass -p $REMOTE_PASSWORD scp -r -P $REMOTE_SSH_PORT /home/$USERNAME/tempkey/.gnupg_fragments_$USERNAME $REMOTE_SERVER
441
-                            if [ ! "$?" = "0" ]; then
442
-                                # Send a warning email
443
-                                echo "Key share to $REMOTE_SERVER failed" | mail -s "${PROJECT_NAME} social key management" $MY_EMAIL_ADDRESS
444
-                            fi
445
-
446
-                            # remove the temp file/directory
447
-                            shred -zu /home/$USERNAME/tempkey/.gnupg_fragments_$USERNAME/*
448
-                            rm -rf /home/$USERNAME/tempkey
449
-
450
-                            # Send a confirmation email
451
-                            echo "Key shared to $REMOTE_SERVER" | mail -s "${PROJECT_NAME} social key management" $MY_EMAIL_ADDRESS
452
-                        fi
453
-                    fi
454
-                fi
477
+                disperse_key_shares $USERNAME $REMOTE_DOMAIN $REMOTE_SSH_PORT "$REMOTE_PASSWORD" $REMOTE_SERVER
455 478
             fi
456 479
         done
457 480
 
458
-        rsync -ratlzv --rsh="/usr/bin/sshpass -p $REMOTE_PASSWORD ssh -p $REMOTE_SSH_PORT -o StrictHostKeyChecking=no" $SERVER_DIRECTORY/backup $REMOTE_SERVER
481
+        rsync -ratlzv --rsh="/usr/bin/sshpass -p \"$REMOTE_PASSWORD\" ssh -p $REMOTE_SSH_PORT -o StrictHostKeyChecking=no" $SERVER_DIRECTORY/backup $REMOTE_SERVER
459 482
         if [ ! "$?" = "0" ]; then
460 483
             echo "$NOW Backup to $REMOTE_SERVER failed" >> /var/log/remotebackups.log
461 484
             # Send a warning email