freedombone-restore-local 25KB

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