freedombone-restore-local 34KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962
  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-2017 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. cp -r $temp_restore_dir/* /home/$USERNAME/.gnupg/
  416. fi
  417. if [ ! "$?" = "0" ]; then
  418. rm -rf $temp_restore_dir
  419. set_user_permissions
  420. backup_unmount_drive
  421. exit 276
  422. fi
  423. rm -rf $temp_restore_dir
  424. gpg_set_permissions $USERNAME
  425. if [[ "$USERNAME" == "$ADMIN_USERNAME" ]]; then
  426. cp -r /home/$USERNAME/.gnupg /root
  427. if [ ! "$?" = "0" ]; then
  428. set_user_permissions
  429. backup_unmount_drive
  430. exit 283
  431. fi
  432. gpg_set_permissions root
  433. fi
  434. fi
  435. done
  436. fi
  437. }
  438. function restore_procmail {
  439. if [[ $RESTORE_APP != 'all' ]]; then
  440. if [[ $RESTORE_APP != 'procmail' ]]; then
  441. return
  442. fi
  443. fi
  444. if [ -d $USB_MOUNT/backup/procmail ]; then
  445. for d in $USB_MOUNT/backup/procmail/*/ ; do
  446. USERNAME=$(echo "$d" | awk -F '/' '{print $6}')
  447. if [[ $(is_valid_user "$USERNAME") == "1" ]]; then
  448. if [ ! -d /home/$USERNAME ]; then
  449. ${PROJECT_NAME}-adduser $USERNAME
  450. fi
  451. echo $"Restoring procmail settings for $USERNAME"
  452. temp_restore_dir=/root/tempprocmail
  453. restore_directory_from_usb $temp_restore_dir procmail/$USERNAME
  454. if [ -d $temp_restore_dir ]; then
  455. if [ -d $temp_restore_dir/home/$USERNAME/tempbackup ]; then
  456. cp -f $temp_restore_dir/home/$USERNAME/tempbackup/.procmailrc /home/$USERNAME/
  457. else
  458. cp -f $temp_restore_dir/.procmailrc /home/$USERNAME/.procmailrc
  459. fi
  460. if [ ! "$?" = "0" ]; then
  461. rm -rf $temp_restore_dir
  462. set_user_permissions
  463. backup_unmount_drive
  464. exit 276
  465. fi
  466. rm -rf $temp_restore_dir
  467. fi
  468. fi
  469. done
  470. fi
  471. }
  472. function restore_spamassassin {
  473. if [[ $RESTORE_APP != 'all' ]]; then
  474. if [[ $RESTORE_APP != 'spamassassin' ]]; then
  475. return
  476. fi
  477. fi
  478. if [ -d $USB_MOUNT/backup/spamassassin ]; then
  479. for d in $USB_MOUNT/backup/spamassassin/*/ ; do
  480. USERNAME=$(echo "$d" | awk -F '/' '{print $6}')
  481. if [[ $(is_valid_user "$USERNAME") == "1" ]]; then
  482. if [ -d $USB_MOUNT/backup/spamassassin/$USERNAME ]; then
  483. if [ ! -d /home/$USERNAME ]; then
  484. ${PROJECT_NAME}-adduser $USERNAME
  485. fi
  486. echo $"Restoring spamassassin settings for $USERNAME"
  487. temp_restore_dir=/root/tempspamassassin
  488. restore_directory_from_usb $temp_restore_dir spamassassin/$USERNAME
  489. if [ -d $temp_restore_dir/home/$USERNAME ]; then
  490. cp -rf $temp_restore_dir/home/$USERNAME/.spamassassin /home/$USERNAME/
  491. else
  492. cp -rf $temp_restore_dir/* /home/$USERNAME/.spamassassin/
  493. fi
  494. if [ ! "$?" = "0" ]; then
  495. rm -rf $temp_restore_dir
  496. set_user_permissions
  497. backup_unmount_drive
  498. exit 276
  499. fi
  500. rm -rf $temp_restore_dir
  501. fi
  502. fi
  503. done
  504. fi
  505. }
  506. function restore_admin_readme {
  507. if [[ $RESTORE_APP != 'all' ]]; then
  508. if [[ $RESTORE_APP != 'readme' ]]; then
  509. return
  510. fi
  511. fi
  512. if [ -d $USB_MOUNT/backup/readme ]; then
  513. echo $"Restoring admin user README"
  514. # Make a backup of the original README file
  515. # incase old passwords need to be used
  516. if [ -f /home/$ADMIN_USERNAME/README ]; then
  517. if [ ! -f /home/$ADMIN_USERNAME/README_original ]; then
  518. cp /home/$ADMIN_USERNAME/README /home/$ADMIN_USERNAME/README_original
  519. fi
  520. fi
  521. temp_restore_dir=/root/tempreadme
  522. restore_directory_from_usb $temp_restore_dir readme
  523. if [ -d $temp_restore_dir/home/$ADMIN_USERNAME/tempbackup ]; then
  524. cp -f $temp_restore_dir/home/$ADMIN_USERNAME/tempbackup/README /home/$ADMIN_USERNAME/
  525. else
  526. cp -f $temp_restore_dir/README /home/$ADMIN_USERNAME/README
  527. fi
  528. if [ ! "$?" = "0" ]; then
  529. rm -rf $temp_restore_dir
  530. set_user_permissions
  531. backup_unmount_drive
  532. exit 276
  533. fi
  534. rm -rf $temp_restore_dir
  535. fi
  536. }
  537. function restore_user_ssh_keys {
  538. if [[ $RESTORE_APP != 'all' ]]; then
  539. if [[ $RESTORE_APP != 'ssh' ]]; then
  540. return
  541. fi
  542. fi
  543. if [ -d $USB_MOUNT/backup/ssh ]; then
  544. for d in $USB_MOUNT/backup/ssh/*/ ; do
  545. USERNAME=$(echo "$d" | awk -F '/' '{print $6}')
  546. if [[ $(is_valid_user "$USERNAME") == "1" ]]; then
  547. if [ ! -d /home/$USERNAME ]; then
  548. ${PROJECT_NAME}-adduser $USERNAME
  549. fi
  550. echo $"Restoring ssh keys for $USERNAME"
  551. temp_restore_dir=/root/tempssh
  552. restore_directory_from_usb $temp_restore_dir ssh/$USERNAME
  553. if [ -d $temp_restore_dir/home/$USERNAME/.ssh ]; then
  554. cp -r $temp_restore_dir/home/$USERNAME/.ssh /home/$USERNAME/
  555. else
  556. cp -r $temp_restore_dir/* /home/$USERNAME/.ssh/
  557. fi
  558. if [ ! "$?" = "0" ]; then
  559. rm -rf $temp_restore_dir
  560. set_user_permissions
  561. backup_unmount_drive
  562. exit 664
  563. fi
  564. rm -rf $temp_restore_dir
  565. fi
  566. done
  567. fi
  568. }
  569. function restore_user_config {
  570. if [[ $RESTORE_APP != 'all' ]]; then
  571. if [[ $RESTORE_APP != 'userconfig' ]]; then
  572. return
  573. fi
  574. fi
  575. if [ -d $USB_MOUNT/backup/config ]; then
  576. for d in $USB_MOUNT/backup/config/*/ ; do
  577. USERNAME=$(echo "$d" | awk -F '/' '{print $6}')
  578. if [[ $(is_valid_user "$USERNAME") == "1" ]]; then
  579. if [ ! -d /home/$USERNAME ]; then
  580. ${PROJECT_NAME}-adduser $USERNAME
  581. fi
  582. echo $"Restoring config files for $USERNAME"
  583. temp_restore_dir=/root/tempconfig
  584. restore_directory_from_usb $temp_restore_dir config/$USERNAME
  585. if [ -d $temp_restore_dir/home/$USERNAME/.config ]; then
  586. cp -r $temp_restore_dir/home/$USERNAME/.config /home/$USERNAME/
  587. else
  588. if [ ! -d /home/$USERNAME/.config ]; then
  589. mkdir /home/$USERNAME/.config
  590. fi
  591. cp -r $temp_restore_dir/* /home/$USERNAME/.config/
  592. fi
  593. if [ ! "$?" = "0" ]; then
  594. rm -rf $temp_restore_dir
  595. set_user_permissions
  596. backup_unmount_drive
  597. exit 664
  598. fi
  599. rm -rf $temp_restore_dir
  600. fi
  601. done
  602. fi
  603. }
  604. function restore_user_monkeysphere {
  605. if [[ $RESTORE_APP != 'all' ]]; then
  606. if [[ $RESTORE_APP != 'usermonkeysphere' ]]; then
  607. return
  608. fi
  609. fi
  610. if [ -d $USB_MOUNT/backup/monkeysphere ]; then
  611. for d in $USB_MOUNT/backup/monkeysphere/*/ ; do
  612. USERNAME=$(echo "$d" | awk -F '/' '{print $6}')
  613. if [[ $(is_valid_user "$USERNAME") == "1" ]]; then
  614. if [ ! -d /home/$USERNAME ]; then
  615. ${PROJECT_NAME}-adduser $USERNAME
  616. fi
  617. echo $"Restoring monkeysphere ids for $USERNAME"
  618. temp_restore_dir=/root/tempmonkeysphere
  619. restore_directory_from_usb $temp_restore_dir monkeysphere/$USERNAME
  620. if [ -d $temp_restore_dir/home/$USERNAME/.monkeysphere ]; then
  621. cp -r $temp_restore_dir/home/$USERNAME/.monkeysphere /home/$USERNAME/
  622. else
  623. cp -r $temp_restore_dir/* /home/$USERNAME/.monkeysphere
  624. fi
  625. if [ ! "$?" = "0" ]; then
  626. rm -rf $temp_restore_dir
  627. set_user_permissions
  628. backup_unmount_drive
  629. exit 664
  630. fi
  631. rm -rf $temp_restore_dir
  632. fi
  633. done
  634. # The admin user is the identity certifier
  635. MY_EMAIL_ADDRESS="${ADMIN_USERNAME}@${HOSTNAME}"
  636. read_config_param MY_EMAIL_ADDRESS
  637. MY_GPG_PUBLIC_KEY_ID=$(gpg_pubkey_from_email "$ADMIN_USERNAME" "$MY_EMAIL_ADDRESS")
  638. fpr=$(gpg --with-colons --fingerprint $MY_GPG_PUBLIC_KEY_ID | grep fpr | head -n 1 | awk -F ':' '{print $10}')
  639. monkeysphere-authentication add-identity-certifier $fpr
  640. monkeysphere-authentication update-users
  641. fi
  642. }
  643. function restore_user_fin {
  644. if [[ $RESTORE_APP != 'all' ]]; then
  645. if [[ $RESTORE_APP != 'userfin' ]]; then
  646. return
  647. fi
  648. fi
  649. if [ -d $USB_MOUNT/backup/fin ]; then
  650. for d in $USB_MOUNT/backup/fin/*/ ; do
  651. USERNAME=$(echo "$d" | awk -F '/' '{print $6}')
  652. if [[ $(is_valid_user "$USERNAME") == "1" ]]; then
  653. if [ ! -d /home/$USERNAME ]; then
  654. ${PROJECT_NAME}-adduser $USERNAME
  655. fi
  656. echo $"Restoring fin files for $USERNAME"
  657. temp_restore_dir=/root/tempfin
  658. restore_directory_from_usb $temp_restore_dir fin/$USERNAME
  659. if [ -d $temp_restore_dir/home/$USERNAME/.fin ]; then
  660. cp -r $temp_restore_dir/home/$USERNAME/.fin /home/$USERNAME/
  661. else
  662. cp -r $temp_restore_dir/* /home/$USERNAME/.fin/
  663. fi
  664. if [ ! "$?" = "0" ]; then
  665. rm -rf $temp_restore_dir
  666. set_user_permissions
  667. backup_unmount_drive
  668. exit 664
  669. fi
  670. rm -rf $temp_restore_dir
  671. fi
  672. done
  673. fi
  674. }
  675. function restore_user_local {
  676. if [[ $RESTORE_APP != 'all' ]]; then
  677. if [[ $RESTORE_APP != 'userlocal' ]]; then
  678. return
  679. fi
  680. fi
  681. if [ -d $USB_MOUNT/backup/local ]; then
  682. for d in $USB_MOUNT/backup/local/*/ ; do
  683. USERNAME=$(echo "$d" | awk -F '/' '{print $6}')
  684. if [[ $(is_valid_user "$USERNAME") == "1" ]]; then
  685. if [ ! -d /home/$USERNAME ]; then
  686. ${PROJECT_NAME}-adduser $USERNAME
  687. fi
  688. echo $"Restoring local files for $USERNAME"
  689. temp_restore_dir=/root/templocal
  690. restore_directory_from_usb $temp_restore_dir local/$USERNAME
  691. if [ -d $temp_restore_dir/home/$USERNAME/.local ]; then
  692. cp -r $temp_restore_dir/home/$USERNAME/.local /home/$USERNAME/
  693. else
  694. cp -r $temp_restore_dir/* /home/$USERNAME/.local/
  695. fi
  696. if [ ! "$?" = "0" ]; then
  697. rm -rf $temp_restore_dir
  698. set_user_permissions
  699. backup_unmount_drive
  700. exit 664
  701. fi
  702. rm -rf $temp_restore_dir
  703. fi
  704. done
  705. fi
  706. }
  707. function restore_certs {
  708. if [[ $RESTORE_APP != 'all' ]]; then
  709. if [[ $RESTORE_APP != 'certs' ]]; then
  710. return
  711. fi
  712. fi
  713. if [ -d $USB_MOUNT/backup/ssl ]; then
  714. echo $"Restoring certificates"
  715. mkdir /root/tempssl
  716. restore_directory_from_usb /root/tempssl ssl
  717. if [ -d /root/tempssl/etc/ssl ]; then
  718. cp -r /root/tempssl/etc/ssl/* /etc/ssl
  719. else
  720. cp -r /root/tempssl/* /etc/ssl/
  721. fi
  722. if [ ! "$?" = "0" ]; then
  723. set_user_permissions
  724. backup_unmount_drive
  725. exit 276
  726. fi
  727. rm -rf /root/tempssl
  728. update-ca-certificates
  729. # restore ownership
  730. if [ -f /etc/ssl/private/xmpp.key ]; then
  731. chown prosody:prosody /etc/ssl/private/xmpp.key
  732. chown prosody:prosody /etc/ssl/certs/xmpp.*
  733. fi
  734. if [ -d /etc/dovecot ]; then
  735. chown root:dovecot /etc/ssl/private/dovecot.*
  736. chown root:dovecot /etc/ssl/certs/dovecot.*
  737. fi
  738. if [ -f /etc/ssl/private/exim.key ]; then
  739. cp /etc/ssl/private/exim.key /etc/exim4
  740. cp /etc/ssl/certs/exim.crt /etc/exim4
  741. cp /etc/ssl/certs/exim.dhparam /etc/exim4
  742. chown root:Debian-exim /etc/exim4/exim.key /etc/exim4/exim.crt /etc/exim4/exim.dhparam
  743. chmod 640 /etc/exim4/exim.key /etc/exim4/exim.crt /etc/exim4/exim.dhparam
  744. fi
  745. if [ -f /etc/ssl/private/mumble.key ]; then
  746. if [ -d /var/lib/mumble-server ]; then
  747. cp /etc/ssl/certs/mumble.* /var/lib/mumble-server
  748. cp /etc/ssl/private/mumble.key /var/lib/mumble-server
  749. chown -R mumble-server:mumble-server /var/lib/mumble-server
  750. fi
  751. fi
  752. fi
  753. }
  754. function restore_personal_settings {
  755. if [[ $RESTORE_APP != 'all' ]]; then
  756. if [[ $RESTORE_APP != 'personal' ]]; then
  757. return
  758. fi
  759. fi
  760. if [ -d $USB_MOUNT/backup/personal ]; then
  761. for d in $USB_MOUNT/backup/personal/*/ ; do
  762. USERNAME=$(echo "$d" | awk -F '/' '{print $6}')
  763. if [[ $(is_valid_user "$USERNAME") == "1" ]]; then
  764. if [ -d $USB_MOUNT/backup/personal/$USERNAME ]; then
  765. if [ ! -d /home/$USERNAME ]; then
  766. ${PROJECT_NAME}-adduser $USERNAME
  767. fi
  768. echo $"Restoring personal settings for $USERNAME"
  769. temp_restore_dir=/root/temppersonal
  770. restore_directory_from_usb $temp_restore_dir personal/$USERNAME
  771. if [ -d /home/$USERNAME/personal ]; then
  772. rm -rf /home/$USERNAME/personal
  773. fi
  774. if [ -d $temp_restore_dir/home/$USERNAME/personal ]; then
  775. mv $temp_restore_dir/home/$USERNAME/personal /home/$USERNAME
  776. else
  777. cp -r $temp_restore_dir/* /home/$USERNAME/personal/
  778. fi
  779. if [ ! "$?" = "0" ]; then
  780. set_user_permissions
  781. backup_unmount_drive
  782. exit 184
  783. fi
  784. rm -rf $temp_restore_dir
  785. fi
  786. fi
  787. done
  788. fi
  789. }
  790. function restore_mailing_list {
  791. if [[ $RESTORE_APP != 'all' ]]; then
  792. if [[ $RESTORE_APP != 'mailinglist' ]]; then
  793. return
  794. fi
  795. fi
  796. if [ -d /var/spool/mlmmj ]; then
  797. echo $"Restoring public mailing list"
  798. temp_restore_dir=/root/tempmailinglist
  799. restore_directory_from_usb $temp_restore_dir mailinglist
  800. if [ -d $temp_restore_dir/root/spool/mlmmj ]; then
  801. cp -r $temp_restore_dir/root/spool/mlmmj/* /var/spool/mlmmj
  802. else
  803. cp -r $temp_restore_dir/* /var/spool/mlmmj/
  804. fi
  805. if [ ! "$?" = "0" ]; then
  806. set_user_permissions
  807. backup_unmount_drive
  808. exit 526
  809. fi
  810. rm -rf $temp_restore_dir
  811. fi
  812. }
  813. function restore_email {
  814. if [[ $RESTORE_APP != 'all' ]]; then
  815. if [[ $RESTORE_APP != 'email' ]]; then
  816. return
  817. fi
  818. fi
  819. if [ -d $USB_MOUNT/backup/mail ]; then
  820. for d in $USB_MOUNT/backup/mail/*/ ; do
  821. USERNAME=$(echo "$d" | awk -F '/' '{print $6}')
  822. if [[ $(is_valid_user "$USERNAME") == "1" ]]; then
  823. if [ ! -d /home/$USERNAME ]; then
  824. ${PROJECT_NAME}-adduser $USERNAME
  825. fi
  826. echo $"Restoring emails for $USERNAME"
  827. temp_restore_dir=/root/tempmail
  828. restore_directory_from_usb $temp_restore_dir mail/$USERNAME
  829. if [ ! -d /home/$USERNAME/Maildir ]; then
  830. mkdir /home/$USERNAME/Maildir
  831. fi
  832. if [ -d $temp_restore_dir/root/tempbackupemail/$USERNAME ]; then
  833. tar -xzvf $temp_restore_dir/root/tempbackupemail/$USERNAME/maildir.tar.gz -C /
  834. else
  835. tar -xzvf $temp_restore_dir/maildir.tar.gz -C /
  836. fi
  837. if [ ! "$?" = "0" ]; then
  838. set_user_permissions
  839. backup_unmount_drive
  840. exit 9276382
  841. fi
  842. rm -rf $temp_restore_dir
  843. fi
  844. done
  845. fi
  846. }
  847. function get_restore_app {
  848. if [ ${1} ]; then
  849. if [ ! -d /home/${1} ]; then
  850. RESTORE_APP=${1}
  851. echo $"Restore $RESTORE_APP"
  852. fi
  853. fi
  854. }
  855. get_restore_app ${2}
  856. backup_mount_drive ${1} ${ADMIN_USERNAME} ${2}
  857. check_backup_exists
  858. check_admin_user
  859. copy_gpg_keys
  860. restore_blocklist
  861. restore_configfiles
  862. same_admin_user
  863. restore_passwordstore
  864. restore_mariadb
  865. restore_postgresql
  866. restore_letsencrypt
  867. restore_tor
  868. restore_mutt_settings
  869. restore_gpg
  870. restore_procmail
  871. restore_spamassassin
  872. restore_admin_readme
  873. restore_user_ssh_keys
  874. restore_user_config
  875. restore_user_monkeysphere
  876. restore_user_fin
  877. restore_user_local
  878. restore_certs
  879. restore_personal_settings
  880. restore_mailing_list
  881. restore_email
  882. restore_apps local $RESTORE_APP
  883. set_user_permissions
  884. update_default_domain
  885. backup_unmount_drive
  886. # ensure that all TLS certificates are pinned
  887. #${PROJECT_NAME}-pin-cert all
  888. echo $"Restore from USB drive is complete. You can now unplug it."
  889. exit 0