freedombone-restore-local 35KB

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