freedombone-restore-local 25KB

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