freedombone-restore-local 25KB

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