Parcourir la source

Backup and restore of postgresql

Bob Mottram il y a 7 ans
Parent
révision
5e5153eeaf

+ 23
- 0
src/freedombone-backup-local Voir le fichier

@@ -337,6 +337,28 @@ function backup_mariadb {
337 337
     fi
338 338
 }
339 339
 
340
+function backup_postgresql {
341
+    if [ ! -d /etc/postgresql ]; then
342
+        return
343
+    fi
344
+
345
+    temp_backup_dir=/root/temppostgresql
346
+    if [ ! -d $temp_backup_dir ]; then
347
+        mkdir $temp_backup_dir
348
+    fi
349
+    sudo -u postgres pg_dumpall --roles-only > $temp_backup_dir/postgresql.sql
350
+    if [ ! -s $temp_backup_dir/postgresql.sql ]; then
351
+        echo $"Unable to backup postgresql settings"
352
+        rm -rf $temp_backup_dir
353
+        umount $USB_MOUNT
354
+        rm -rf $USB_MOUNT
355
+        exit 684365
356
+    fi
357
+    echo "$DATABASE_PASSWORD" > $temp_backup_dir/db
358
+    chmod 400 $temp_backup_dir/db
359
+    backup_directory_to_usb $temp_backup_dir postgresql
360
+}
361
+
340 362
 # has the remove option been set ?
341 363
 remove_option=$2
342 364
 if [[ $1 == "remove" ]]; then
@@ -355,6 +377,7 @@ backup_configfiles
355 377
 backup_blocklist
356 378
 backup_admin_readme
357 379
 backup_mariadb
380
+backup_postgresql
358 381
 backup_extra_directories local
359 382
 backup_unmount_drive $USB_DRIVE $USB_MOUNT
360 383
 echo $"Backup to USB drive is complete. You can now unplug it."

+ 23
- 0
src/freedombone-backup-remote Voir le fichier

@@ -336,6 +336,28 @@ function backup_mariadb {
336 336
     fi
337 337
 }
338 338
 
339
+function backup_postgresql {
340
+    if [ ! -d /etc/postgresql ]; then
341
+        return
342
+    fi
343
+
344
+    temp_backup_dir=/root/temppostgresql
345
+    if [ ! -d $temp_backup_dir ]; then
346
+        mkdir $temp_backup_dir
347
+    fi
348
+    sudo -u postgres pg_dumpall --roles-only > $temp_backup_dir/postgresql.sql
349
+    if [ ! -s $temp_backup_dir/postgresql.sql ]; then
350
+        echo $"Unable to backup postgresql settings"
351
+        rm -rf $temp_backup_dir
352
+        umount $USB_MOUNT
353
+        rm -rf $USB_MOUNT
354
+        exit 684365
355
+    fi
356
+    echo "$DATABASE_PASSWORD" > $temp_backup_dir/db
357
+    chmod 400 $temp_backup_dir/db
358
+    backup_directory_to_friend $temp_backup_dir postgresql
359
+}
360
+
339 361
 # Returns the filename of a key share
