freedombone-restore-local 35KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983
  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. cp -f $temp_restore_dir$MONGODB_APPS_FILE $MONGODB_APPS_FILE
  160. if [ ! "$?" = "0" ]; then
  161. set_user_permissions
  162. backup_unmount_drive
  163. rm -rf $temp_restore_dir
  164. exit 859034853
  165. fi
  166. fi
  167. #if [ -f $CONFIGURATION_FILE ]; then
  168. # # install according to the config file
  169. # freedombone -c $CONFIGURATION_FILE
  170. #fi
  171. #if [ -f $temp_restore_dir/root/${PROJECT_NAME}-completed.txt ]; then
  172. # cp -f $temp_restore_dir/root/${PROJECT_NAME}-completed.txt $COMPLETION_FILE
  173. # if [ ! "$?" = "0" ]; then
  174. # set_user_permissions
  175. # backup_unmount_drive
  176. # rm -rf $temp_restore_dir
  177. # exit 6382
  178. # fi
  179. #fi
  180. if [ -f ${temp_restore_dir}${BACKUP_EXTRA_DIRECTORIES} ]; then
  181. cp -f ${temp_restore_dir}${BACKUP_EXTRA_DIRECTORIES} ${BACKUP_EXTRA_DIRECTORIES}
  182. if [ ! "$?" = "0" ]; then
  183. set_user_permissions
  184. backup_unmount_drive
  185. rm -rf $temp_restore_dir
  186. exit 62121
  187. fi
  188. fi
  189. # restore nginx password hashes
  190. if [ -d $temp_restore_dir/root ]; then
  191. if [ -f $temp_restore_dir/root/htpasswd ]; then
  192. cp -f $temp_restore_dir/root/htpasswd /etc/nginx/.htpasswd
  193. fi
  194. else
  195. if [ -f $temp_restore_dir/htpasswd ]; then
  196. cp -f $temp_restore_dir/htpasswd /etc/nginx/.htpasswd
  197. fi
  198. fi
  199. rm -rf $temp_restore_dir
  200. fi
  201. }
  202. function same_admin_user {
  203. PREV_ADMIN_USERNAME=$(get_completion_param "Admin user")
  204. if [[ "$PREV_ADMIN_USERNAME" != "$ADMIN_USERNAME" ]]; then
  205. 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"
  206. set_user_permissions
  207. backup_unmount_drive
  208. exit 73265
  209. fi
  210. }
  211. function restore_mariadb {
  212. if [[ $RESTORE_APP != 'all' ]]; then
  213. if [[ $RESTORE_APP != 'mariadb' ]]; then
  214. return
  215. fi
  216. fi
  217. if [[ $(is_completed install_mariadb) == "0" ]]; then
  218. function_check install_mariadb
  219. install_mariadb
  220. fi
  221. if [ -d $USB_MOUNT/backup/mariadb ]; then
  222. echo $"Restoring mysql settings"
  223. keep_database_running
  224. temp_restore_dir=/root/tempmariadb
  225. restore_directory_from_usb $temp_restore_dir mariadb
  226. store_original_mariadb_password
  227. echo $'Obtaining original MariaDB password'
  228. db_pass=$(cat /root/.mariadboriginal)
  229. if [ ${#db_pass} -gt 0 ]; then
  230. echo $"Restore the MariaDB user table"
  231. if [ -d ${temp_restore_dir}${temp_restore_dir} ]; then
  232. mysqlsuccess=$(mysql -u root --password="$db_pass" mysql -o < ${temp_restore_dir}${temp_restore_dir}/mysql.sql)
  233. else
  234. mysqlsuccess=$(mysql -u root --password="$db_pass" mysql -o < ${temp_restore_dir}/mysql.sql)
  235. fi
  236. if [ ! "$?" = "0" ]; then
  237. echo $"Try again using the password obtained from backup"
  238. db_pass=$(${PROJECT_NAME}-pass -u root -a mariadb)
  239. if [ -d ${temp_restore_dir}${temp_restore_dir} ]; then
  240. mysqlsuccess=$(mysql -u root --password="$db_pass" mysql -o < ${temp_restore_dir}${temp_restore_dir}/mysql.sql)
  241. else
  242. mysqlsuccess=$(mysql -u root --password="$db_pass" mysql -o < ${temp_restore_dir}/mysql.sql)
  243. fi
  244. fi
  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. if [ ! "$?" = "0" ]; then
  286. echo $"Try again using the password obtained from backup"
  287. db_pass=$(${PROJECT_NAME}-pass -u root -a postgresql)
  288. if [ -d ${temp_restore_dir}${temp_restore_dir} ]; then
  289. mysqlsuccess=$(sudo -u postgres pg_restore ${temp_restore_dir}${temp_restore_dir}/postgresql.sql)
  290. else
  291. mysqlsuccess=$(sudo -u postgres pg_restore ${temp_restore_dir}/postgresql.sql)
  292. fi
  293. fi
  294. if [ ! "$?" = "0" ]; then
  295. echo "$mysqlsuccess"
  296. set_user_permissions
  297. backup_unmount_drive
  298. exit 73825
  299. fi
  300. echo $"Restarting database"
  301. systemctl restart postgresql
  302. echo $"Ensure postgresql handles authentication"
  303. POSTGRESQL_PASSWORD=$(${PROJECT_NAME}-pass -u root -a postgresql)
  304. DATABASE_PASSWORD=$(${PROJECT_NAME}-pass -u root -a postgresql)
  305. fi
  306. rm -rf $temp_restore_dir
  307. fi
  308. }
  309. function restore_letsencrypt {
  310. if [[ $RESTORE_APP != 'all' ]]; then
  311. if [[ $RESTORE_APP != 'letsencrypt' ]]; then
  312. return
  313. fi
  314. fi
  315. if [ -d $USB_MOUNT/backup/letsencrypt ]; then
  316. echo $"Restoring Lets Encrypt settings"
  317. #restore_directory_from_usb / letsencrypt
  318. restore_directory_from_usb /etc/letsencrypt letsencrypt
  319. chgrp -R ssl-cert /etc/letsencrypt
  320. chmod -R g=rX /etc/letsencrypt
  321. fi
  322. }
  323. function restore_passwordstore {
  324. if [[ $RESTORE_APP != 'all' ]]; then
  325. if [[ $RESTORE_APP != 'passwords' ]]; then
  326. return
  327. fi
  328. fi
  329. if [ -d $USB_MOUNT/backup/passwordstore ]; then
  330. store_original_mariadb_password
  331. echo $"Restoring password store"
  332. #restore_directory_from_usb / passwordstore
  333. restore_directory_from_usb /root/.passwords passwordstore
  334. fi
  335. }
  336. function restore_tor {
  337. if [[ $RESTORE_APP != 'all' ]]; then
  338. if [[ $RESTORE_APP != 'tor' ]]; then
  339. return
  340. fi
  341. fi
  342. if [ -d $USB_MOUNT/backup/tor ]; then
  343. echo $"Restoring Tor settings"
  344. #restore_directory_from_usb / tor
  345. restore_directory_from_usb /var/lib/tor tor
  346. fi
  347. }
  348. function restore_mutt_settings {
  349. if [[ $RESTORE_APP != 'all' ]]; then
  350. if [[ $RESTORE_APP != 'mutt' ]]; then
  351. return
  352. fi
  353. fi
  354. if [ -d $USB_MOUNT/backup/mutt ]; then
  355. for d in $USB_MOUNT/backup/mutt/*/ ; do
  356. USERNAME=$(echo "$d" | awk -F '/' '{print $6}')
  357. if [[ $(is_valid_user "$USERNAME") == "1" ]]; then
  358. if [ ! -d /home/$USERNAME ]; then
  359. ${PROJECT_NAME}-adduser $USERNAME
  360. fi
  361. echo $"Restoring Mutt settings for $USERNAME"
  362. temp_restore_dir=/root/tempmutt
  363. restore_directory_from_usb $temp_restore_dir mutt/$USERNAME
  364. if [ -d $temp_restore_dir/home/$USERNAME/tempbackup ]; then
  365. if [ -f $temp_restore_dir/home/$USERNAME/tempbackup/.muttrc ]; then
  366. cp -f $temp_restore_dir/home/$USERNAME/tempbackup/.muttrc /home/$USERNAME/.muttrc
  367. sed -i '/set sidebar_delim/d' /home/$USERNAME/.muttrc
  368. sed -i '/set sidebar_sort/d' /home/$USERNAME/.muttrc
  369. fi
  370. if [ -f $temp_restore_dir/home/$USERNAME/tempbackup/Muttrc ]; then
  371. cp -f $temp_restore_dir/home/$USERNAME/tempbackup/Muttrc /etc/Muttrc
  372. sed -i '/set sidebar_delim/d' /etc/Muttrc
  373. sed -i '/set sidebar_sort/d' /etc/Muttrc
  374. fi
  375. else
  376. if [ -f $temp_restore_dir/.muttrc ]; then
  377. cp -f $temp_restore_dir/.muttrc /home/$USERNAME/.muttrc
  378. sed -i '/set sidebar_delim/d' /home/$USERNAME/.muttrc
  379. sed -i '/set sidebar_sort/d' /home/$USERNAME/.muttrc
  380. fi
  381. if [ -f $temp_restore_dir/Muttrc ]; then
  382. cp -f $temp_restore_dir/Muttrc /etc/Muttrc
  383. sed -i '/set sidebar_delim/d' /etc/Muttrc
  384. sed -i '/set sidebar_sort/d' /etc/Muttrc
  385. fi
  386. fi
  387. if [ ! "$?" = "0" ]; then
  388. rm -rf $temp_restore_dir
  389. set_user_permissions
  390. backup_unmount_drive
  391. exit 276
  392. fi
  393. rm -rf $temp_restore_dir
  394. fi
  395. done
  396. fi
  397. }
  398. function restore_gpg {
  399. if [[ $RESTORE_APP != 'gpg' ]]; then
  400. return
  401. fi
  402. if [ -d $USB_MOUNT/backup/gnupg ]; then
  403. for d in $USB_MOUNT/backup/gnupg/*/ ; do
  404. USERNAME=$(echo "$d" | awk -F '/' '{print $6}')
  405. if [[ $(is_valid_user "$USERNAME") == "1" ]]; then
  406. if [ ! -d /home/$USERNAME ]; then
  407. ${PROJECT_NAME}-adduser $USERNAME
  408. fi
  409. echo $"Restoring gnupg settings for $USERNAME"
  410. temp_restore_dir=/root/tempgnupg
  411. restore_directory_from_usb $temp_restore_dir gnupg/$USERNAME
  412. if [ -d $temp_restore_dir/home/$USERNAME/.gnupg ]; then
  413. cp -r $temp_restore_dir/home/$USERNAME/.gnupg /home/$USERNAME/
  414. else
  415. if [ ! -d /home/$USERNAME/.gnupg ]; then
  416. mkdir /home/$USERNAME/.gnupg
  417. fi
  418. cp -r $temp_restore_dir/* /home/$USERNAME/.gnupg/
  419. fi
  420. if [ ! "$?" = "0" ]; then
  421. rm -rf $temp_restore_dir
  422. set_user_permissions
  423. backup_unmount_drive
  424. exit 276
  425. fi
  426. rm -rf $temp_restore_dir
  427. gpg_set_permissions $USERNAME
  428. if [[ "$USERNAME" == "$ADMIN_USERNAME" ]]; then
  429. cp -r /home/$USERNAME/.gnupg /root
  430. if [ ! "$?" = "0" ]; then
  431. set_user_permissions
  432. backup_unmount_drive
  433. exit 283
  434. fi
  435. gpg_set_permissions root
  436. fi
  437. fi
  438. done
  439. fi
  440. }
  441. function restore_procmail {
  442. if [[ $RESTORE_APP != 'all' ]]; then
  443. if [[ $RESTORE_APP != 'procmail' ]]; then
  444. return
  445. fi
  446. fi
  447. if [ -d $USB_MOUNT/backup/procmail ]; then
  448. for d in $USB_MOUNT/backup/procmail/*/ ; do
  449. USERNAME=$(echo "$d" | awk -F '/' '{print $6}')
  450. if [[ $(is_valid_user "$USERNAME") == "1" ]]; then
  451. if [ ! -d /home/$USERNAME ]; then
  452. ${PROJECT_NAME}-adduser $USERNAME
  453. fi
  454. echo $"Restoring procmail settings for $USERNAME"
  455. temp_restore_dir=/root/tempprocmail
  456. restore_directory_from_usb $temp_restore_dir procmail/$USERNAME
  457. if [ -d $temp_restore_dir ]; then
  458. if [ -d $temp_restore_dir/home/$USERNAME/tempbackup ]; then
  459. cp -f $temp_restore_dir/home/$USERNAME/tempbackup/.procmailrc /home/$USERNAME/
  460. else
  461. cp -f $temp_restore_dir/.procmailrc /home/$USERNAME/.procmailrc
  462. fi
  463. if [ ! "$?" = "0" ]; then
  464. rm -rf $temp_restore_dir
  465. set_user_permissions
  466. backup_unmount_drive
  467. exit 276
  468. fi
  469. rm -rf $temp_restore_dir
  470. fi
  471. fi
  472. done
  473. fi
  474. }
  475. function restore_spamassassin {
  476. if [[ $RESTORE_APP != 'all' ]]; then
  477. if [[ $RESTORE_APP != 'spamassassin' ]]; then
  478. return
  479. fi
  480. fi
  481. if [ -d $USB_MOUNT/backup/spamassassin ]; then
  482. for d in $USB_MOUNT/backup/spamassassin/*/ ; do
  483. USERNAME=$(echo "$d" | awk -F '/' '{print $6}')
  484. if [[ $(is_valid_user "$USERNAME") == "1" ]]; then
  485. if [ -d $USB_MOUNT/backup/spamassassin/$USERNAME ]; then
  486. if [ ! -d /home/$USERNAME ]; then
  487. ${PROJECT_NAME}-adduser $USERNAME
  488. fi
  489. echo $"Restoring spamassassin settings for $USERNAME"
  490. temp_restore_dir=/root/tempspamassassin
  491. restore_directory_from_usb $temp_restore_dir spamassassin/$USERNAME
  492. if [ -d $temp_restore_dir/home/$USERNAME ]; then
  493. cp -rf $temp_restore_dir/home/$USERNAME/.spamassassin /home/$USERNAME/
  494. else
  495. if [ ! -d /home/$USERNAME/.spamassassin ]; then
  496. mkdir /home/$USERNAME/.spamassassin
  497. fi
  498. cp -rf $temp_restore_dir/* /home/$USERNAME/.spamassassin/
  499. fi
  500. if [ ! "$?" = "0" ]; then
  501. rm -rf $temp_restore_dir
  502. set_user_permissions
  503. backup_unmount_drive
  504. exit 276
  505. fi
  506. rm -rf $temp_restore_dir
  507. fi
  508. fi
  509. done
  510. fi
  511. }
  512. function restore_admin_readme {
  513. if [[ $RESTORE_APP != 'all' ]]; then
  514. if [[ $RESTORE_APP != 'readme' ]]; then
  515. return
  516. fi
  517. fi
  518. if [ -d $USB_MOUNT/backup/readme ]; then
  519. echo $"Restoring admin user README"
  520. # Make a backup of the original README file
  521. # incase old passwords need to be used
  522. if [ -f /home/$ADMIN_USERNAME/README ]; then
  523. if [ ! -f /home/$ADMIN_USERNAME/README_original ]; then
  524. cp /home/$ADMIN_USERNAME/README /home/$ADMIN_USERNAME/README_original
  525. fi
  526. fi
  527. temp_restore_dir=/root/tempreadme
  528. restore_directory_from_usb $temp_restore_dir readme
  529. if [ -d $temp_restore_dir/home/$ADMIN_USERNAME/tempbackup ]; then
  530. cp -f $temp_restore_dir/home/$ADMIN_USERNAME/tempbackup/README /home/$ADMIN_USERNAME/
  531. else
  532. cp -f $temp_restore_dir/README /home/$ADMIN_USERNAME/README
  533. fi
  534. if [ ! "$?" = "0" ]; then
  535. rm -rf $temp_restore_dir
  536. set_user_permissions
  537. backup_unmount_drive
  538. exit 276
  539. fi
  540. rm -rf $temp_restore_dir
  541. fi
  542. }
  543. function restore_user_ssh_keys {
  544. if [[ $RESTORE_APP != 'all' ]]; then
  545. if [[ $RESTORE_APP != 'ssh' ]]; then
  546. return
  547. fi
  548. fi
  549. if [ -d $USB_MOUNT/backup/ssh ]; then
  550. for d in $USB_MOUNT/backup/ssh/*/ ; do
  551. USERNAME=$(echo "$d" | awk -F '/' '{print $6}')
  552. if [[ $(is_valid_user "$USERNAME") == "1" ]]; then
  553. if [ ! -d /home/$USERNAME ]; then
  554. ${PROJECT_NAME}-adduser $USERNAME
  555. fi
  556. echo $"Restoring ssh keys for $USERNAME"
  557. temp_restore_dir=/root/tempssh
  558. restore_directory_from_usb $temp_restore_dir ssh/$USERNAME
  559. if [ -d $temp_restore_dir/home/$USERNAME/.ssh ]; then
  560. cp -r $temp_restore_dir/home/$USERNAME/.ssh /home/$USERNAME/
  561. else
  562. if [ ! -d /home/$USERNAME/.ssh ]; then
  563. mkdir /home/$USERNAME/.ssh
  564. fi
  565. cp -r $temp_restore_dir/* /home/$USERNAME/.ssh/
  566. fi
  567. if [ ! "$?" = "0" ]; then
  568. rm -rf $temp_restore_dir
  569. set_user_permissions
  570. backup_unmount_drive
  571. exit 664
  572. fi
  573. rm -rf $temp_restore_dir
  574. fi
  575. done
  576. fi
  577. }
  578. function restore_user_config {
  579. if [[ $RESTORE_APP != 'all' ]]; then
  580. if [[ $RESTORE_APP != 'userconfig' ]]; then
  581. return
  582. fi
  583. fi
  584. if [ -d $USB_MOUNT/backup/config ]; then
  585. for d in $USB_MOUNT/backup/config/*/ ; do
  586. USERNAME=$(echo "$d" | awk -F '/' '{print $6}')
  587. if [[ $(is_valid_user "$USERNAME") == "1" ]]; then
  588. if [ ! -d /home/$USERNAME ]; then
  589. ${PROJECT_NAME}-adduser $USERNAME
  590. fi
  591. echo $"Restoring config files for $USERNAME"
  592. temp_restore_dir=/root/tempconfig
  593. restore_directory_from_usb $temp_restore_dir config/$USERNAME
  594. if [ -d $temp_restore_dir/home/$USERNAME/.config ]; then
  595. cp -r $temp_restore_dir/home/$USERNAME/.config /home/$USERNAME/
  596. else
  597. if [ ! -d /home/$USERNAME/.config ]; then
  598. mkdir /home/$USERNAME/.config
  599. fi
  600. cp -r $temp_restore_dir/* /home/$USERNAME/.config/
  601. fi
  602. if [ ! "$?" = "0" ]; then
  603. rm -rf $temp_restore_dir
  604. set_user_permissions
  605. backup_unmount_drive
  606. exit 664
  607. fi
  608. rm -rf $temp_restore_dir
  609. fi
  610. done
  611. fi
  612. }
  613. function restore_user_monkeysphere {
  614. if [[ $RESTORE_APP != 'all' ]]; then
  615. if [[ $RESTORE_APP != 'usermonkeysphere' ]]; then
  616. return
  617. fi
  618. fi
  619. if [ -d $USB_MOUNT/backup/monkeysphere ]; then
  620. for d in $USB_MOUNT/backup/monkeysphere/*/ ; do
  621. USERNAME=$(echo "$d" | awk -F '/' '{print $6}')
  622. if [[ $(is_valid_user "$USERNAME") == "1" ]]; then
  623. if [ ! -d /home/$USERNAME ]; then
  624. ${PROJECT_NAME}-adduser $USERNAME
  625. fi
  626. echo $"Restoring monkeysphere ids for $USERNAME"
  627. temp_restore_dir=/root/tempmonkeysphere
  628. restore_directory_from_usb $temp_restore_dir monkeysphere/$USERNAME
  629. if [ -d $temp_restore_dir/home/$USERNAME/.monkeysphere ]; then
  630. cp -r $temp_restore_dir/home/$USERNAME/.monkeysphere /home/$USERNAME/
  631. else
  632. if [ ! -d /home/$USERNAME/.monkeysphere ]; then
  633. mkdir /home/$USERNAME/.monkeysphere
  634. fi
  635. cp -r $temp_restore_dir/* /home/$USERNAME/.monkeysphere
  636. fi
  637. if [ ! "$?" = "0" ]; then
  638. rm -rf $temp_restore_dir
  639. set_user_permissions
  640. backup_unmount_drive
  641. exit 664
  642. fi
  643. rm -rf $temp_restore_dir
  644. fi
  645. done
  646. # The admin user is the identity certifier
  647. MY_EMAIL_ADDRESS="${ADMIN_USERNAME}@${HOSTNAME}"
  648. read_config_param MY_EMAIL_ADDRESS
  649. MY_GPG_PUBLIC_KEY_ID=$(gpg_pubkey_from_email "$ADMIN_USERNAME" "$MY_EMAIL_ADDRESS")
  650. fpr=$(gpg --with-colons --fingerprint $MY_GPG_PUBLIC_KEY_ID | grep fpr | head -n 1 | awk -F ':' '{print $10}')
  651. monkeysphere-authentication add-identity-certifier $fpr
  652. monkeysphere-authentication update-users
  653. fi
  654. }
  655. function restore_user_fin {
  656. if [[ $RESTORE_APP != 'all' ]]; then
  657. if [[ $RESTORE_APP != 'userfin' ]]; then
  658. return
  659. fi
  660. fi
  661. if [ -d $USB_MOUNT/backup/fin ]; then
  662. for d in $USB_MOUNT/backup/fin/*/ ; do
  663. USERNAME=$(echo "$d" | awk -F '/' '{print $6}')
  664. if [[ $(is_valid_user "$USERNAME") == "1" ]]; then
  665. if [ ! -d /home/$USERNAME ]; then
  666. ${PROJECT_NAME}-adduser $USERNAME
  667. fi
  668. echo $"Restoring fin files for $USERNAME"
  669. temp_restore_dir=/root/tempfin
  670. restore_directory_from_usb $temp_restore_dir fin/$USERNAME
  671. if [ -d $temp_restore_dir/home/$USERNAME/.fin ]; then
  672. cp -r $temp_restore_dir/home/$USERNAME/.fin /home/$USERNAME/
  673. else
  674. if [ ! -d /home/$USERNAME/.fin ]; then
  675. mkdir /home/$USERNAME/.fin
  676. fi
  677. cp -r $temp_restore_dir/* /home/$USERNAME/.fin/
  678. fi
  679. if [ ! "$?" = "0" ]; then
  680. rm -rf $temp_restore_dir
  681. set_user_permissions
  682. backup_unmount_drive
  683. exit 664
  684. fi
  685. rm -rf $temp_restore_dir
  686. fi
  687. done
  688. fi
  689. }
  690. function restore_user_local {
  691. if [[ $RESTORE_APP != 'all' ]]; then
  692. if [[ $RESTORE_APP != 'userlocal' ]]; then
  693. return
  694. fi
  695. fi
  696. if [ -d $USB_MOUNT/backup/local ]; then
  697. for d in $USB_MOUNT/backup/local/*/ ; do
  698. USERNAME=$(echo "$d" | awk -F '/' '{print $6}')
  699. if [[ $(is_valid_user "$USERNAME") == "1" ]]; then
  700. if [ ! -d /home/$USERNAME ]; then
  701. ${PROJECT_NAME}-adduser $USERNAME
  702. fi
  703. echo $"Restoring local files for $USERNAME"
  704. temp_restore_dir=/root/templocal
  705. restore_directory_from_usb $temp_restore_dir local/$USERNAME
  706. if [ -d $temp_restore_dir/home/$USERNAME/.local ]; then
  707. cp -r $temp_restore_dir/home/$USERNAME/.local /home/$USERNAME/
  708. else
  709. if [ ! -d /home/$USERNAME/.local ]; then
  710. mkdir /home/$USERNAME/.local
  711. fi
  712. cp -r $temp_restore_dir/* /home/$USERNAME/.local/
  713. fi
  714. if [ ! "$?" = "0" ]; then
  715. rm -rf $temp_restore_dir
  716. set_user_permissions
  717. backup_unmount_drive
  718. exit 664
  719. fi
  720. rm -rf $temp_restore_dir
  721. fi
  722. done
  723. fi
  724. }
  725. function restore_certs {
  726. if [[ $RESTORE_APP != 'all' ]]; then
  727. if [[ $RESTORE_APP != 'certs' ]]; then
  728. return
  729. fi
  730. fi
  731. if [ -d $USB_MOUNT/backup/ssl ]; then
  732. echo $"Restoring certificates"
  733. mkdir /root/tempssl
  734. restore_directory_from_usb /root/tempssl ssl
  735. if [ -d /root/tempssl/etc/ssl ]; then
  736. cp -r /root/tempssl/etc/ssl/* /etc/ssl
  737. else
  738. cp -r /root/tempssl/* /etc/ssl/
  739. fi
  740. if [ ! "$?" = "0" ]; then
  741. set_user_permissions
  742. backup_unmount_drive
  743. exit 276
  744. fi
  745. rm -rf /root/tempssl
  746. update-ca-certificates
  747. # restore ownership
  748. if [ -f /etc/ssl/private/xmpp.key ]; then
  749. chown prosody:prosody /etc/ssl/private/xmpp.key
  750. chown prosody:prosody /etc/ssl/certs/xmpp.*
  751. fi
  752. if [ -d /etc/dovecot ]; then
  753. chown root:dovecot /etc/ssl/private/dovecot.*
  754. chown root:dovecot /etc/ssl/certs/dovecot.*
  755. fi
  756. if [ -f /etc/ssl/private/exim.key ]; then
  757. cp /etc/ssl/private/exim.key /etc/exim4
  758. cp /etc/ssl/certs/exim.crt /etc/exim4
  759. cp /etc/ssl/certs/exim.dhparam /etc/exim4
  760. chown root:Debian-exim /etc/exim4/exim.key /etc/exim4/exim.crt /etc/exim4/exim.dhparam
  761. chmod 640 /etc/exim4/exim.key /etc/exim4/exim.crt /etc/exim4/exim.dhparam
  762. fi
  763. if [ -f /etc/ssl/private/mumble.key ]; then
  764. if [ -d /var/lib/mumble-server ]; then
  765. cp /etc/ssl/certs/mumble.* /var/lib/mumble-server
  766. cp /etc/ssl/private/mumble.key /var/lib/mumble-server
  767. chown -R mumble-server:mumble-server /var/lib/mumble-server
  768. fi
  769. fi
  770. fi
  771. }
  772. function restore_personal_settings {
  773. if [[ $RESTORE_APP != 'all' ]]; then
  774. if [[ $RESTORE_APP != 'personal' ]]; then
  775. return
  776. fi
  777. fi
  778. if [ -d $USB_MOUNT/backup/personal ]; then
  779. for d in $USB_MOUNT/backup/personal/*/ ; do
  780. USERNAME=$(echo "$d" | awk -F '/' '{print $6}')
  781. if [[ $(is_valid_user "$USERNAME") == "1" ]]; then
  782. if [ -d $USB_MOUNT/backup/personal/$USERNAME ]; then
  783. if [ ! -d /home/$USERNAME ]; then
  784. ${PROJECT_NAME}-adduser $USERNAME
  785. fi
  786. echo $"Restoring personal settings for $USERNAME"
  787. temp_restore_dir=/root/temppersonal
  788. restore_directory_from_usb $temp_restore_dir personal/$USERNAME
  789. if [ -d /home/$USERNAME/personal ]; then
  790. rm -rf /home/$USERNAME/personal
  791. fi
  792. if [ -d $temp_restore_dir/home/$USERNAME/personal ]; then
  793. mv $temp_restore_dir/home/$USERNAME/personal /home/$USERNAME
  794. else
  795. if [ ! -d /home/$USERNAME/personal ]; then
  796. mkdir /home/$USERNAME/personal
  797. fi
  798. cp -r $temp_restore_dir/* /home/$USERNAME/personal/
  799. fi
  800. if [ ! "$?" = "0" ]; then
  801. set_user_permissions
  802. backup_unmount_drive
  803. exit 184
  804. fi
  805. rm -rf $temp_restore_dir
  806. fi
  807. fi
  808. done
  809. fi
  810. }
  811. function restore_mailing_list {
  812. if [[ $RESTORE_APP != 'all' ]]; then
  813. if [[ $RESTORE_APP != 'mailinglist' ]]; then
  814. return
  815. fi
  816. fi
  817. if [ -d /var/spool/mlmmj ]; then
  818. echo $"Restoring public mailing list"
  819. temp_restore_dir=/root/tempmailinglist
  820. restore_directory_from_usb $temp_restore_dir mailinglist
  821. if [ -d $temp_restore_dir/root/spool/mlmmj ]; then
  822. cp -r $temp_restore_dir/root/spool/mlmmj/* /var/spool/mlmmj
  823. else
  824. cp -r $temp_restore_dir/* /var/spool/mlmmj/
  825. fi
  826. if [ ! "$?" = "0" ]; then
  827. set_user_permissions
  828. backup_unmount_drive
  829. exit 526
  830. fi
  831. rm -rf $temp_restore_dir
  832. fi
  833. }
  834. function restore_email {
  835. if [[ $RESTORE_APP != 'all' ]]; then
  836. if [[ $RESTORE_APP != 'email' ]]; then
  837. return
  838. fi
  839. fi
  840. if [ -d $USB_MOUNT/backup/mail ]; then
  841. for d in $USB_MOUNT/backup/mail/*/ ; do
  842. USERNAME=$(echo "$d" | awk -F '/' '{print $6}')
  843. if [[ $(is_valid_user "$USERNAME") == "1" ]]; then
  844. if [ ! -d /home/$USERNAME ]; then
  845. ${PROJECT_NAME}-adduser $USERNAME
  846. fi
  847. echo $"Restoring emails for $USERNAME"
  848. temp_restore_dir=/root/tempmail
  849. restore_directory_from_usb $temp_restore_dir mail/$USERNAME
  850. if [ ! -d /home/$USERNAME/Maildir ]; then
  851. mkdir /home/$USERNAME/Maildir
  852. fi
  853. if [ -d $temp_restore_dir/root/tempbackupemail/$USERNAME ]; then
  854. tar -xzvf $temp_restore_dir/root/tempbackupemail/$USERNAME/maildir.tar.gz -C /
  855. else
  856. tar -xzvf $temp_restore_dir/maildir.tar.gz -C /
  857. fi
  858. if [ ! "$?" = "0" ]; then
  859. set_user_permissions
  860. backup_unmount_drive
  861. exit 9276382
  862. fi
  863. rm -rf $temp_restore_dir
  864. fi
  865. done
  866. fi
  867. }
  868. function get_restore_app {
  869. if [ ${1} ]; then
  870. if [ ! -d /home/${1} ]; then
  871. RESTORE_APP=${1}
  872. echo $"Restore $RESTORE_APP"
  873. fi
  874. fi
  875. }
  876. get_restore_app ${2}
  877. backup_mount_drive ${1} ${ADMIN_USERNAME} ${2}
  878. check_backup_exists
  879. check_admin_user
  880. copy_gpg_keys
  881. restore_blocklist
  882. restore_configfiles
  883. same_admin_user
  884. restore_passwordstore
  885. restore_mariadb
  886. restore_postgresql
  887. restore_letsencrypt
  888. restore_tor
  889. restore_mutt_settings
  890. restore_gpg
  891. restore_procmail
  892. restore_spamassassin
  893. restore_admin_readme
  894. restore_user_ssh_keys
  895. restore_user_config
  896. restore_user_monkeysphere
  897. restore_user_fin
  898. restore_user_local
  899. restore_certs
  900. restore_personal_settings
  901. restore_mailing_list
  902. restore_email
  903. restore_apps local $RESTORE_APP
  904. set_user_permissions
  905. update_default_domain
  906. backup_unmount_drive
  907. # ensure that all TLS certificates are pinned
  908. #${PROJECT_NAME}-pin-cert all
  909. echo $"Restore from USB drive is complete. You can now unplug it."
  910. exit 0