freedombone-restore-local 31KB

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