freedombone-restore-local 26KB

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