freedombone-restore-local 27KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797
  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. CONFIGURATION_FILE=$HOME/${PROJECT_NAME}.cfg
  32. BACKUP_EXTRA_DIRECTORIES=/root/backup-extra-dirs.csv
  33. # whether to restore everything or just a specific application
  34. RESTORE_APP='all'
  35. export TEXTDOMAIN=${PROJECT_NAME}-restore-local
  36. export TEXTDOMAINDIR="/usr/share/locale"
  37. PROJECT_INSTALL_DIR=/usr/local/bin
  38. if [ -f /usr/bin/${PROJECT_NAME} ]; then
  39. PROJECT_INSTALL_DIR=/usr/bin
  40. fi
  41. # MariaDB password
  42. DATABASE_PASSWORD=$(${PROJECT_NAME}-pass -u root -a mariadb)
  43. function please_wait {
  44. local str width height length
  45. width=$(tput cols)
  46. height=$(tput lines)
  47. str="Standby to restore from USB"
  48. length=${#str}
  49. clear
  50. tput cup $((height / 2)) $(((width / 2) - (length / 2)))
  51. echo "$str"
  52. tput cup $((height * 3 / 5)) $(((width / 2)))
  53. echo -n ''
  54. }
  55. please_wait
  56. source $PROJECT_INSTALL_DIR/${PROJECT_NAME}-vars
  57. # include utils which allow function_check, go and drive mount
  58. UTILS_FILES=/usr/share/${PROJECT_NAME}/utils/${PROJECT_NAME}-utils-*
  59. for f in $UTILS_FILES
  60. do
  61. source $f
  62. done
  63. clear
  64. USB_DRIVE=/dev/sdb1
  65. USB_MOUNT=/mnt/usb
  66. ADMIN_USERNAME=''
  67. ADMIN_NAME=
  68. read_config_param USB_DRIVE
  69. if [ -f $COMPLETION_FILE ]; then
  70. ADMIN_USERNAME=$(get_completion_param "Admin user")
  71. fi
  72. function check_backup_exists {
  73. if [ ! -d $USB_MOUNT/backup ]; then
  74. echo $"No backup directory found on the USB drive."
  75. set_user_permissions
  76. backup_unmount_drive
  77. exit 2
  78. fi
  79. }
  80. function check_admin_user {
  81. echo $"Checking that admin user exists"
  82. if [ ! -d /home/$ADMIN_USERNAME ]; then
  83. echo $"Username $ADMIN_USERNAME not found. Reinstall ${PROJECT_NAME} with this username."
  84. set_user_permissions
  85. backup_unmount_drive
  86. exit 295
  87. fi
  88. }
  89. function copy_gpg_keys {
  90. echo $"Copying GPG keys from admin user to root"
  91. cp -r /home/$ADMIN_USERNAME/.gnupg /root
  92. gpg_set_permissions root
  93. }
  94. function restore_blocklist {
  95. if [[ $RESTORE_APP != 'all' ]]; then
  96. if [[ $RESTORE_APP != 'blocklist' ]]; then
  97. return
  98. fi
  99. fi
  100. if [ -d $USB_MOUNT/backup/blocklist ]; then
  101. echo $"Restoring blocklist"
  102. temp_restore_dir=/root/tempblocklist
  103. restore_directory_from_usb $temp_restore_dir blocklist
  104. if [ -f $temp_restore_dir/root/tempbackupblocklist/${PROJECT_NAME}-firewall-domains.cfg ]; then
  105. cp -f $temp_restore_dir/root/tempbackupblocklist/${PROJECT_NAME}-firewall-domains.cfg /root/${PROJECT_NAME}-firewall-domains.cfg
  106. fi
  107. rm -rf $temp_restore_dir
  108. firewall_refresh_blocklist
  109. fi
  110. }
  111. function restore_configfiles {
  112. if [[ $RESTORE_APP != 'all' ]]; then
  113. if [[ $RESTORE_APP != 'configfiles' ]]; then
  114. return
  115. fi
  116. fi
  117. # this restores *.cfg and COMPLETION_FILE
  118. if [ -d $USB_MOUNT/backup/configfiles ]; then
  119. echo $"Restoring configuration files"
  120. temp_restore_dir=/root/tempconfigfiles
  121. restore_directory_from_usb $temp_restore_dir configfiles
  122. if [ -f $temp_restore_dir/root/.nostore ]; then
  123. if [ ! -f /root/.nostore ]; then
  124. touch /root/.nostore
  125. fi
  126. else
  127. if [ -f /root/.nostore ]; then
  128. rm /root/.nostore
  129. fi
  130. fi
  131. #if [ -f $temp_restore_dir$NODEJS_INSTALLED_APPS_FILE ]; then
  132. # cp -f $temp_restore_dir$NODEJS_INSTALLED_APPS_FILE $NODEJS_INSTALLED_APPS_FILE
  133. #fi
  134. #if [ -f $temp_restore_dir/root/${PROJECT_NAME}.cfg ]; then
  135. # cp -f $temp_restore_dir/root/${PROJECT_NAME}.cfg $CONFIGURATION_FILE
  136. # if [ ! "$?" = "0" ]; then
  137. # set_user_permissions
  138. # backup_unmount_drive
  139. # rm -rf $temp_restore_dir
  140. # exit 5294
  141. # fi
  142. #fi
  143. #if [ -f $CONFIGURATION_FILE ]; then
  144. # # install according to the config file
  145. # freedombone -c $CONFIGURATION_FILE
  146. #fi
  147. #if [ -f $temp_restore_dir/root/${PROJECT_NAME}-completed.txt ]; then
  148. # cp -f $temp_restore_dir/root/${PROJECT_NAME}-completed.txt $COMPLETION_FILE
  149. # if [ ! "$?" = "0" ]; then
  150. # set_user_permissions
  151. # backup_unmount_drive
  152. # rm -rf $temp_restore_dir
  153. # exit 6382
  154. # fi
  155. #fi
  156. if [ -f ${temp_restore_dir}${BACKUP_EXTRA_DIRECTORIES} ]; then
  157. cp -f ${temp_restore_dir}${BACKUP_EXTRA_DIRECTORIES} ${BACKUP_EXTRA_DIRECTORIES}
  158. if [ ! "$?" = "0" ]; then
  159. set_user_permissions
  160. backup_unmount_drive
  161. rm -rf $temp_restore_dir
  162. exit 62121
  163. fi
  164. fi
  165. # restore nginx password hashes
  166. if [ -f $temp_restore_dir/root/htpasswd ]; then
  167. cp -f $temp_restore_dir/root/htpasswd /etc/nginx/.htpasswd
  168. fi
  169. rm -rf $temp_restore_dir
  170. fi
  171. }
  172. function same_admin_user {
  173. PREV_ADMIN_USERNAME=$(get_completion_param "Admin user")
  174. if [[ "$PREV_ADMIN_USERNAME" != "$ADMIN_USERNAME" ]]; then
  175. 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"
  176. set_user_permissions
  177. backup_unmount_drive
  178. exit 73265
  179. fi
  180. }
  181. function restore_mariadb {
  182. if [[ $RESTORE_APP != 'all' ]]; then
  183. if [[ $RESTORE_APP != 'mariadb' ]]; then
  184. return
  185. fi
  186. fi
  187. if [[ $(is_completed install_mariadb) == "0" ]]; then
  188. function_check install_mariadb
  189. install_mariadb
  190. fi
  191. if [ -d $USB_MOUNT/backup/mariadb ]; then
  192. echo $"Restoring mysql settings"
  193. keep_database_running
  194. temp_restore_dir=/root/tempmariadb
  195. restore_directory_from_usb $temp_restore_dir mariadb
  196. store_original_mariadb_password
  197. echo $'Obtaining original MariaDB password'
  198. db_pass=$(cat /root/.mariadboriginal)
  199. if [ ${#db_pass} -gt 0 ]; then
  200. echo $"Restore the MariaDB user table"
  201. mysqlsuccess=$(mysql -u root --password="$db_pass" mysql -o < ${temp_restore_dir}${temp_restore_dir}/mysql.sql)
  202. if [ ! "$?" = "0" ]; then
  203. echo $"Try again using the password obtained from backup"
  204. db_pass=$(${PROJECT_NAME}-pass -u root -a mariadb)
  205. mysqlsuccess=$(mysql -u root --password="$db_pass" mysql -o < ${temp_restore_dir}${temp_restore_dir}/mysql.sql)
  206. fi
  207. if [ ! "$?" = "0" ]; then
  208. echo "$mysqlsuccess"
  209. set_user_permissions
  210. backup_unmount_drive
  211. exit 962
  212. fi
  213. echo $"Restarting database"
  214. systemctl restart mariadb
  215. echo $"Ensure MariaDB handles authentication"
  216. MARIADB_PASSWORD=$(${PROJECT_NAME}-pass -u root -a mariadb)
  217. mariadb_fix_authentication
  218. DATABASE_PASSWORD=$(${PROJECT_NAME}-pass -u root -a mariadb)
  219. fi
  220. rm -rf $temp_restore_dir
  221. fi
  222. }
  223. function restore_letsencrypt {
  224. if [[ $RESTORE_APP != 'all' ]]; then
  225. if [[ $RESTORE_APP != 'letsencrypt' ]]; then
  226. return
  227. fi
  228. fi
  229. if [ -d $USB_MOUNT/backup/letsencrypt ]; then
  230. echo $"Restoring Lets Encrypt settings"
  231. restore_directory_from_usb / letsencrypt
  232. chgrp -R ssl-cert /etc/letsencrypt
  233. chmod -R g=rX /etc/letsencrypt
  234. fi
  235. }
  236. function restore_passwordstore {
  237. if [[ $RESTORE_APP != 'all' ]]; then
  238. if [[ $RESTORE_APP != 'passwords' ]]; then
  239. return
  240. fi
  241. fi
  242. if [ -d $USB_MOUNT/backup/passwordstore ]; then
  243. store_original_mariadb_password
  244. echo $"Restoring password store"
  245. restore_directory_from_usb / passwordstore
  246. fi
  247. }
  248. function restore_tor {
  249. if [[ $RESTORE_APP != 'all' ]]; then
  250. if [[ $RESTORE_APP != 'tor' ]]; then
  251. return
  252. fi
  253. fi
  254. if [ -d $USB_MOUNT/backup/tor ]; then
  255. echo $"Restoring Tor settings"
  256. restore_directory_from_usb / tor
  257. fi
  258. }
  259. function restore_mutt_settings {
  260. if [[ $RESTORE_APP != 'all' ]]; then
  261. if [[ $RESTORE_APP != 'mutt' ]]; then
  262. return
  263. fi
  264. fi
  265. if [ -d $USB_MOUNT/backup/mutt ]; then
  266. for d in $USB_MOUNT/backup/mutt/*/ ; do
  267. USERNAME=$(echo "$d" | awk -F '/' '{print $6}')
  268. if [[ $(is_valid_user "$USERNAME") == "1" ]]; then
  269. if [ ! -d /home/$USERNAME ]; then
  270. ${PROJECT_NAME}-adduser $USERNAME
  271. fi
  272. echo $"Restoring Mutt settings for $USERNAME"
  273. temp_restore_dir=/root/tempmutt
  274. restore_directory_from_usb $temp_restore_dir mutt/$USERNAME
  275. if [ -f $temp_restore_dir/home/$USERNAME/tempbackup/.muttrc ]; then
  276. cp -f $temp_restore_dir/home/$USERNAME/tempbackup/.muttrc /home/$USERNAME/.muttrc
  277. sed -i '/set sidebar_delim/d' /home/$USERNAME/.muttrc
  278. sed -i '/set sidebar_sort/d' /home/$USERNAME/.muttrc
  279. fi
  280. if [ -f $temp_restore_dir/home/$USERNAME/tempbackup/Muttrc ]; then
  281. cp -f $temp_restore_dir/home/$USERNAME/tempbackup/Muttrc /etc/Muttrc
  282. sed -i '/set sidebar_delim/d' /etc/Muttrc
  283. sed -i '/set sidebar_sort/d' /etc/Muttrc
  284. fi
  285. if [ ! "$?" = "0" ]; then
  286. rm -rf $temp_restore_dir
  287. set_user_permissions
  288. backup_unmount_drive
  289. exit 276
  290. fi
  291. rm -rf $temp_restore_dir
  292. fi
  293. done
  294. fi
  295. }
  296. function restore_gpg {
  297. if [[ $RESTORE_APP != 'gpg' ]]; then
  298. return
  299. fi
  300. if [ -d $USB_MOUNT/backup/gnupg ]; then
  301. for d in $USB_MOUNT/backup/gnupg/*/ ; do
  302. USERNAME=$(echo "$d" | awk -F '/' '{print $6}')
  303. if [[ $(is_valid_user "$USERNAME") == "1" ]]; then
  304. if [ ! -d /home/$USERNAME ]; then
  305. ${PROJECT_NAME}-adduser $USERNAME
  306. fi
  307. echo $"Restoring gnupg settings for $USERNAME"
  308. temp_restore_dir=/root/tempgnupg
  309. restore_directory_from_usb $temp_restore_dir gnupg/$USERNAME
  310. cp -r $temp_restore_dir/home/$USERNAME/.gnupg /home/$USERNAME/
  311. if [ ! "$?" = "0" ]; then
  312. rm -rf $temp_restore_dir
  313. set_user_permissions
  314. backup_unmount_drive
  315. exit 276
  316. fi
  317. rm -rf $temp_restore_dir
  318. gpg_set_permissions $USERNAME
  319. if [[ "$USERNAME" == "$ADMIN_USERNAME" ]]; then
  320. cp -r /home/$USERNAME/.gnupg /root
  321. if [ ! "$?" = "0" ]; then
  322. set_user_permissions
  323. backup_unmount_drive
  324. exit 283
  325. fi
  326. gpg_set_permissions root
  327. fi
  328. fi
  329. done
  330. fi
  331. }
  332. function restore_procmail {
  333. if [[ $RESTORE_APP != 'all' ]]; then
  334. if [[ $RESTORE_APP != 'procmail' ]]; then
  335. return
  336. fi
  337. fi
  338. if [ -d $USB_MOUNT/backup/procmail ]; then
  339. for d in $USB_MOUNT/backup/procmail/*/ ; do
  340. USERNAME=$(echo "$d" | awk -F '/' '{print $6}')
  341. if [[ $(is_valid_user "$USERNAME") == "1" ]]; then
  342. if [ ! -d /home/$USERNAME ]; then
  343. ${PROJECT_NAME}-adduser $USERNAME
  344. fi
  345. echo $"Restoring procmail settings for $USERNAME"
  346. temp_restore_dir=/root/tempprocmail
  347. restore_directory_from_usb $temp_restore_dir procmail/$USERNAME
  348. if [ -d $temp_restore_dir ]; then
  349. cp -f $temp_restore_dir/home/$USERNAME/tempbackup/.procmailrc /home/$USERNAME/
  350. if [ ! "$?" = "0" ]; then
  351. rm -rf $temp_restore_dir
  352. set_user_permissions
  353. backup_unmount_drive
  354. exit 276
  355. fi
  356. rm -rf $temp_restore_dir
  357. fi
  358. fi
  359. done
  360. fi
  361. }
  362. function restore_spamassassin {
  363. if [[ $RESTORE_APP != 'all' ]]; then
  364. if [[ $RESTORE_APP != 'spamassassin' ]]; then
  365. return
  366. fi
  367. fi
  368. if [ -d $USB_MOUNT/backup/spamassassin ]; then
  369. for d in $USB_MOUNT/backup/spamassassin/*/ ; do
  370. USERNAME=$(echo "$d" | awk -F '/' '{print $6}')
  371. if [[ $(is_valid_user "$USERNAME") == "1" ]]; then
  372. if [ -d $USB_MOUNT/backup/spamassassin/$USERNAME ]; then
  373. if [ ! -d /home/$USERNAME ]; then
  374. ${PROJECT_NAME}-adduser $USERNAME
  375. fi
  376. echo $"Restoring spamassassin settings for $USERNAME"
  377. temp_restore_dir=/root/tempspamassassin
  378. restore_directory_from_usb $temp_restore_dir spamassassin/$USERNAME
  379. cp -rf $temp_restore_dir/home/$USERNAME/.spamassassin /home/$USERNAME/
  380. if [ ! "$?" = "0" ]; then
  381. rm -rf $temp_restore_dir
  382. set_user_permissions
  383. backup_unmount_drive
  384. exit 276
  385. fi
  386. rm -rf $temp_restore_dir
  387. fi
  388. fi
  389. done
  390. fi
  391. }
  392. function restore_admin_readme {
  393. if [[ $RESTORE_APP != 'all' ]]; then
  394. if [[ $RESTORE_APP != 'readme' ]]; then
  395. return
  396. fi
  397. fi
  398. if [ -d $USB_MOUNT/backup/readme ]; then
  399. echo $"Restoring admin user README"
  400. # Make a backup of the original README file
  401. # incase old passwords need to be used
  402. if [ -f /home/$ADMIN_USERNAME/README ]; then
  403. if [ ! -f /home/$ADMIN_USERNAME/README_original ]; then
  404. cp /home/$ADMIN_USERNAME/README /home/$ADMIN_USERNAME/README_original
  405. fi
  406. fi
  407. temp_restore_dir=/root/tempreadme
  408. restore_directory_from_usb $temp_restore_dir readme
  409. cp -f $temp_restore_dir/home/$ADMIN_USERNAME/tempbackup/README /home/$ADMIN_USERNAME/
  410. if [ ! "$?" = "0" ]; then
  411. rm -rf $temp_restore_dir
  412. set_user_permissions
  413. backup_unmount_drive
  414. exit 276
  415. fi
  416. rm -rf $temp_restore_dir
  417. fi
  418. }
  419. function restore_user_ssh_keys {
  420. if [[ $RESTORE_APP != 'all' ]]; then
  421. if [[ $RESTORE_APP != 'ssh' ]]; then
  422. return
  423. fi
  424. fi
  425. if [ -d $USB_MOUNT/backup/ssh ]; then
  426. for d in $USB_MOUNT/backup/ssh/*/ ; do
  427. USERNAME=$(echo "$d" | awk -F '/' '{print $6}')
  428. if [[ $(is_valid_user "$USERNAME") == "1" ]]; then
  429. if [ ! -d /home/$USERNAME ]; then
  430. ${PROJECT_NAME}-adduser $USERNAME
  431. fi
  432. echo $"Restoring ssh keys for $USERNAME"
  433. temp_restore_dir=/root/tempssh
  434. restore_directory_from_usb $temp_restore_dir ssh/$USERNAME
  435. cp -r $temp_restore_dir/home/$USERNAME/.ssh /home/$USERNAME/
  436. if [ ! "$?" = "0" ]; then
  437. rm -rf $temp_restore_dir
  438. set_user_permissions
  439. backup_unmount_drive
  440. exit 664
  441. fi
  442. rm -rf $temp_restore_dir
  443. fi
  444. done
  445. fi
  446. }
  447. function restore_user_config {
  448. if [[ $RESTORE_APP != 'all' ]]; then
  449. if [[ $RESTORE_APP != 'userconfig' ]]; then
  450. return
  451. fi
  452. fi
  453. if [ -d $USB_MOUNT/backup/config ]; then
  454. for d in $USB_MOUNT/backup/config/*/ ; 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 config files for $USERNAME"
  461. temp_restore_dir=/root/tempconfig
  462. restore_directory_from_usb $temp_restore_dir config/$USERNAME
  463. cp -r $temp_restore_dir/home/$USERNAME/.config /home/$USERNAME/
  464. if [ ! "$?" = "0" ]; then
  465. rm -rf $temp_restore_dir
  466. set_user_permissions
  467. backup_unmount_drive
  468. exit 664
  469. fi
  470. rm -rf $temp_restore_dir
  471. fi
  472. done
  473. fi
  474. }
  475. function restore_user_monkeysphere {
  476. if [[ $RESTORE_APP != 'all' ]]; then
  477. if [[ $RESTORE_APP != 'usermonkeysphere' ]]; then
  478. return
  479. fi
  480. fi
  481. if [ -d $USB_MOUNT/backup/monkeysphere ]; then
  482. for d in $USB_MOUNT/backup/monkeysphere/*/ ; do
  483. USERNAME=$(echo "$d" | awk -F '/' '{print $6}')
  484. if [[ $(is_valid_user "$USERNAME") == "1" ]]; then
  485. if [ ! -d /home/$USERNAME ]; then
  486. ${PROJECT_NAME}-adduser $USERNAME
  487. fi
  488. echo $"Restoring monkeysphere ids for $USERNAME"
  489. temp_restore_dir=/root/tempmonkeysphere
  490. restore_directory_from_usb $temp_restore_dir monkeysphere/$USERNAME
  491. cp -r $temp_restore_dir/home/$USERNAME/.monkeysphere /home/$USERNAME/
  492. if [ ! "$?" = "0" ]; then
  493. rm -rf $temp_restore_dir
  494. set_user_permissions
  495. backup_unmount_drive
  496. exit 664
  497. fi
  498. rm -rf $temp_restore_dir
  499. fi
  500. done
  501. # The admin user is the identity certifier
  502. MY_EMAIL_ADDRESS="${ADMIN_USERNAME}@${HOSTNAME}"
  503. read_config_param MY_EMAIL_ADDRESS
  504. MY_GPG_PUBLIC_KEY_ID=$(gpg_pubkey_from_email "$ADMIN_USERNAME" "$MY_EMAIL_ADDRESS")
  505. fpr=$(gpg --with-colons --fingerprint $MY_GPG_PUBLIC_KEY_ID | grep fpr | head -n 1 | awk -F ':' '{print $10}')
  506. monkeysphere-authentication add-identity-certifier $fpr
  507. monkeysphere-authentication update-users
  508. fi
  509. }
  510. function restore_user_fin {
  511. if [[ $RESTORE_APP != 'all' ]]; then
  512. if [[ $RESTORE_APP != 'userfin' ]]; then
  513. return
  514. fi
  515. fi
  516. if [ -d $USB_MOUNT/backup/fin ]; then
  517. for d in $USB_MOUNT/backup/fin/*/ ; do
  518. USERNAME=$(echo "$d" | awk -F '/' '{print $6}')
  519. if [[ $(is_valid_user "$USERNAME") == "1" ]]; then
  520. if [ ! -d /home/$USERNAME ]; then
  521. ${PROJECT_NAME}-adduser $USERNAME
  522. fi
  523. echo $"Restoring fin files for $USERNAME"
  524. temp_restore_dir=/root/tempfin
  525. restore_directory_from_usb $temp_restore_dir fin/$USERNAME
  526. cp -r $temp_restore_dir/home/$USERNAME/.fin /home/$USERNAME/
  527. if [ ! "$?" = "0" ]; then
  528. rm -rf $temp_restore_dir
  529. set_user_permissions
  530. backup_unmount_drive
  531. exit 664
  532. fi
  533. rm -rf $temp_restore_dir
  534. fi
  535. done
  536. fi
  537. }
  538. function restore_user_local {
  539. if [[ $RESTORE_APP != 'all' ]]; then
  540. if [[ $RESTORE_APP != 'userlocal' ]]; then
  541. return
  542. fi
  543. fi
  544. if [ -d $USB_MOUNT/backup/local ]; then
  545. for d in $USB_MOUNT/backup/local/*/ ; do
  546. USERNAME=$(echo "$d" | awk -F '/' '{print $6}')
  547. if [[ $(is_valid_user "$USERNAME") == "1" ]]; then
  548. if [ ! -d /home/$USERNAME ]; then
  549. ${PROJECT_NAME}-adduser $USERNAME
  550. fi
  551. echo $"Restoring local files for $USERNAME"
  552. temp_restore_dir=/root/templocal
  553. restore_directory_from_usb $temp_restore_dir local/$USERNAME
  554. cp -r $temp_restore_dir/home/$USERNAME/.local /home/$USERNAME/
  555. if [ ! "$?" = "0" ]; then
  556. rm -rf $temp_restore_dir
  557. set_user_permissions
  558. backup_unmount_drive
  559. exit 664
  560. fi
  561. rm -rf $temp_restore_dir
  562. fi
  563. done
  564. fi
  565. }
  566. function restore_certs {
  567. if [[ $RESTORE_APP != 'all' ]]; then
  568. if [[ $RESTORE_APP != 'certs' ]]; then
  569. return
  570. fi
  571. fi
  572. if [ -d $USB_MOUNT/backup/ssl ]; then
  573. echo $"Restoring certificates"
  574. mkdir /root/tempssl
  575. restore_directory_from_usb /root/tempssl ssl
  576. cp -r /root/tempssl/etc/ssl/* /etc/ssl
  577. if [ ! "$?" = "0" ]; then
  578. set_user_permissions
  579. backup_unmount_drive
  580. exit 276
  581. fi
  582. rm -rf /root/tempssl
  583. # restore ownership
  584. if [ -f /etc/ssl/private/xmpp.key ]; then
  585. chown prosody:prosody /etc/ssl/private/xmpp.key
  586. chown prosody:prosody /etc/ssl/certs/xmpp.*
  587. fi
  588. if [ -d /etc/dovecot ]; then
  589. chown root:dovecot /etc/ssl/private/dovecot.*
  590. chown root:dovecot /etc/ssl/certs/dovecot.*
  591. fi
  592. if [ -f /etc/ssl/private/exim.key ]; then
  593. cp /etc/ssl/private/exim.key /etc/exim4
  594. cp /etc/ssl/certs/exim.crt /etc/exim4
  595. cp /etc/ssl/certs/exim.dhparam /etc/exim4
  596. chown root:Debian-exim /etc/exim4/exim.key /etc/exim4/exim.crt /etc/exim4/exim.dhparam
  597. chmod 640 /etc/exim4/exim.key /etc/exim4/exim.crt /etc/exim4/exim.dhparam
  598. fi
  599. if [ -f /etc/ssl/private/mumble.key ]; then
  600. if [ -d /var/lib/mumble-server ]; then
  601. cp /etc/ssl/certs/mumble.* /var/lib/mumble-server
  602. cp /etc/ssl/private/mumble.key /var/lib/mumble-server
  603. chown -R mumble-server:mumble-server /var/lib/mumble-server
  604. fi
  605. fi
  606. fi
  607. }
  608. function restore_personal_settings {
  609. if [[ $RESTORE_APP != 'all' ]]; then
  610. if [[ $RESTORE_APP != 'personal' ]]; then
  611. return
  612. fi
  613. fi
  614. if [ -d $USB_MOUNT/backup/personal ]; then
  615. for d in $USB_MOUNT/backup/personal/*/ ; do
  616. USERNAME=$(echo "$d" | awk -F '/' '{print $6}')
  617. if [[ $(is_valid_user "$USERNAME") == "1" ]]; then
  618. if [ -d $USB_MOUNT/backup/personal/$USERNAME ]; then
  619. if [ ! -d /home/$USERNAME ]; then
  620. ${PROJECT_NAME}-adduser $USERNAME
  621. fi
  622. echo $"Restoring personal settings for $USERNAME"
  623. temp_restore_dir=/root/temppersonal
  624. restore_directory_from_usb $temp_restore_dir personal/$USERNAME
  625. if [ -d /home/$USERNAME/personal ]; then
  626. rm -rf /home/$USERNAME/personal
  627. fi
  628. mv $temp_restore_dir/home/$USERNAME/personal /home/$USERNAME
  629. if [ ! "$?" = "0" ]; then
  630. set_user_permissions
  631. backup_unmount_drive
  632. exit 184
  633. fi
  634. rm -rf $temp_restore_dir
  635. fi
  636. fi
  637. done
  638. fi
  639. }
  640. function restore_mailing_list {
  641. if [[ $RESTORE_APP != 'all' ]]; then
  642. if [[ $RESTORE_APP != 'mailinglist' ]]; then
  643. return
  644. fi
  645. fi
  646. if [ -d /var/spool/mlmmj ]; then
  647. echo $"Restoring public mailing list"
  648. temp_restore_dir=/root/tempmailinglist
  649. restore_directory_from_usb $temp_restore_dir mailinglist
  650. cp -r $temp_restore_dir/root/spool/mlmmj/* /var/spool/mlmmj
  651. if [ ! "$?" = "0" ]; then
  652. set_user_permissions
  653. backup_unmount_drive
  654. exit 526
  655. fi
  656. rm -rf $temp_restore_dir
  657. fi
  658. }
  659. function restore_email {
  660. if [[ $RESTORE_APP != 'all' ]]; then
  661. if [[ $RESTORE_APP != 'email' ]]; then
  662. return
  663. fi
  664. fi
  665. if [ -d $USB_MOUNT/backup/mail ]; then
  666. for d in $USB_MOUNT/backup/mail/*/ ; do
  667. USERNAME=$(echo "$d" | awk -F '/' '{print $6}')
  668. if [[ $(is_valid_user "$USERNAME") == "1" ]]; then
  669. if [ ! -d /home/$USERNAME ]; then
  670. ${PROJECT_NAME}-adduser $USERNAME
  671. fi
  672. echo $"Restoring emails for $USERNAME"
  673. temp_restore_dir=/root/tempmail
  674. restore_directory_from_usb $temp_restore_dir mail/$USERNAME
  675. if [ ! -d /home/$USERNAME/Maildir ]; then
  676. mkdir /home/$USERNAME/Maildir
  677. fi
  678. tar -xzvf $temp_restore_dir/root/tempbackupemail/$USERNAME/maildir.tar.gz -C /
  679. if [ ! "$?" = "0" ]; then
  680. set_user_permissions
  681. backup_unmount_drive
  682. exit 927
  683. fi
  684. rm -rf $temp_restore_dir
  685. fi
  686. done
  687. fi
  688. }
  689. function get_restore_app {
  690. if [ ${1} ]; then
  691. if [ ! -d /home/${1} ]; then
  692. RESTORE_APP=${1}
  693. echo $"Restore $RESTORE_APP"
  694. fi
  695. fi
  696. }
  697. get_restore_app ${2}
  698. backup_mount_drive ${1} ${ADMIN_USERNAME} ${2}
  699. check_backup_exists
  700. check_admin_user
  701. copy_gpg_keys
  702. restore_blocklist
  703. restore_configfiles
  704. same_admin_user
  705. restore_passwordstore
  706. restore_mariadb
  707. restore_letsencrypt
  708. restore_tor
  709. restore_mutt_settings
  710. restore_gpg
  711. restore_procmail
  712. restore_spamassassin
  713. restore_admin_readme
  714. restore_user_ssh_keys
  715. restore_user_config
  716. restore_user_monkeysphere
  717. restore_user_fin
  718. restore_user_local
  719. restore_certs
  720. restore_personal_settings
  721. restore_mailing_list
  722. restore_email
  723. restore_apps local $RESTORE_APP
  724. set_user_permissions
  725. update_default_domain
  726. backup_unmount_drive
  727. # ensure that all TLS certificates are pinned
  728. #${PROJECT_NAME}-pin-cert all
  729. echo $"Restore from USB drive is complete. You can now unplug it."
  730. exit 0