freedombone-restore-local 26KB

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