freedombone-restore-local 36KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007
  1. #!/bin/bash
  2. #
  3. # .---. . .
  4. # | | |
  5. # |--- .--. .-. .-. .-.| .-. .--.--. |.-. .-. .--. .-.
  6. # | | (.-' (.-' ( | ( )| | | | )( )| | (.-'
  7. # ' ' --' --' -' - -' ' ' -' -' -' ' - --'
  8. #
  9. # Freedom in the Cloud
  10. #
  11. # Restore from local storage - typically a USB drive
  12. # License
  13. # =======
  14. #
  15. # Copyright (C) 2015-2018 Bob Mottram <bob@freedombone.net>
  16. #
  17. # This program is free software: you can redistribute it and/or modify
  18. # it under the terms of the GNU Affero General Public License as published by
  19. # the Free Software Foundation, either version 3 of the License, or
  20. # (at your option) any later version.
  21. #
  22. # This program is distributed in the hope that it will be useful,
  23. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  24. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  25. # GNU Affero General Public License for more details.
  26. #
  27. # You should have received a copy of the GNU Affero General Public License
  28. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  29. PROJECT_NAME='freedombone'
  30. COMPLETION_FILE=$HOME/${PROJECT_NAME}-completed.txt
  31. MONGODB_APPS_FILE=$HOME/.mongodbapps
  32. CONFIGURATION_FILE="$HOME/${PROJECT_NAME}.cfg"
  33. BACKUP_EXTRA_DIRECTORIES=/root/backup-extra-dirs.csv
  34. # whether to restore everything or just a specific application
  35. RESTORE_APP='all'
  36. export TEXTDOMAIN=${PROJECT_NAME}-restore-local
  37. export TEXTDOMAINDIR="/usr/share/locale"
  38. PROJECT_INSTALL_DIR=/usr/local/bin
  39. if [ -f /usr/bin/${PROJECT_NAME} ]; then
  40. PROJECT_INSTALL_DIR=/usr/bin
  41. fi
  42. # MariaDB password
  43. DATABASE_PASSWORD=$(${PROJECT_NAME}-pass -u root -a mariadb)
  44. function please_wait {
  45. local str width height length
  46. width=$(tput cols)
  47. height=$(tput lines)
  48. str="Standby to restore from USB"
  49. length=${#str}
  50. clear
  51. tput cup $((height / 2)) $(((width / 2) - (length / 2)))
  52. echo "$str"
  53. tput cup $((height * 3 / 5)) $(((width / 2)))
  54. echo -n ''
  55. }
  56. please_wait
  57. source $PROJECT_INSTALL_DIR/${PROJECT_NAME}-vars
  58. # include utils which allow function_check, go and drive mount
  59. UTILS_FILES="/usr/share/${PROJECT_NAME}/utils/${PROJECT_NAME}-utils-*"
  60. for f in $UTILS_FILES
  61. do
  62. source "$f"
  63. done
  64. clear
  65. USB_DRIVE=/dev/sdb1
  66. USB_MOUNT=/mnt/usb
  67. ADMIN_USERNAME=''
  68. ADMIN_NAME=
  69. read_config_param USB_DRIVE
  70. if [ -f "$COMPLETION_FILE" ]; then
  71. ADMIN_USERNAME=$(get_completion_param "Admin user")
  72. fi
  73. function check_backup_exists {
  74. if [ ! -d $USB_MOUNT/backup ]; then
  75. echo $"No backup directory found on the USB drive."
  76. set_user_permissions
  77. backup_unmount_drive
  78. exit 2
  79. fi
  80. }
  81. function check_admin_user {
  82. echo $"Checking that admin user exists"
  83. if [ ! -d "/home/$ADMIN_USERNAME" ]; then
  84. echo $"Username $ADMIN_USERNAME not found. Reinstall ${PROJECT_NAME} with this username."
  85. set_user_permissions
  86. backup_unmount_drive
  87. exit 295
  88. fi
  89. }
  90. function copy_gpg_keys {
  91. echo $"Copying GPG keys from admin user to root"
  92. cp -r "/home/$ADMIN_USERNAME/.gnupg" /root
  93. gpg_set_permissions root
  94. }
  95. function restore_blocklist {
  96. if [[ $RESTORE_APP != 'all' ]]; then
  97. if [[ $RESTORE_APP != 'blocklist' ]]; then
  98. return
  99. fi
  100. fi
  101. if [ -d $USB_MOUNT/backup/blocklist ]; then
  102. echo $"Restoring blocklist"
  103. temp_restore_dir=/root/tempblocklist
  104. restore_directory_from_usb $temp_restore_dir blocklist
  105. if [ -d $temp_restore_dir/root/tempbackupblocklist ]; then
  106. cp -f $temp_restore_dir/root/tempbackupblocklist/${PROJECT_NAME}-firewall-domains.cfg /root/${PROJECT_NAME}-firewall-domains.cfg
  107. else
  108. cp -f $temp_restore_dir/${PROJECT_NAME}-firewall-domains.cfg /root/${PROJECT_NAME}-firewall-domains.cfg
  109. fi
  110. rm -rf $temp_restore_dir
  111. firewall_refresh_blocklist
  112. fi
  113. }
  114. function restore_configfiles {
  115. if [[ $RESTORE_APP != 'all' ]]; then
  116. if [[ $RESTORE_APP != 'configfiles' ]]; then
  117. return
  118. fi
  119. fi
  120. # this restores *.cfg and COMPLETION_FILE
  121. if [ -d $USB_MOUNT/backup/configfiles ]; then
  122. echo $"Restoring configuration files"
  123. temp_restore_dir=/root/tempconfigfiles
  124. restore_directory_from_usb $temp_restore_dir configfiles
  125. if [ -d $temp_restore_dir/root ]; then
  126. if [ -f $temp_restore_dir/root/.nostore ]; then
  127. if [ ! -f /root/.nostore ]; then
  128. touch /root/.nostore
  129. fi
  130. else
  131. if [ -f /root/.nostore ]; then
  132. rm /root/.nostore
  133. fi
  134. fi
  135. else
  136. if [ -f $temp_restore_dir/.nostore ]; then
  137. if [ ! -f /root/.nostore ]; then
  138. touch /root/.nostore
  139. fi
  140. else
  141. if [ -f /root/.nostore ]; then
  142. rm /root/.nostore
  143. fi
  144. fi
  145. fi
  146. #if [ -f $temp_restore_dir$NODEJS_INSTALLED_APPS_FILE ]; then
  147. # cp -f $temp_restore_dir$NODEJS_INSTALLED_APPS_FILE $NODEJS_INSTALLED_APPS_FILE
  148. #fi
  149. #if [ -f $temp_restore_dir/root/${PROJECT_NAME}.cfg ]; then
  150. # cp -f $temp_restore_dir/root/${PROJECT_NAME}.cfg $CONFIGURATION_FILE
  151. # if [ ! "$?" = "0" ]; then
  152. # set_user_permissions
  153. # backup_unmount_drive
  154. # rm -rf $temp_restore_dir
  155. # exit 5294
  156. # fi
  157. #fi
  158. if [ -f "$temp_restore_dir$MONGODB_APPS_FILE" ]; then
  159. if ! cp -f "$temp_restore_dir$MONGODB_APPS_FILE" "$MONGODB_APPS_FILE"; then
  160. set_user_permissions
  161. backup_unmount_drive
  162. rm -rf "$temp_restore_dir"
  163. exit 859034853
  164. fi
  165. fi
  166. #if [ -f $CONFIGURATION_FILE ]; then
  167. # # install according to the config file
  168. # freedombone -c $CONFIGURATION_FILE
  169. #fi
  170. #if [ -f $temp_restore_dir/root/${PROJECT_NAME}-completed.txt ]; then
  171. # cp -f $temp_restore_dir/root/${PROJECT_NAME}-completed.txt $COMPLETION_FILE
  172. # if [ ! "$?" = "0" ]; then
  173. # set_user_permissions
  174. # backup_unmount_drive
  175. # rm -rf $temp_restore_dir
  176. # exit 6382
  177. # fi
  178. #fi
  179. if [ -f "${temp_restore_dir}${BACKUP_EXTRA_DIRECTORIES}" ]; then
  180. if ! cp -f "${temp_restore_dir}${BACKUP_EXTRA_DIRECTORIES}" "${BACKUP_EXTRA_DIRECTORIES}"; then
  181. set_user_permissions
  182. backup_unmount_drive
  183. rm -rf "$temp_restore_dir"
  184. exit 62121
  185. fi
  186. fi
  187. # restore nginx password hashes
  188. if [ -d $temp_restore_dir/root ]; then
  189. if [ -f $temp_restore_dir/root/htpasswd ]; then
  190. cp -f $temp_restore_dir/root/htpasswd /etc/nginx/.htpasswd
  191. fi
  192. else
  193. if [ -f $temp_restore_dir/htpasswd ]; then
  194. cp -f $temp_restore_dir/htpasswd /etc/nginx/.htpasswd
  195. fi
  196. fi
  197. rm -rf $temp_restore_dir
  198. fi
  199. }
  200. function same_admin_user {
  201. PREV_ADMIN_USERNAME=$(get_completion_param "Admin user")
  202. if [[ "$PREV_ADMIN_USERNAME" != "$ADMIN_USERNAME" ]]; then
  203. echo $"The admin username has changed from $PREV_ADMIN_USERNAME to $ADMIN_USERNAME. To restore you will first need to install a new ${PROJECT_NAME} system with an initial admin user named $PREV_ADMIN_USERNAME"
  204. set_user_permissions
  205. backup_unmount_drive
  206. exit 73265
  207. fi
  208. }
  209. function restore_mariadb {
  210. if [[ $RESTORE_APP != 'all' ]]; then
  211. if [[ $RESTORE_APP != 'mariadb' ]]; then
  212. return
  213. fi
  214. fi
  215. if [[ $(is_completed install_mariadb) == "0" ]]; then
  216. function_check install_mariadb
  217. install_mariadb
  218. fi
  219. if [ -d $USB_MOUNT/backup/mariadb ]; then
  220. echo $"Restoring mysql settings"
  221. keep_database_running
  222. temp_restore_dir=/root/tempmariadb
  223. restore_directory_from_usb $temp_restore_dir mariadb
  224. store_original_mariadb_password
  225. echo $'Obtaining original MariaDB password'
  226. db_pass=$(cat /root/.mariadboriginal)
  227. if [ ${#db_pass} -gt 0 ]; then
  228. echo $"Restore the MariaDB user table"
  229. if [ -d ${temp_restore_dir}${temp_restore_dir} ]; then
  230. mysqlsuccess=$(mysql -u root --password="$db_pass" mysql -o < ${temp_restore_dir}${temp_restore_dir}/mysql.sql)
  231. else
  232. mysqlsuccess=$(mysql -u root --password="$db_pass" mysql -o < ${temp_restore_dir}/mysql.sql)
  233. fi
  234. # shellcheck disable=SC2181
  235. if [ ! "$?" = "0" ]; then
  236. echo $"Try again using the password obtained from backup"
  237. db_pass=$(${PROJECT_NAME}-pass -u root -a mariadb)
  238. if [ -d ${temp_restore_dir}${temp_restore_dir} ]; then
  239. mysqlsuccess=$(mysql -u root --password="$db_pass" mysql -o < ${temp_restore_dir}${temp_restore_dir}/mysql.sql)
  240. else
  241. mysqlsuccess=$(mysql -u root --password="$db_pass" mysql -o < ${temp_restore_dir}/mysql.sql)
  242. fi
  243. fi
  244. # shellcheck disable=SC2181
  245. if [ ! "$?" = "0" ]; then
  246. echo "$mysqlsuccess"
  247. set_user_permissions
  248. backup_unmount_drive
  249. exit 962
  250. fi
  251. echo $"Restarting database"
  252. systemctl restart mariadb
  253. echo $"Ensure MariaDB handles authentication"
  254. MARIADB_PASSWORD=$("${PROJECT_NAME}-pass" -u root -a mariadb)
  255. mariadb_fix_authentication
  256. DATABASE_PASSWORD=$("${PROJECT_NAME}-pass" -u root -a mariadb)
  257. fi
  258. rm -rf $temp_restore_dir
  259. fi
  260. }
  261. function restore_postgresql {
  262. if [[ $RESTORE_APP != 'all' ]]; then
  263. if [[ $RESTORE_APP != 'postgresql' ]]; then
  264. return
  265. fi
  266. fi
  267. if [[ $(is_completed install_postgresql) == "0" ]]; then
  268. function_check install_postgresql
  269. install_postgresql
  270. fi
  271. if [ -d $USB_MOUNT/backup/postgresql ]; then
  272. echo $"Restoring postgresql settings"
  273. temp_restore_dir=/root/temppostgresql
  274. restore_directory_from_usb $temp_restore_dir postgresql
  275. store_original_postgresql_password
  276. echo $'Obtaining original postgresql password'
  277. db_pass=$(cat /root/.postgresqloriginal)
  278. if [ ${#db_pass} -gt 0 ]; then
  279. echo $"Restore the postgresql user table"
  280. if [ -d ${temp_restore_dir}${temp_restore_dir} ]; then
  281. mysqlsuccess=$(sudo -u postgres pg_restore ${temp_restore_dir}${temp_restore_dir}/postgresql.sql)
  282. else
  283. mysqlsuccess=$(sudo -u postgres pg_restore ${temp_restore_dir}/postgresql.sql)
  284. fi
  285. # shellcheck disable=SC2181
  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. # shellcheck disable=SC2181
  296. if [ ! "$?" = "0" ]; then
  297. echo "$mysqlsuccess"
  298. set_user_permissions
  299. backup_unmount_drive
  300. exit 73825
  301. fi
  302. echo $"Restarting database"
  303. systemctl restart postgresql
  304. echo $"Ensure postgresql handles authentication"
  305. POSTGRESQL_PASSWORD=$("${PROJECT_NAME}-pass" -u root -a postgresql)
  306. DATABASE_PASSWORD=$("${PROJECT_NAME}-pass" -u root -a postgresql)
  307. fi
  308. rm -rf $temp_restore_dir
  309. fi
  310. }
  311. function restore_letsencrypt {
  312. if [[ $RESTORE_APP != 'all' ]]; then
  313. if [[ $RESTORE_APP != 'letsencrypt' ]]; then
  314. return
  315. fi
  316. fi
  317. if [ -d $USB_MOUNT/backup/letsencrypt ]; then
  318. echo $"Restoring Lets Encrypt settings"
  319. #restore_directory_from_usb / letsencrypt
  320. restore_directory_from_usb /etc/letsencrypt letsencrypt
  321. chgrp -R ssl-cert /etc/letsencrypt
  322. chmod -R g=rX /etc/letsencrypt
  323. fi
  324. }
  325. function restore_passwordstore {
  326. if [[ $RESTORE_APP != 'all' ]]; then
  327. if [[ $RESTORE_APP != 'passwords' ]]; then
  328. return
  329. fi
  330. fi
  331. if [ -d $USB_MOUNT/backup/passwordstore ]; then
  332. store_original_mariadb_password
  333. echo $"Restoring password store"
  334. #restore_directory_from_usb / passwordstore
  335. restore_directory_from_usb /root/.passwords passwordstore
  336. fi
  337. }
  338. function restore_tor {
  339. if [[ $RESTORE_APP != 'all' ]]; then
  340. if [[ $RESTORE_APP != 'tor' ]]; then
  341. return
  342. fi
  343. fi
  344. if [ -d $USB_MOUNT/backup/tor ]; then
  345. echo $"Restoring Tor settings"
  346. #restore_directory_from_usb / tor
  347. restore_directory_from_usb /var/lib/tor tor
  348. fi
  349. }
  350. function restore_mutt_settings {
  351. if [[ $RESTORE_APP != 'all' ]]; then
  352. if [[ $RESTORE_APP != 'mutt' ]]; then
  353. return
  354. fi
  355. fi
  356. if [ -d $USB_MOUNT/backup/mutt ]; then
  357. for d in $USB_MOUNT/backup/mutt/*/ ; do
  358. USERNAME=$(echo "$d" | awk -F '/' '{print $6}')
  359. # skip over configurations
  360. if [[ "$USERNAME" == *'configs' ]]; then
  361. continue
  362. fi
  363. if [[ $(is_valid_user "$USERNAME") == "1" ]]; then
  364. if [ ! -d "/home/$USERNAME" ]; then
  365. "${PROJECT_NAME}-adduser" "$USERNAME"
  366. fi
  367. echo $"Restoring Mutt configurations for $USERNAME"
  368. restore_directory_from_usb "/home/$USERNAME/.mutt" "mutt/${USERNAME}configs"
  369. echo $"Restoring Mutt settings for $USERNAME"
  370. temp_restore_dir=/root/tempmutt
  371. restore_directory_from_usb "$temp_restore_dir" "mutt/$USERNAME"
  372. if [ -d "$temp_restore_dir/home/$USERNAME/tempbackup" ]; then
  373. if [ -f "$temp_restore_dir/home/$USERNAME/tempbackup/.muttrc" ]; then
  374. cp -f "$temp_restore_dir/home/$USERNAME/tempbackup/.muttrc" "/home/$USERNAME/.muttrc"
  375. sed -i '/set sidebar_delim/d' "/home/$USERNAME/.muttrc"
  376. sed -i '/set sidebar_sort/d' "/home/$USERNAME/.muttrc"
  377. fi
  378. if [ -f "$temp_restore_dir/home/$USERNAME/tempbackup/Muttrc" ]; then
  379. cp -f "$temp_restore_dir/home/$USERNAME/tempbackup/Muttrc" /etc/Muttrc
  380. sed -i '/set sidebar_delim/d' /etc/Muttrc
  381. sed -i '/set sidebar_sort/d' /etc/Muttrc
  382. fi
  383. else
  384. if [ -f $temp_restore_dir/.muttrc ]; then
  385. cp -f "$temp_restore_dir/.muttrc" "/home/$USERNAME/.muttrc"
  386. sed -i '/set sidebar_delim/d' "/home/$USERNAME/.muttrc"
  387. sed -i '/set sidebar_sort/d' "/home/$USERNAME/.muttrc"
  388. fi
  389. if [ -f $temp_restore_dir/Muttrc ]; then
  390. cp -f $temp_restore_dir/Muttrc /etc/Muttrc
  391. sed -i '/set sidebar_delim/d' /etc/Muttrc
  392. sed -i '/set sidebar_sort/d' /etc/Muttrc
  393. fi
  394. fi
  395. # shellcheck disable=SC2181
  396. if [ ! "$?" = "0" ]; then
  397. rm -rf $temp_restore_dir
  398. set_user_permissions
  399. backup_unmount_drive
  400. exit 276
  401. fi
  402. rm -rf $temp_restore_dir
  403. fi
  404. done
  405. fi
  406. }
  407. function restore_gpg {
  408. if [[ $RESTORE_APP != 'gpg' ]]; then
  409. return
  410. fi
  411. if [ -d $USB_MOUNT/backup/gnupg ]; then
  412. for d in $USB_MOUNT/backup/gnupg/*/ ; do
  413. USERNAME=$(echo "$d" | awk -F '/' '{print $6}')
  414. if [[ $(is_valid_user "$USERNAME") == "1" ]]; then
  415. if [ ! -d "/home/$USERNAME" ]; then
  416. "${PROJECT_NAME}-adduser" "$USERNAME"
  417. fi
  418. echo $"Restoring gnupg settings for $USERNAME"
  419. temp_restore_dir=/root/tempgnupg
  420. restore_directory_from_usb $temp_restore_dir "gnupg/$USERNAME"
  421. if [ -d "$temp_restore_dir/home/$USERNAME/.gnupg" ]; then
  422. cp -r "$temp_restore_dir/home/$USERNAME/.gnupg" "/home/$USERNAME/"
  423. else
  424. if [ ! -d "/home/$USERNAME/.gnupg" ]; then
  425. mkdir "/home/$USERNAME/.gnupg"
  426. fi
  427. cp -r $temp_restore_dir/* "/home/$USERNAME/.gnupg/"
  428. fi
  429. # shellcheck disable=SC2181
  430. if [ ! "$?" = "0" ]; then
  431. rm -rf $temp_restore_dir
  432. set_user_permissions
  433. backup_unmount_drive
  434. exit 276
  435. fi
  436. rm -rf $temp_restore_dir
  437. gpg_set_permissions "$USERNAME"
  438. if [[ "$USERNAME" == "$ADMIN_USERNAME" ]]; then
  439. if ! cp -r "/home/$USERNAME/.gnupg" /root; then
  440. set_user_permissions
  441. backup_unmount_drive
  442. exit 283
  443. fi
  444. gpg_set_permissions root
  445. fi
  446. fi
  447. done
  448. fi
  449. }
  450. function restore_procmail {
  451. if [[ $RESTORE_APP != 'all' ]]; then
  452. if [[ $RESTORE_APP != 'procmail' ]]; then
  453. return
  454. fi
  455. fi
  456. if [ -d $USB_MOUNT/backup/procmail ]; then
  457. for d in $USB_MOUNT/backup/procmail/*/ ; do
  458. USERNAME=$(echo "$d" | awk -F '/' '{print $6}')
  459. if [[ $(is_valid_user "$USERNAME") == "1" ]]; then
  460. if [ ! -d "/home/$USERNAME" ]; then
  461. ${PROJECT_NAME}-adduser "$USERNAME"
  462. fi
  463. echo $"Restoring procmail settings for $USERNAME"
  464. temp_restore_dir=/root/tempprocmail
  465. restore_directory_from_usb $temp_restore_dir "procmail/$USERNAME"
  466. if [ -d $temp_restore_dir ]; then
  467. if [ -d "$temp_restore_dir/home/$USERNAME/tempbackup" ]; then
  468. cp -f "$temp_restore_dir/home/$USERNAME/tempbackup/.procmailrc" "/home/$USERNAME/"
  469. else
  470. cp -f "$temp_restore_dir/.procmailrc" "/home/$USERNAME/.procmailrc"
  471. fi
  472. # shellcheck disable=SC2181
  473. if [ ! "$?" = "0" ]; then
  474. rm -rf $temp_restore_dir
  475. set_user_permissions
  476. backup_unmount_drive
  477. exit 276
  478. fi
  479. rm -rf $temp_restore_dir
  480. fi
  481. fi
  482. done
  483. fi
  484. }
  485. function restore_spamassassin {
  486. if [[ $RESTORE_APP != 'all' ]]; then
  487. if [[ $RESTORE_APP != 'spamassassin' ]]; then
  488. return
  489. fi
  490. fi
  491. if [ -d $USB_MOUNT/backup/spamassassin ]; then
  492. for d in $USB_MOUNT/backup/spamassassin/*/ ; do
  493. USERNAME=$(echo "$d" | awk -F '/' '{print $6}')
  494. if [[ $(is_valid_user "$USERNAME") == "1" ]]; then
  495. if [ -d "$USB_MOUNT/backup/spamassassin/$USERNAME" ]; then
  496. if [ ! -d "/home/$USERNAME" ]; then
  497. ${PROJECT_NAME}-adduser "$USERNAME"
  498. fi
  499. echo $"Restoring spamassassin settings for $USERNAME"
  500. temp_restore_dir=/root/tempspamassassin
  501. restore_directory_from_usb $temp_restore_dir "spamassassin/$USERNAME"
  502. if [ -d "$temp_restore_dir/home/$USERNAME" ]; then
  503. cp -rf "$temp_restore_dir/home/$USERNAME/.spamassassin" "/home/$USERNAME/"
  504. else
  505. if [ ! -d "/home/$USERNAME/.spamassassin" ]; then
  506. mkdir "/home/$USERNAME/.spamassassin"
  507. fi
  508. cp -rf "$temp_restore_dir/*" "/home/$USERNAME/.spamassassin/"
  509. fi
  510. # shellcheck disable=SC2181
  511. if [ ! "$?" = "0" ]; then
  512. rm -rf $temp_restore_dir
  513. set_user_permissions
  514. backup_unmount_drive
  515. exit 276
  516. fi
  517. rm -rf $temp_restore_dir
  518. fi
  519. fi
  520. done
  521. fi
  522. }
  523. function restore_admin_readme {
  524. if [[ $RESTORE_APP != 'all' ]]; then
  525. if [[ $RESTORE_APP != 'readme' ]]; then
  526. return
  527. fi
  528. fi
  529. if [ -d $USB_MOUNT/backup/readme ]; then
  530. echo $"Restoring admin user README"
  531. # Make a backup of the original README file
  532. # incase old passwords need to be used
  533. if [ -f "/home/$ADMIN_USERNAME/README" ]; then
  534. if [ ! -f "/home/$ADMIN_USERNAME/README_original" ]; then
  535. cp "/home/$ADMIN_USERNAME/README" "/home/$ADMIN_USERNAME/README_original"
  536. fi
  537. fi
  538. temp_restore_dir=/root/tempreadme
  539. restore_directory_from_usb $temp_restore_dir readme
  540. if [ -d "$temp_restore_dir/home/$ADMIN_USERNAME/tempbackup" ]; then
  541. cp -f "$temp_restore_dir/home/$ADMIN_USERNAME/tempbackup/README" "/home/$ADMIN_USERNAME/"
  542. else
  543. cp -f "$temp_restore_dir/README" "/home/$ADMIN_USERNAME/README"
  544. fi
  545. # shellcheck disable=SC2181
  546. if [ ! "$?" = "0" ]; then
  547. rm -rf $temp_restore_dir
  548. set_user_permissions
  549. backup_unmount_drive
  550. exit 276
  551. fi
  552. rm -rf $temp_restore_dir
  553. fi
  554. }
  555. function restore_user_ssh_keys {
  556. if [[ $RESTORE_APP != 'all' ]]; then
  557. if [[ $RESTORE_APP != 'ssh' ]]; then
  558. return
  559. fi
  560. fi
  561. if [ -d $USB_MOUNT/backup/ssh ]; then
  562. for d in $USB_MOUNT/backup/ssh/*/ ; do
  563. USERNAME=$(echo "$d" | awk -F '/' '{print $6}')
  564. if [[ $(is_valid_user "$USERNAME") == "1" ]]; then
  565. if [ ! -d "/home/$USERNAME" ]; then
  566. ${PROJECT_NAME}-adduser "$USERNAME"
  567. fi
  568. echo $"Restoring ssh keys for $USERNAME"
  569. temp_restore_dir=/root/tempssh
  570. restore_directory_from_usb $temp_restore_dir "ssh/$USERNAME"
  571. if [ -d "$temp_restore_dir/home/$USERNAME/.ssh" ]; then
  572. cp -r "$temp_restore_dir/home/$USERNAME/.ssh" "/home/$USERNAME/"
  573. else
  574. if [ ! -d "/home/$USERNAME/.ssh" ]; then
  575. mkdir "/home/$USERNAME/.ssh"
  576. fi
  577. cp -r "$temp_restore_dir/*" "/home/$USERNAME/.ssh/"
  578. fi
  579. # shellcheck disable=SC2181
  580. if [ ! "$?" = "0" ]; then
  581. rm -rf $temp_restore_dir
  582. set_user_permissions
  583. backup_unmount_drive
  584. exit 664
  585. fi
  586. rm -rf $temp_restore_dir
  587. fi
  588. done
  589. fi
  590. }
  591. function restore_user_config {
  592. if [[ $RESTORE_APP != 'all' ]]; then
  593. if [[ $RESTORE_APP != 'userconfig' ]]; then
  594. return
  595. fi
  596. fi
  597. if [ -d $USB_MOUNT/backup/config ]; then
  598. for d in $USB_MOUNT/backup/config/*/ ; do
  599. USERNAME=$(echo "$d" | awk -F '/' '{print $6}')
  600. if [[ $(is_valid_user "$USERNAME") == "1" ]]; then
  601. if [ ! -d "/home/$USERNAME" ]; then
  602. ${PROJECT_NAME}-adduser "$USERNAME"
  603. fi
  604. echo $"Restoring config files for $USERNAME"
  605. temp_restore_dir=/root/tempconfig
  606. restore_directory_from_usb $temp_restore_dir "config/$USERNAME"
  607. if [ -d "$temp_restore_dir/home/$USERNAME/.config" ]; then
  608. cp -r "$temp_restore_dir/home/$USERNAME/.config" "/home/$USERNAME/"
  609. else
  610. if [ ! -d "/home/$USERNAME/.config" ]; then
  611. mkdir "/home/$USERNAME/.config"
  612. fi
  613. cp -r $temp_restore_dir/* "/home/$USERNAME/.config/"
  614. fi
  615. # shellcheck disable=SC2181
  616. if [ ! "$?" = "0" ]; then
  617. rm -rf $temp_restore_dir
  618. set_user_permissions
  619. backup_unmount_drive
  620. exit 664
  621. fi
  622. rm -rf $temp_restore_dir
  623. fi
  624. done
  625. fi
  626. }
  627. function restore_user_monkeysphere {
  628. if [[ $RESTORE_APP != 'all' ]]; then
  629. if [[ $RESTORE_APP != 'usermonkeysphere' ]]; then
  630. return
  631. fi
  632. fi
  633. if [ -d $USB_MOUNT/backup/monkeysphere ]; then
  634. for d in $USB_MOUNT/backup/monkeysphere/*/ ; do
  635. USERNAME=$(echo "$d" | awk -F '/' '{print $6}')
  636. if [[ $(is_valid_user "$USERNAME") == "1" ]]; then
  637. if [ ! -d "/home/$USERNAME" ]; then
  638. ${PROJECT_NAME}-adduser "$USERNAME"
  639. fi
  640. echo $"Restoring monkeysphere ids for $USERNAME"
  641. temp_restore_dir=/root/tempmonkeysphere
  642. restore_directory_from_usb $temp_restore_dir "monkeysphere/$USERNAME"
  643. if [ -d "$temp_restore_dir/home/$USERNAME/.monkeysphere" ]; then
  644. cp -r "$temp_restore_dir/home/$USERNAME/.monkeysphere" "/home/$USERNAME/"
  645. else
  646. if [ ! -d "/home/$USERNAME/.monkeysphere" ]; then
  647. mkdir "/home/$USERNAME/.monkeysphere"
  648. fi
  649. cp -r $temp_restore_dir/* "/home/$USERNAME/.monkeysphere"
  650. fi
  651. # shellcheck disable=SC2181
  652. if [ ! "$?" = "0" ]; then
  653. rm -rf $temp_restore_dir
  654. set_user_permissions
  655. backup_unmount_drive
  656. exit 664
  657. fi
  658. rm -rf $temp_restore_dir
  659. fi
  660. done
  661. # The admin user is the identity certifier
  662. MY_EMAIL_ADDRESS="${ADMIN_USERNAME}@${HOSTNAME}"
  663. read_config_param MY_EMAIL_ADDRESS
  664. MY_GPG_PUBLIC_KEY_ID=$(gpg_pubkey_from_email "$ADMIN_USERNAME" "$MY_EMAIL_ADDRESS")
  665. fpr=$(gpg --with-colons --fingerprint "$MY_GPG_PUBLIC_KEY_ID" | grep fpr | head -n 1 | awk -F ':' '{print $10}')
  666. monkeysphere-authentication add-identity-certifier "$fpr"
  667. monkeysphere-authentication update-users
  668. fi
  669. }
  670. function restore_user_fin {
  671. if [[ $RESTORE_APP != 'all' ]]; then
  672. if [[ $RESTORE_APP != 'userfin' ]]; then
  673. return
  674. fi
  675. fi
  676. if [ -d $USB_MOUNT/backup/fin ]; then
  677. for d in $USB_MOUNT/backup/fin/*/ ; do
  678. USERNAME=$(echo "$d" | awk -F '/' '{print $6}')
  679. if [[ $(is_valid_user "$USERNAME") == "1" ]]; then
  680. if [ ! -d "/home/$USERNAME" ]; then
  681. ${PROJECT_NAME}-adduser "$USERNAME"
  682. fi
  683. echo $"Restoring fin files for $USERNAME"
  684. temp_restore_dir=/root/tempfin
  685. restore_directory_from_usb $temp_restore_dir "fin/$USERNAME"
  686. if [ -d "$temp_restore_dir/home/$USERNAME/.fin" ]; then
  687. cp -r "$temp_restore_dir/home/$USERNAME/.fin" "/home/$USERNAME/"
  688. else
  689. if [ ! -d "/home/$USERNAME/.fin" ]; then
  690. mkdir "/home/$USERNAME/.fin"
  691. fi
  692. cp -r "$temp_restore_dir/*" "/home/$USERNAME/.fin/"
  693. fi
  694. # shellcheck disable=SC2181
  695. if [ ! "$?" = "0" ]; then
  696. rm -rf $temp_restore_dir
  697. set_user_permissions
  698. backup_unmount_drive
  699. exit 664
  700. fi
  701. rm -rf $temp_restore_dir
  702. fi
  703. done
  704. fi
  705. }
  706. function restore_user_local {
  707. if [[ $RESTORE_APP != 'all' ]]; then
  708. if [[ $RESTORE_APP != 'userlocal' ]]; then
  709. return
  710. fi
  711. fi
  712. if [ -d $USB_MOUNT/backup/local ]; then
  713. for d in $USB_MOUNT/backup/local/*/ ; do
  714. USERNAME=$(echo "$d" | awk -F '/' '{print $6}')
  715. if [[ $(is_valid_user "$USERNAME") == "1" ]]; then
  716. if [ ! -d "/home/$USERNAME" ]; then
  717. ${PROJECT_NAME}-adduser "$USERNAME"
  718. fi
  719. echo $"Restoring local files for $USERNAME"
  720. temp_restore_dir=/root/templocal
  721. restore_directory_from_usb $temp_restore_dir "local/$USERNAME"
  722. if [ -d "$temp_restore_dir/home/$USERNAME/.local" ]; then
  723. cp -r "$temp_restore_dir/home/$USERNAME/.local" "/home/$USERNAME/"
  724. else
  725. if [ ! -d "/home/$USERNAME/.local" ]; then
  726. mkdir "/home/$USERNAME/.local"
  727. fi
  728. cp -r $temp_restore_dir/* "/home/$USERNAME/.local/"
  729. fi
  730. # shellcheck disable=SC2181
  731. if [ ! "$?" = "0" ]; then
  732. rm -rf $temp_restore_dir
  733. set_user_permissions
  734. backup_unmount_drive
  735. exit 664
  736. fi
  737. rm -rf $temp_restore_dir
  738. fi
  739. done
  740. fi
  741. }
  742. function restore_certs {
  743. if [[ $RESTORE_APP != 'all' ]]; then
  744. if [[ $RESTORE_APP != 'certs' ]]; then
  745. return
  746. fi
  747. fi
  748. if [ -d $USB_MOUNT/backup/ssl ]; then
  749. echo $"Restoring certificates"
  750. mkdir /root/tempssl
  751. restore_directory_from_usb /root/tempssl ssl
  752. if [ -d /root/tempssl/etc/ssl ]; then
  753. cp -r /root/tempssl/etc/ssl/* /etc/ssl
  754. else
  755. cp -r /root/tempssl/* /etc/ssl/
  756. fi
  757. # shellcheck disable=SC2181
  758. if [ ! "$?" = "0" ]; then
  759. set_user_permissions
  760. backup_unmount_drive
  761. exit 276
  762. fi
  763. rm -rf /root/tempssl
  764. update-ca-certificates
  765. # restore ownership
  766. if [ -f /etc/ssl/private/xmpp.key ]; then
  767. chown prosody:prosody /etc/ssl/private/xmpp.key
  768. chown prosody:prosody /etc/ssl/certs/xmpp.*
  769. fi
  770. if [ -d /etc/dovecot ]; then
  771. chown root:dovecot /etc/ssl/private/dovecot.*
  772. chown root:dovecot /etc/ssl/certs/dovecot.*
  773. fi
  774. if [ -f /etc/ssl/private/exim.key ]; then
  775. cp /etc/ssl/private/exim.key /etc/exim4
  776. cp /etc/ssl/certs/exim.crt /etc/exim4
  777. cp /etc/ssl/certs/exim.dhparam /etc/exim4
  778. chown root:Debian-exim /etc/exim4/exim.key /etc/exim4/exim.crt /etc/exim4/exim.dhparam
  779. chmod 640 /etc/exim4/exim.key /etc/exim4/exim.crt /etc/exim4/exim.dhparam
  780. fi
  781. if [ -f /etc/ssl/private/mumble.key ]; then
  782. if [ -d /var/lib/mumble-server ]; then
  783. cp /etc/ssl/certs/mumble.* /var/lib/mumble-server
  784. cp /etc/ssl/private/mumble.key /var/lib/mumble-server
  785. chown -R mumble-server:mumble-server /var/lib/mumble-server
  786. fi
  787. fi
  788. fi
  789. }
  790. function restore_personal_settings {
  791. if [[ $RESTORE_APP != 'all' ]]; then
  792. if [[ $RESTORE_APP != 'personal' ]]; then
  793. return
  794. fi
  795. fi
  796. if [ -d $USB_MOUNT/backup/personal ]; then
  797. for d in $USB_MOUNT/backup/personal/*/ ; do
  798. USERNAME=$(echo "$d" | awk -F '/' '{print $6}')
  799. if [[ $(is_valid_user "$USERNAME") == "1" ]]; then
  800. if [ -d "$USB_MOUNT/backup/personal/$USERNAME" ]; then
  801. if [ ! -d "/home/$USERNAME" ]; then
  802. ${PROJECT_NAME}-adduser "$USERNAME"
  803. fi
  804. echo $"Restoring personal settings for $USERNAME"
  805. temp_restore_dir=/root/temppersonal
  806. restore_directory_from_usb $temp_restore_dir "personal/$USERNAME"
  807. if [ -d "/home/$USERNAME/personal" ]; then
  808. rm -rf "/home/$USERNAME/personal"
  809. fi
  810. if [ -d "$temp_restore_dir/home/$USERNAME/personal" ]; then
  811. mv "$temp_restore_dir/home/$USERNAME/personal" "/home/$USERNAME"
  812. else
  813. if [ ! -d "/home/$USERNAME/personal" ]; then
  814. mkdir "/home/$USERNAME/personal"
  815. fi
  816. cp -r $temp_restore_dir/* "/home/$USERNAME/personal/"
  817. fi
  818. # shellcheck disable=SC2181
  819. if [ ! "$?" = "0" ]; then
  820. set_user_permissions
  821. backup_unmount_drive
  822. exit 184
  823. fi
  824. rm -rf $temp_restore_dir
  825. fi
  826. fi
  827. done
  828. fi
  829. }
  830. function restore_mailing_list {
  831. if [[ $RESTORE_APP != 'all' ]]; then
  832. if [[ $RESTORE_APP != 'mailinglist' ]]; then
  833. return
  834. fi
  835. fi
  836. if [ -d /var/spool/mlmmj ]; then
  837. echo $"Restoring public mailing list"
  838. temp_restore_dir=/root/tempmailinglist
  839. restore_directory_from_usb $temp_restore_dir mailinglist
  840. if [ -d $temp_restore_dir/root/spool/mlmmj ]; then
  841. cp -r $temp_restore_dir/root/spool/mlmmj/* /var/spool/mlmmj
  842. else
  843. cp -r $temp_restore_dir/* /var/spool/mlmmj/
  844. fi
  845. # shellcheck disable=SC2181
  846. if [ ! "$?" = "0" ]; then
  847. set_user_permissions
  848. backup_unmount_drive
  849. exit 526
  850. fi
  851. rm -rf $temp_restore_dir
  852. fi
  853. }
  854. function restore_email {
  855. if [[ $RESTORE_APP != 'all' ]]; then
  856. if [[ $RESTORE_APP != 'email' ]]; then
  857. return
  858. fi
  859. fi
  860. if [ -d $USB_MOUNT/backup/mail ]; then
  861. for d in $USB_MOUNT/backup/mail/*/ ; do
  862. USERNAME=$(echo "$d" | awk -F '/' '{print $6}')
  863. if [[ $(is_valid_user "$USERNAME") == "1" ]]; then
  864. if [ ! -d "/home/$USERNAME" ]; then
  865. ${PROJECT_NAME}-adduser "$USERNAME"
  866. fi
  867. echo $"Restoring emails for $USERNAME"
  868. temp_restore_dir=/root/tempmail
  869. restore_directory_from_usb $temp_restore_dir "mail/$USERNAME"
  870. if [ ! -d "/home/$USERNAME/Maildir" ]; then
  871. mkdir "/home/$USERNAME/Maildir"
  872. fi
  873. if [ -d "$temp_restore_dir/root/tempbackupemail/$USERNAME" ]; then
  874. tar -xzvf "$temp_restore_dir/root/tempbackupemail/$USERNAME/maildir.tar.gz" -C /
  875. else
  876. tar -xzvf $temp_restore_dir/maildir.tar.gz -C /
  877. fi
  878. # shellcheck disable=SC2181
  879. if [ ! "$?" = "0" ]; then
  880. set_user_permissions
  881. backup_unmount_drive
  882. exit 9276382
  883. fi
  884. rm -rf $temp_restore_dir
  885. fi
  886. done
  887. fi
  888. }
  889. function get_restore_app {
  890. if [ "${1}" ]; then
  891. if [ ! -d "/home/${1}" ]; then
  892. RESTORE_APP="${1}"
  893. echo $"Restore $RESTORE_APP"
  894. fi
  895. fi
  896. }
  897. get_restore_app "${2}"
  898. backup_mount_drive "${1}" "${ADMIN_USERNAME}" "${2}"
  899. check_backup_exists
  900. check_admin_user
  901. copy_gpg_keys
  902. gpg_agent_setup root
  903. restore_blocklist
  904. restore_configfiles
  905. same_admin_user
  906. restore_passwordstore
  907. restore_mariadb
  908. restore_postgresql
  909. restore_letsencrypt
  910. restore_tor
  911. restore_mutt_settings
  912. restore_gpg
  913. restore_procmail
  914. restore_spamassassin
  915. restore_admin_readme
  916. restore_user_ssh_keys
  917. restore_user_config
  918. restore_user_monkeysphere
  919. restore_user_fin
  920. restore_user_local
  921. restore_certs
  922. restore_personal_settings
  923. restore_mailing_list
  924. restore_email
  925. restore_apps local "$RESTORE_APP"
  926. set_user_permissions
  927. update_default_domain
  928. backup_unmount_drive
  929. # ensure that all TLS certificates are pinned
  930. #${PROJECT_NAME}-pin-cert all
  931. echo $"Restore from USB drive is complete. You can now unplug it."
  932. exit 0