340 362
 function get_key_share {
341 363
     no_of_shares=$1
@@ -410,6 +432,7 @@ if [[ $TEST_MODE == "no" ]]; then
410 432
     backup_web_server
411 433
     backup_admin_readme
412 434
     backup_mariadb
435
+    backup_postgresql
413 436
     backup_certs
414 437
     backup_mailing_list
415 438
     backup_apps remote

+ 54
- 0
src/freedombone-restore-local Voir le fichier

@@ -289,6 +289,59 @@ function restore_mariadb {
289 289
     fi
290 290
 }
291 291
 
292
+function restore_postgresql {
293
+    if [[ $RESTORE_APP != 'all' ]]; then
294
+        if [[ $RESTORE_APP != 'postgresql' ]]; then
295
+            return
296
+        fi
297
+    fi
298
+
299
+    if [[ $(is_completed install_postgresql) == "0" ]]; then
300
+        function_check install_postgresql
301
+        install_postgresql
302
+    fi
303
+
304
+    if [ -d $USB_MOUNT/backup/postgresql ]; then
305
+        echo $"Restoring postgresql settings"
306
+        temp_restore_dir=/root/temppostgresql
307
+        restore_directory_from_usb $temp_restore_dir postgresql
308
+
309
+        store_original_postgresql_password
310
+
311
+        echo $'Obtaining original postgresql password'
312
+        db_pass=$(cat /root/.postgresqloriginal)
313
+        if [ ${#db_pass} -gt 0 ]; then
314
+            echo $"Restore the postgresql user table"
315
+            if [ -d ${temp_restore_dir}${temp_restore_dir} ]; then
316
+                mysqlsuccess=$(sudo -u postgres pg_restore ${temp_restore_dir}${temp_restore_dir}/postgresql.sql)
317
+            else
318
+                mysqlsuccess=$(sudo -u postgres pg_restore ${temp_restore_dir}/postgresql.sql)
319
+            fi
320
+            if [ ! "$?" = "0" ]; then
321
+                echo $"Try again using the password obtained from backup"
322
+                db_pass=$(${PROJECT_NAME}-pass -u root -a postgresql)
323
+                if [ -d ${temp_restore_dir}${temp_restore_dir} ]; then
324
+                    mysqlsuccess=$(sudo -u postgres pg_restore ${temp_restore_dir}${temp_restore_dir}/postgresql.sql)
325
+                else
326
+                    mysqlsuccess=$(sudo -u postgres pg_restore ${temp_restore_dir}/postgresql.sql)
327
+                fi
328
+            fi
329
+            if [ ! "$?" = "0" ]; then
330
+                echo "$mysqlsuccess"
331
+                set_user_permissions
332
+                backup_unmount_drive
333
+                exit 73825
334
+            fi
335
+            echo $"Restarting database"
336
+            systemctl restart postgresql
337
+            echo $"Ensure postgresql handles authentication"
338
+            POSTGRESQL_PASSWORD=$(${PROJECT_NAME}-pass -u root -a postgresql)
339
+            DATABASE_PASSWORD=$(${PROJECT_NAME}-pass -u root -a postgresql)
340
+        fi
341
+        rm -rf $temp_restore_dir
342
+    fi
343
+}
344
+
292 345
 function restore_letsencrypt {
293 346
     if [[ $RESTORE_APP != 'all' ]]; then
294 347
         if [[ $RESTORE_APP != 'letsencrypt' ]]; then
@@ -865,6 +918,7 @@ restore_configfiles
865 918
 same_admin_user
866 919
 restore_passwordstore
867 920
 restore_mariadb
921
+restore_postgresql
868 922
 restore_letsencrypt
869 923
 restore_tor
870 924
 restore_mutt_settings

+ 51
- 0
src/freedombone-restore-remote Voir le fichier

@@ -255,6 +255,56 @@ function restore_mariadb {
255 255
     fi
256 256
 }
257 257
 
258
+function restore_postgresql {
259
+    if [[ $RESTORE_APP != 'all' ]]; then
260
+        if [[ $RESTORE_APP != 'postgresql' ]]; then
261
+            return
262
+        fi
263
+    fi
264
+
265
+    if [[ $(is_completed install_postgresql) == "0" ]]; then
266
+        function_check install_postgresql
267
+        install_postgresql
268
+    fi
269
+
270
+    if [ -d $SERVER_DIRECTORY/backup/postgresql ]; then
271
+        echo $"Restoring Postgresql settings"
272
+        temp_restore_dir=/root/temppostgresql
273
+        restore_directory_from_friend $temp_restore_dir postgresql
274
+
275
+        store_original_postgresql_password
276
+
277
+        echo $'Obtaining Postgresql password'
278
+        db_pass=$(cat /root/.postgresqloriginal)
279
+        if [ ${#db_pass} -gt 0 ]; then
280
+            echo $"Restore the Postgresql user table"
281
+            if [ -d ${temp_restore_dir}${temp_restore_dir} ]; then
282
+                mysqlsuccess=$(sudo -u postgres pg_restore ${temp_restore_dir}${temp_restore_dir}/postgresql.sql)
283
+            else
284
+                mysqlsuccess=$(sudo -u postgres pg_restore ${temp_restore_dir}/postgresql.sql)
285
+            fi
286
+            if [ ! "$?" = "0" ]; then
287
+                echo $"Try again using the password obtained from backup"
288
+                db_pass=$(${PROJECT_NAME}-pass -u root -a postgresql)
289
+                if [ -d ${temp_restore_dir}${temp_restore_dir} ]; then
290
+                    mysqlsuccess=$(sudo -u postgres pg_restore ${temp_restore_dir}${temp_restore_dir}/postgresql.sql)
291
+                else
292
+                    mysqlsuccess=$(sudo -u postgres pg_restore ${temp_restore_dir}/postgresql.sql)
293
+                fi
294
+            fi
295
+            if [ ! "$?" = "0" ]; then
296
+                echo "$mysqlsuccess"
297
+                exit 962
298
+            fi
299
+            echo $"Restarting database"
300
+            systemctl restart postgresql
301
+            echo $"Ensure postgresql handles authentication"
302
+            POSTGRESQL_PASSWORD=$(${PROJECT_NAME}-pass -u root -a postgresql)
303
+        fi
304
+        rm -rf ${temp_restore_dir}
305
+    fi
306
+}
307
+
258 308
 function restore_letsencrypt {
259 309
     if [[ $RESTORE_APP != 'all' ]]; then
260 310
         if [[ $RESTORE_APP != 'letsencrypt' ]]; then
@@ -769,6 +819,7 @@ restore_blocklist
769 819
 restore_configfiles
770 820
 restore_passwordstore
771 821
 restore_mariadb
822
+restore_postgresql
772 823
 restore_letsencrypt
773 824
 restore_mutt_settings
774 825
 restore_gpg

+ 10
- 5
src/freedombone-utils-backup Voir le fichier

@@ -241,7 +241,7 @@ function backup_database_local_usb {
241 241
         mysqldump --lock-tables --password="$DATABASE_PASSWORD" ${1} > ${local_database_dir}/${1}.sql
242 242
     else
243 243
         USE_POSTGRESQL=
244
-        pg_dump ${1} > ${local_database_dir}/${1}.sql
244
+        sudo -u postgres pg_dump ${1} > ${local_database_dir}/${1}.sql
245 245
     fi
246 246
     if [ -f ${local_database_dir}/${1}.sql ]; then
247 247
         if [ ! -s ${local_database_dir}/${1}.sql ]; then
@@ -557,7 +557,7 @@ function backup_database_remote {
557 557
         mysqldump --lock-tables --password="$DATABASE_PASSWORD" ${1} > ${local_database_dir}/${1}.sql
558 558
     else
559 559
         USE_POSTGRESQL=
560
-        pg_dump ${1} > ${local_database_dir}/${1}.sql
560
+        sudo -u postgres pg_dump ${1} > ${local_database_dir}/${1}.sql
561 561
     fi
562 562
 
563 563
     if [ -f ${local_database_dir}/${1}.sql ]; then
@@ -659,7 +659,7 @@ function restore_database_from_friend {
659 659
             mysqlsuccess=$(mysql -u root --password="$DATABASE_PASSWORD" ${1} -o < ${local_database_dir}/${RESTORE_SUBDIR}/temp${1}data/${1}.sql)
660 660
         else
661 661
             USE_POSTGRESQL=
662
-            mysqlsuccess=$(sudo -u postgres psql $database_name < ${local_database_dir}/${RESTORE_SUBDIR}/temp${1}data/${1}.sql)
662
+            mysqlsuccess=$(sudo -u postgres pg_restore ${local_database_dir}/${RESTORE_SUBDIR}/temp${1}data/${1}.sql)
663 663
         fi
664 664
         if [ ! "$?" = "0" ]; then
665 665
             echo "$mysqlsuccess"
@@ -738,8 +738,13 @@ function restore_database {
738 738
             backup_unmount_drive
739 739
             exit 503
740 740
         fi
741
-        keep_database_running
742
-        mysqlsuccess=$(mysql -u root --password="$DATABASE_PASSWORD" ${restore_app_name} -o < $database_file)
741
+        if [ ! $USE_POSTGRESQL ]; then
742
+            keep_database_running
743
+            mysqlsuccess=$(mysql -u root --password="$DATABASE_PASSWORD" ${restore_app_name} -o < $database_file)
744
+        else
745
+            USE_POSTGRESQL=
746
+            mysqlsuccess=$(sudo -u postgres pg_restore $database_file)
747
+        fi
743 748
         if [ ! "$?" = "0" ]; then
744 749
             echo "$mysqlsuccess"
745 750
             function_check set_user_permissions

+ 9
- 0
src/freedombone-utils-postgresql Voir le fichier

@@ -31,6 +31,15 @@
31 31
 # Set this when calling backup and restore commands
32 32
 USE_POSTGRESQL=
33 33
 
34
+function store_original_postgresql_password {
35
+    if [ ! -f /root/.postgresqloriginal ]; then
36
+        echo $'Storing original postgresql password'
37
+        ORIGINAL_POSTGRESQL_PASSWORD=$(${PROJECT_NAME}-pass -u root -a postgresql)
38
+        # We can store this in plaintext because it will soon be of historical interest only
39
+        echo -n "$ORIGINAL_POSTGRESQL_PASSWORD" > /root/.postgresqloriginal
40
+    fi
41
+}
42
+
34 43
 function get_postgresql_password {
35 44
     POSTGRESQL_PASSWORD=$(${PROJECT_NAME}-pass -u root -a postgresql)
36 45
     if [[ "$POSTGRESQL_PASSWORD" == *'failed'* ]]; then