浏览代码

Backup and restore of postgresql

Bob Mottram 7 年前
父节点
当前提交
5e5153eeaf

+ 23
- 0
src/freedombone-backup-local 查看文件

337
     fi
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
 # has the remove option been set ?
362
 # has the remove option been set ?
341
 remove_option=$2
363
 remove_option=$2
342
 if [[ $1 == "remove" ]]; then
364
 if [[ $1 == "remove" ]]; then
355
 backup_blocklist
377
 backup_blocklist
356
 backup_admin_readme
378
 backup_admin_readme
357
 backup_mariadb
379
 backup_mariadb
380
+backup_postgresql
358
 backup_extra_directories local
381
 backup_extra_directories local
359
 backup_unmount_drive $USB_DRIVE $USB_MOUNT
382
 backup_unmount_drive $USB_DRIVE $USB_MOUNT
360
 echo $"Backup to USB drive is complete. You can now unplug it."
383
 echo $"Backup to USB drive is complete. You can now unplug it."

+ 23
- 0
src/freedombone-backup-remote 查看文件

336
     fi
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
 # Returns the filename of a key share
361
 # Returns the filename of a key share
340
 function get_key_share {
362
 function get_key_share {
341
     no_of_shares=$1
363
     no_of_shares=$1
410
     backup_web_server
432
     backup_web_server
411
     backup_admin_readme
433
     backup_admin_readme
412
     backup_mariadb
434
     backup_mariadb
435
+    backup_postgresql
413
     backup_certs
436
     backup_certs
414
     backup_mailing_list
437
     backup_mailing_list
415
     backup_apps remote
438
     backup_apps remote

+ 54
- 0
src/freedombone-restore-local 查看文件

289
     fi
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
 function restore_letsencrypt {
345
 function restore_letsencrypt {
293
     if [[ $RESTORE_APP != 'all' ]]; then
346
     if [[ $RESTORE_APP != 'all' ]]; then
294
         if [[ $RESTORE_APP != 'letsencrypt' ]]; then
347
         if [[ $RESTORE_APP != 'letsencrypt' ]]; then
865
 same_admin_user
918
 same_admin_user
866
 restore_passwordstore
919
 restore_passwordstore
867
 restore_mariadb
920
 restore_mariadb
921
+restore_postgresql
868
 restore_letsencrypt
922
 restore_letsencrypt
869
 restore_tor
923
 restore_tor
870
 restore_mutt_settings
924
 restore_mutt_settings

+ 51
- 0
src/freedombone-restore-remote 查看文件

255
     fi
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
 function restore_letsencrypt {
308
 function restore_letsencrypt {
259
     if [[ $RESTORE_APP != 'all' ]]; then
309
     if [[ $RESTORE_APP != 'all' ]]; then
260
         if [[ $RESTORE_APP != 'letsencrypt' ]]; then
310
         if [[ $RESTORE_APP != 'letsencrypt' ]]; then
769
 restore_configfiles
819
 restore_configfiles
770
 restore_passwordstore
820
 restore_passwordstore
771
 restore_mariadb
821
 restore_mariadb
822
+restore_postgresql
772
 restore_letsencrypt
823
 restore_letsencrypt
773
 restore_mutt_settings
824
 restore_mutt_settings
774
 restore_gpg
825
 restore_gpg

+ 10
- 5
src/freedombone-utils-backup 查看文件

241
         mysqldump --lock-tables --password="$DATABASE_PASSWORD" ${1} > ${local_database_dir}/${1}.sql
241
         mysqldump --lock-tables --password="$DATABASE_PASSWORD" ${1} > ${local_database_dir}/${1}.sql
242
     else
242
     else
243
         USE_POSTGRESQL=
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
     fi
245
     fi
246
     if [ -f ${local_database_dir}/${1}.sql ]; then
246
     if [ -f ${local_database_dir}/${1}.sql ]; then
247
         if [ ! -s ${local_database_dir}/${1}.sql ]; then
247
         if [ ! -s ${local_database_dir}/${1}.sql ]; then
557
         mysqldump --lock-tables --password="$DATABASE_PASSWORD" ${1} > ${local_database_dir}/${1}.sql
557
         mysqldump --lock-tables --password="$DATABASE_PASSWORD" ${1} > ${local_database_dir}/${1}.sql
558
     else
558
     else
559
         USE_POSTGRESQL=
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
     fi
561
     fi
562
 
562
 
563
     if [ -f ${local_database_dir}/${1}.sql ]; then
563
     if [ -f ${local_database_dir}/${1}.sql ]; then
659
             mysqlsuccess=$(mysql -u root --password="$DATABASE_PASSWORD" ${1} -o < ${local_database_dir}/${RESTORE_SUBDIR}/temp${1}data/${1}.sql)
659
             mysqlsuccess=$(mysql -u root --password="$DATABASE_PASSWORD" ${1} -o < ${local_database_dir}/${RESTORE_SUBDIR}/temp${1}data/${1}.sql)
660
         else
660
         else
661
             USE_POSTGRESQL=
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
         fi
663
         fi
664
         if [ ! "$?" = "0" ]; then
664
         if [ ! "$?" = "0" ]; then
665
             echo "$mysqlsuccess"
665
             echo "$mysqlsuccess"
738
             backup_unmount_drive
738
             backup_unmount_drive
739
             exit 503
739
             exit 503
740
         fi
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
         if [ ! "$?" = "0" ]; then
748
         if [ ! "$?" = "0" ]; then
744
             echo "$mysqlsuccess"
749
             echo "$mysqlsuccess"
745
             function_check set_user_permissions
750
             function_check set_user_permissions

+ 9
- 0
src/freedombone-utils-postgresql 查看文件

31
 # Set this when calling backup and restore commands
31
 # Set this when calling backup and restore commands
32
 USE_POSTGRESQL=
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
 function get_postgresql_password {
43
 function get_postgresql_password {
35
     POSTGRESQL_PASSWORD=$(${PROJECT_NAME}-pass -u root -a postgresql)
44
     POSTGRESQL_PASSWORD=$(${PROJECT_NAME}-pass -u root -a postgresql)
36
     if [[ "$POSTGRESQL_PASSWORD" == *'failed'* ]]; then
45
     if [[ "$POSTGRESQL_PASSWORD" == *'failed'* ]]; then