freedombone-restore-local 34KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044
  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 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 General Public License for more details.
  26. #
  27. # You should have received a copy of the GNU 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. USB_DRIVE=/dev/sdb1
  37. USB_MOUNT=/mnt/usb
  38. # get default USB from config file
  39. CONFIG_FILE=$HOME/${PROJECT_NAME}.cfg
  40. if [ -f $CONFIG_FILE ]; then
  41. if grep -q "USB_DRIVE=" $CONFIG_FILE; then
  42. USB_DRIVE=$(cat $CONFIG_FILE | grep "USB_DRIVE=" | awk -F '=' '{print $2}')
  43. fi
  44. fi
  45. ADMIN_USERNAME=
  46. ADMIN_NAME=
  47. # MariaDB password
  48. DATABASE_PASSWORD=$(cat /root/dbpass)
  49. MICROBLOG_DOMAIN_NAME=
  50. HUBZILLA_DOMAIN_NAME=
  51. OWNCLOUD_DOMAIN_NAME=
  52. GIT_DOMAIN_NAME=
  53. WIKI_DOMAIN_NAME=
  54. FULLBLOG_DOMAIN_NAME=
  55. function mount_drive {
  56. if [ $1 ]; then
  57. USB_DRIVE=/dev/${1}1
  58. fi
  59. # get the admin user
  60. ADMIN_USERNAME=$(cat $COMPLETION_FILE | grep "Admin user" | awk -F ':' '{print $2}')
  61. if [ $2 ]; then
  62. ADMIN_USERNAME=$2
  63. fi
  64. ADMIN_NAME=$(getent passwd $ADMIN_USERNAME | cut -d: -f5 | cut -d, -f1)
  65. # check that the backup destination is available
  66. if [ ! -b $USB_DRIVE ]; then
  67. echo $"Please attach a USB drive"
  68. exit 1
  69. fi
  70. # unmount if already mounted
  71. umount -f $USB_MOUNT
  72. if [ ! -d $USB_MOUNT ]; then
  73. mkdir $USB_MOUNT
  74. fi
  75. if [ -f /dev/mapper/encrypted_usb ]; then
  76. rm -rf /dev/mapper/encrypted_usb
  77. fi
  78. cryptsetup luksClose encrypted_usb
  79. # mount the encrypted backup drive
  80. cryptsetup luksOpen $USB_DRIVE encrypted_usb
  81. if [ "$?" = "0" ]; then
  82. USB_DRIVE=/dev/mapper/encrypted_usb
  83. fi
  84. mount $USB_DRIVE $USB_MOUNT
  85. if [ ! "$?" = "0" ]; then
  86. echo $"There was a problem mounting the USB drive to $USB_MOUNT"
  87. rm -rf $USB_MOUNT
  88. exit 2
  89. fi
  90. }
  91. function unmount_drive {
  92. sync
  93. umount $USB_MOUNT
  94. if [ ! "$?" = "0" ]; then
  95. echo $"Unable to unmount the drive. This means that the backup did not work"
  96. rm -rf $USB_MOUNT
  97. exit 9
  98. fi
  99. rm -rf $USB_MOUNT
  100. echo $"Setting permissions"
  101. for d in /home/*/ ; do
  102. USERNAME=$(echo "$d" | awk -F '/' '{print $3}')
  103. if [[ $USERNAME != "git" ]]; then
  104. chown -R $USERNAME:$USERNAME /home/$USERNAME
  105. fi
  106. done
  107. if [[ $USB_DRIVE == /dev/mapper/encrypted_usb ]]; then
  108. echo $"Unmount encrypted USB"
  109. cryptsetup luksClose encrypted_usb
  110. fi
  111. if [ -f /dev/mapper/encrypted_usb ]; then
  112. rm -rf /dev/mapper/encrypted_usb
  113. fi
  114. }
  115. function check_backup_exists {
  116. if [ ! -d $USB_MOUNT/backup ]; then
  117. echo $"No backup directory found on the USB drive."
  118. unmount_drive
  119. exit 2
  120. fi
  121. }
  122. function check_admin_user {
  123. echo $"Checking that admin user exists"
  124. if [ ! -d /home/$ADMIN_USERNAME ]; then
  125. echo $"Username $ADMIN_USERNAME not found. Reinstall ${PROJECT_NAME} with this username."
  126. unmount_drive
  127. exit 295
  128. fi
  129. }
  130. function copy_gpg_keys {
  131. echo $"Copying GPG keys from admin user to root"
  132. cp -r /home/$ADMIN_USERNAME/.gnupg /root
  133. }
  134. function restore_directory_from_usb {
  135. if [ ! -d ${1} ]; then
  136. mkdir ${1}
  137. fi
  138. obnam restore -r $USB_MOUNT/backup/${2} --to ${1}
  139. }
  140. function restore_database {
  141. RESTORE_SUBDIR="root"
  142. if [ -d $USB_MOUNT/backup/${1} ]; then
  143. echo $"Restoring ${1} database"
  144. restore_directory_from_usb "/root/temp${1}data" "${1}data"
  145. if [ ! -f /root/temp${1}data/${RESTORE_SUBDIR}/temp${1}data/${1}.sql ]; then
  146. echo $"Unable to restore ${1} database"
  147. rm -rf /root/temp${1}data
  148. unmount_drive
  149. exit 503
  150. fi
  151. mysqlsuccess=$(mysql -u root --password=$DATABASE_PASSWORD ${1} -o < /root/temp${1}data/${RESTORE_SUBDIR}/temp${1}data/${1}.sql)
  152. if [ ! "$?" = "0" ]; then
  153. echo "$mysqlsuccess"
  154. unmount_drive
  155. exit 964
  156. fi
  157. shred -zu /root/temp${1}data/${RESTORE_SUBDIR}/temp${1}data/*
  158. rm -rf /root/temp${1}data
  159. echo $"Restoring ${1} installation"
  160. if [ ! -d /root/temp${1} ]; then
  161. mkdir /root/temp${1}
  162. fi
  163. restore_directory_from_usb "/root/temp${1}" "${1}"
  164. RESTORE_SUBDIR="var"
  165. if [ ${2} ]; then
  166. if [ -d /var/www/${2}/htdocs ]; then
  167. if [ -d /root/temp${1}/${RESTORE_SUBDIR}/www/${2}/htdocs ]; then
  168. rm -rf /var/www/${2}/htdocs
  169. mv /root/temp${1}/${RESTORE_SUBDIR}/www/${2}/htdocs /var/www/${2}/
  170. if [ ! "$?" = "0" ]; then
  171. unmount_drive
  172. exit 683
  173. fi
  174. if [ -d /etc/letsencrypt/live/${2} ]; then
  175. ln -s /etc/letsencrypt/live/${2}/privkey.pem /etc/ssl/private/${2}.key
  176. ln -s /etc/letsencrypt/live/${2}/fullchain.pem /etc/ssl/certs/${2}.pem
  177. else
  178. # Ensure that the bundled SSL cert is being used
  179. if [ -f /etc/ssl/certs/${2}.bundle.crt ]; then
  180. sed -i "s|${2}.crt|${2}.bundle.crt|g" /etc/nginx/sites-available/${2}
  181. fi
  182. fi
  183. fi
  184. fi
  185. fi
  186. fi
  187. }
  188. function update_domains {
  189. if grep -q "GNU Social domain" $COMPLETION_FILE; then
  190. MICROBLOG_DOMAIN_NAME=$(cat $COMPLETION_FILE | grep "GNU Social domain" | awk -F ':' '{print $2}')
  191. fi
  192. if grep -q "Hubzilla domain" $COMPLETION_FILE; then
  193. HUBZILLA_DOMAIN_NAME=$(cat $COMPLETION_FILE | grep "Hubzilla domain" | awk -F ':' '{print $2}')
  194. fi
  195. if grep -q "Owncloud domain" $COMPLETION_FILE; then
  196. OWNCLOUD_DOMAIN_NAME=$(cat $COMPLETION_FILE | grep "Owncloud domain" | awk -F ':' '{print $2}')
  197. fi
  198. if grep -q "Gogs domain" $COMPLETION_FILE; then
  199. GIT_DOMAIN_NAME=$(cat $COMPLETION_FILE | grep "Gogs domain" | awk -F ':' '{print $2}')
  200. fi
  201. if [ -d $USB_MOUNT/backup/wiki ]; then
  202. WIKI_DOMAIN_NAME=$(cat $COMPLETION_FILE | grep "Wiki domain" | awk -F ':' '{print $2}')
  203. fi
  204. if [ -d $USB_MOUNT/backup/blog ]; then
  205. FULLBLOG_DOMAIN_NAME=$(cat $COMPLETION_FILE | grep "Blog domain" | awk -F ':' '{print $2}')
  206. fi
  207. }
  208. function restore_configuration {
  209. if [[ $RESTORE_APP != 'all' ]]; then
  210. if [[ $RESTORE_APP != 'configuration' ]]; then
  211. return
  212. fi
  213. fi
  214. # this restores *.cfg and COMPLETION_FILE
  215. if [ -d $USB_MOUNT/backup/config ]; then
  216. echo $"Restoring configuration files"
  217. restore_directory_from_usb /root/tempconfig config
  218. cp -f /root/tempconfig/root/${PROJECT_NAME}.cfg $CONFIG_FILE
  219. if [ ! "$?" = "0" ]; then
  220. unmount_drive
  221. rm -rf /root/tempconfig
  222. exit 5294
  223. fi
  224. if [ -f $CONFIG_FILE ]; then
  225. # install according to the config file
  226. freedombone -c $CONFIG_FILE
  227. fi
  228. cp -f /root/tempconfig/root/${PROJECT_NAME}-completed.txt $COMPLETION_FILE
  229. if [ ! "$?" = "0" ]; then
  230. unmount_drive
  231. rm -rf /root/tempconfig
  232. exit 6382
  233. fi
  234. if [ -f /root/tempconfig${BACKUP_EXTRA_DIRECTORIES} ]; then
  235. cp -f /root/tempconfig${BACKUP_EXTRA_DIRECTORIES} ${BACKUP_EXTRA_DIRECTORIES}
  236. if [ ! "$?" = "0" ]; then
  237. unmount_drive
  238. rm -rf /root/tempconfig
  239. exit 62121
  240. fi
  241. fi
  242. rm -rf /root/tempconfig
  243. fi
  244. }
  245. function same_admin_user {
  246. PREV_ADMIN_USERNAME=$(cat $COMPLETION_FILE | grep "Admin user" | awk -F ':' '{print $2}')
  247. if [[ "$PREV_ADMIN_USERNAME" != "$ADMIN_USERNAME" ]]; then
  248. 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"
  249. unmount_drive
  250. exit 73265
  251. fi
  252. }
  253. function restore_mariadb {
  254. if [[ $RESTORE_APP != 'all' ]]; then
  255. if [[ $RESTORE_APP != 'mariadb' ]]; then
  256. return
  257. fi
  258. fi
  259. if [ -d $USB_MOUNT/backup/mariadb ]; then
  260. echo $"Restoring mysql settings"
  261. restore_directory_from_usb /root/tempmariadb mariadb
  262. echo $"Get the MariaDB password from the backup"
  263. if [ ! -f /root/tempmariadb/root/tempmariadb/db ]; then
  264. echo $"MariaDB password file not found"
  265. exit 495
  266. fi
  267. BACKUP_MARIADB_PASSWORD=$(cat /root/tempmariadb/root/tempmariadb/db)
  268. if [[ $BACKUP_MARIADB_PASSWORD != $DATABASE_PASSWORD ]]; then
  269. echo $"Restore the MariaDB user table"
  270. mysqlsuccess=$(mysql -u root --password=$DATABASE_PASSWORD mysql -o < /root/tempmariadb/root/tempmariadb/mysql.sql)
  271. if [ ! "$?" = "0" ]; then
  272. echo $"Try again using the password obtained from backup"
  273. mysqlsuccess=$(mysql -u root --password=$BACKUP_MARIADB_PASSWORD mysql -o < /root/tempmariadb/root/tempmariadb/mysql.sql)
  274. fi
  275. if [ ! "$?" = "0" ]; then
  276. echo "$mysqlsuccess"
  277. unmount_drive
  278. exit 962
  279. fi
  280. echo $"Restarting database"
  281. service mysql restart
  282. echo $"Change the MariaDB password to the backup version"
  283. DATABASE_PASSWORD=$BACKUP_MARIADB_PASSWORD
  284. fi
  285. shred -zu /root/tempmariadb/root/tempmariadb/db
  286. rm -rf /root/tempmariadb
  287. # Change database password file
  288. echo "$DATABASE_PASSWORD" > /root/dbpass
  289. chmod 600 /root/dbpass
  290. fi
  291. }
  292. function restore_letsencrypt {
  293. if [[ $RESTORE_APP != 'all' ]]; then
  294. if [[ $RESTORE_APP != 'letsencrypt' ]]; then
  295. return
  296. fi
  297. fi
  298. if [ -d $USB_MOUNT/backup/letsencrypt ]; then
  299. echo $"Restoring Lets Encrypt settings"
  300. restore_directory_from_usb / letsencrypt
  301. fi
  302. }
  303. function restore_tor {
  304. if [[ $RESTORE_APP != 'all' ]]; then
  305. if [[ $RESTORE_APP != 'tor' ]]; then
  306. return
  307. fi
  308. fi
  309. if [ -d $USB_MOUNT/backup/tor ]; then
  310. echo $"Restoring Tor settings"
  311. restore_directory_from_usb / tor
  312. fi
  313. }
  314. function restore_mutt_settings {
  315. if [[ $RESTORE_APP != 'all' ]]; then
  316. if [[ $RESTORE_APP != 'mutt' ]]; then
  317. return
  318. fi
  319. fi
  320. if [ -d $USB_MOUNT/backup/mutt ]; then
  321. for d in $USB_MOUNT/backup/mutt/*/ ; do
  322. USERNAME=$(echo "$d" | awk -F '/' '{print $6}')
  323. if [[ $USERNAME != "git" ]]; then
  324. if [ ! -d /home/$USERNAME ]; then
  325. ${PROJECT_NAME}-adduser $USERNAME
  326. fi
  327. echo $"Restoring Mutt settings for $USERNAME"
  328. restore_directory_from_usb /root/tempmutt mutt/$USERNAME
  329. if [ -f /root/tempmutt/home/$USERNAME/tempbackup/.muttrc ]; then
  330. cp -f /root/tempmutt/home/$USERNAME/tempbackup/.muttrc /home/$USERNAME/.muttrc
  331. fi
  332. if [ -f /root/tempmutt/home/$USERNAME/tempbackup/Muttrc ]; then
  333. cp -f /root/tempmutt/home/$USERNAME/tempbackup/Muttrc /etc/Muttrc
  334. fi
  335. if [ ! "$?" = "0" ]; then
  336. rm -rf /root/tempmutt
  337. unmount_drive
  338. exit 276
  339. fi
  340. rm -rf /root/tempmutt
  341. fi
  342. done
  343. fi
  344. }
  345. function restore_gpg {
  346. if [[ $RESTORE_APP != 'all' ]]; then
  347. if [[ $RESTORE_APP != 'gpg' ]]; then
  348. return
  349. fi
  350. fi
  351. if [ -d $USB_MOUNT/backup/gnupg ]; then
  352. for d in $USB_MOUNT/backup/gnupg/*/ ; do
  353. USERNAME=$(echo "$d" | awk -F '/' '{print $6}')
  354. if [[ $USERNAME != "git" ]]; then
  355. if [ ! -d /home/$USERNAME ]; then
  356. ${PROJECT_NAME}-adduser $USERNAME
  357. fi
  358. echo $"Restoring gnupg settings for $USERNAME"
  359. restore_directory_from_usb /root/tempgnupg gnupg/$USERNAME
  360. cp -r /root/tempgnupg/home/$USERNAME/.gnupg /home/$USERNAME/
  361. if [ ! "$?" = "0" ]; then
  362. rm -rf /root/tempgnupg
  363. unmount_drive
  364. exit 276
  365. fi
  366. rm -rf /root/tempgnupg
  367. if [[ "$USERNAME" == "$ADMIN_USERNAME" ]]; then
  368. cp -r /home/$USERNAME/.gnupg /root
  369. if [ ! "$?" = "0" ]; then
  370. unmount_drive
  371. exit 283
  372. fi
  373. fi
  374. fi
  375. done
  376. fi
  377. }
  378. function restore_procmail {
  379. if [[ $RESTORE_APP != 'all' ]]; then
  380. if [[ $RESTORE_APP != 'procmail' ]]; then
  381. return
  382. fi
  383. fi
  384. if [ -d $USB_MOUNT/backup/procmail ]; then
  385. for d in $USB_MOUNT/backup/procmail/*/ ; do
  386. USERNAME=$(echo "$d" | awk -F '/' '{print $6}')
  387. if [[ $USERNAME != "git" ]]; then
  388. if [ ! -d /home/$USERNAME ]; then
  389. ${PROJECT_NAME}-adduser $USERNAME
  390. fi
  391. echo $"Restoring procmail settings for $USERNAME"
  392. restore_directory_from_usb /root/tempprocmail procmail/$USERNAME
  393. cp -f /root/tempprocmail/home/$USERNAME/tempbackup/.procmailrc /home/$USERNAME/
  394. if [ ! "$?" = "0" ]; then
  395. rm -rf /root/tempprocmail
  396. unmount_drive
  397. exit 276
  398. fi
  399. rm -rf /root/tempprocmail
  400. fi
  401. done
  402. fi
  403. }
  404. function restore_spamassassin {
  405. if [[ $RESTORE_APP != 'all' ]]; then
  406. if [[ $RESTORE_APP != 'spamassassin' ]]; then
  407. return
  408. fi
  409. fi
  410. if [ -d $USB_MOUNT/backup/spamassassin ]; then
  411. for d in $USB_MOUNT/backup/spamassassin/*/ ; do
  412. USERNAME=$(echo "$d" | awk -F '/' '{print $6}')
  413. if [[ $USERNAME != "git" ]]; then
  414. if [ -d $USB_MOUNT/backup/spamassassin/$USERNAME ]; then
  415. if [ ! -d /home/$USERNAME ]; then
  416. ${PROJECT_NAME}-adduser $USERNAME
  417. fi
  418. echo $"Restoring spamassassin settings for $USERNAME"
  419. restore_directory_from_usb /root/tempspamassassin spamassassin/$USERNAME
  420. cp -rf /root/tempspamassassin/home/$USERNAME/.spamassassin /home/$USERNAME/
  421. if [ ! "$?" = "0" ]; then
  422. rm -rf /root/tempspamassassin
  423. unmount_drive
  424. exit 276
  425. fi
  426. rm -rf /root/tempspamassassin
  427. fi
  428. fi
  429. done
  430. fi
  431. }
  432. function restore_admin_readme {
  433. if [[ $RESTORE_APP != 'all' ]]; then
  434. if [[ $RESTORE_APP != 'readme' ]]; then
  435. return
  436. fi
  437. fi
  438. if [ -d $USB_MOUNT/backup/readme ]; then
  439. echo $"Restoring admin user README"
  440. # Make a backup of the original README file
  441. # incase old passwords need to be used
  442. if [ -f /home/$ADMIN_USERNAME/README ]; then
  443. if [ ! -f /home/$ADMIN_USERNAME/README_original ]; then
  444. cp /home/$ADMIN_USERNAME/README /home/$ADMIN_USERNAME/README_original
  445. fi
  446. fi
  447. restore_directory_from_usb /root/tempreadme readme
  448. cp -f /root/tempreadme/home/$ADMIN_USERNAME/tempbackup/README /home/$ADMIN_USERNAME/
  449. if [ ! "$?" = "0" ]; then
  450. rm -rf /root/tempreadme
  451. unmount_drive
  452. exit 276
  453. fi
  454. rm -rf /root/tempreadme
  455. fi
  456. }
  457. function restore_ipfs {
  458. if [[ $RESTORE_APP != 'all' ]]; then
  459. if [[ $RESTORE_APP != 'ipfs' ]]; then
  460. return
  461. fi
  462. fi
  463. if [ -d $USB_MOUNT/backup/ipfs ]; then
  464. echo $"Restoring IPFS"
  465. restore_directory_from_usb /root/tempipfs ipfs
  466. cp -rf /root/tempipfs/home/$ADMIN_USERNAME/.ipfs/* /home/$ADMIN_USERNAME/.ipfs
  467. if [ ! "$?" = "0" ]; then
  468. rm -rf /root/tempipfs
  469. unmount_drive
  470. exit 276
  471. fi
  472. rm -rf /root/tempipfs
  473. fi
  474. }
  475. function restore_user_ssh_keys {
  476. if [[ $RESTORE_APP != 'all' ]]; then
  477. if [[ $RESTORE_APP != 'ssh' ]]; then
  478. return
  479. fi
  480. fi
  481. if [ -d $USB_MOUNT/backup/ssh ]; then
  482. for d in $USB_MOUNT/backup/ssh/*/ ; do
  483. USERNAME=$(echo "$d" | awk -F '/' '{print $6}')
  484. if [[ $USERNAME != "git" ]]; then
  485. if [ ! -d /home/$USERNAME ]; then
  486. ${PROJECT_NAME}-adduser $USERNAME
  487. fi
  488. echo $"Restoring ssh keys for $USERNAME"
  489. restore_directory_from_usb /root/tempssh ssh/$USERNAME
  490. cp -r /root/tempssh/home/$USERNAME/.ssh /home/$USERNAME/
  491. if [ ! "$?" = "0" ]; then
  492. rm -rf /root/tempssh
  493. unmount_drive
  494. exit 664
  495. fi
  496. rm -rf /root/tempssh
  497. fi
  498. done
  499. fi
  500. }
  501. function restore_user_config {
  502. if [[ $RESTORE_APP != 'all' ]]; then
  503. if [[ $RESTORE_APP != 'userconfig' ]]; then
  504. return
  505. fi
  506. fi
  507. if [ -d $USB_MOUNT/backup/config ]; then
  508. for d in $USB_MOUNT/backup/config/*/ ; do
  509. USERNAME=$(echo "$d" | awk -F '/' '{print $6}')
  510. if [[ $USERNAME != "git" ]]; then
  511. if [ ! -d /home/$USERNAME ]; then
  512. ${PROJECT_NAME}-adduser $USERNAME
  513. fi
  514. echo $"Restoring config files for $USERNAME"
  515. restore_directory_from_usb /root/tempconfig config/$USERNAME
  516. cp -r /root/tempconfig/home/$USERNAME/.config /home/$USERNAME/
  517. if [ ! "$?" = "0" ]; then
  518. rm -rf /root/tempconfig
  519. unmount_drive
  520. exit 664
  521. fi
  522. rm -rf /root/tempconfig
  523. fi
  524. done
  525. fi
  526. }
  527. function restore_certs {
  528. if [[ $RESTORE_APP != 'all' ]]; then
  529. if [[ $RESTORE_APP != 'certs' ]]; then
  530. return
  531. fi
  532. fi
  533. if [ -d $USB_MOUNT/backup/ssl ]; then
  534. echo $"Restoring certificates"
  535. mkdir /root/tempssl
  536. restore_directory_from_usb /root/tempssl ssl
  537. cp -r /root/tempssl/etc/ssl/* /etc/ssl
  538. if [ ! "$?" = "0" ]; then
  539. unmount_drive
  540. exit 276
  541. fi
  542. rm -rf /root/tempssl
  543. # restore ownership
  544. if [ -f /etc/ssl/private/xmpp.key ]; then
  545. chown prosody:prosody /etc/ssl/private/xmpp.key
  546. chown prosody:prosody /etc/ssl/certs/xmpp.*
  547. fi
  548. if [ -d /etc/dovecot ]; then
  549. chown root:dovecot /etc/ssl/private/dovecot.*
  550. chown root:dovecot /etc/ssl/certs/dovecot.*
  551. fi
  552. if [ -f /etc/ssl/private/exim.key ]; then
  553. cp /etc/ssl/private/exim.key /etc/exim4
  554. cp /etc/ssl/certs/exim.crt /etc/exim4
  555. cp /etc/ssl/certs/exim.dhparam /etc/exim4
  556. chown root:Debian-exim /etc/exim4/exim.key /etc/exim4/exim.crt /etc/exim4/exim.dhparam
  557. chmod 640 /etc/exim4/exim.key /etc/exim4/exim.crt /etc/exim4/exim.dhparam
  558. fi
  559. if [ -f /etc/ssl/private/mumble.key ]; then
  560. if [ -d /var/lib/mumble-server ]; then
  561. cp /etc/ssl/certs/mumble.* /var/lib/mumble-server
  562. cp /etc/ssl/private/mumble.key /var/lib/mumble-server
  563. chown -R mumble-server:mumble-server /var/lib/mumble-server
  564. fi
  565. fi
  566. fi
  567. }
  568. function restore_personal_settings {
  569. if [[ $RESTORE_APP != 'all' ]]; then
  570. if [[ $RESTORE_APP != 'personal' ]]; then
  571. return
  572. fi
  573. fi
  574. if [ -d $USB_MOUNT/backup/personal ]; then
  575. for d in $USB_MOUNT/backup/personal/*/ ; do
  576. USERNAME=$(echo "$d" | awk -F '/' '{print $6}')
  577. if [[ $USERNAME != "git" ]]; then
  578. if [ -d $USB_MOUNT/backup/personal/$USERNAME ]; then
  579. if [ ! -d /home/$USERNAME ]; then
  580. ${PROJECT_NAME}-adduser $USERNAME
  581. fi
  582. echo $"Restoring personal settings for $USERNAME"
  583. restore_directory_from_usb /root/temppersonal personal/$USERNAME
  584. if [ -d /home/$USERNAME/personal ]; then
  585. rm -rf /home/$USERNAME/personal
  586. fi
  587. mv /root/temppersonal/home/$USERNAME/personal /home/$USERNAME
  588. if [ ! "$?" = "0" ]; then
  589. unmount_drive
  590. exit 184
  591. fi
  592. rm -rf /root/temppersonal
  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. restore_directory_from_usb /root/tempmailinglist mailinglist
  607. cp -r /root/tempmailinglist/root/spool/mlmmj/* /var/spool/mlmmj
  608. if [ ! "$?" = "0" ]; then
  609. unmount_drive
  610. exit 526
  611. fi
  612. rm -rf /root/tempmailinglist
  613. fi
  614. }
  615. function restore_xmpp {
  616. if [[ $RESTORE_APP != 'all' ]]; then
  617. if [[ $RESTORE_APP != 'xmpp' ]]; then
  618. return
  619. fi
  620. fi
  621. if [ -d /var/lib/prosody ]; then
  622. echo $"Restoring XMPP settings"
  623. restore_directory_from_usb /root/tempxmpp xmpp
  624. cp -r /root/tempxmpp/var/lib/prosody/* /var/lib/prosody
  625. if [ ! "$?" = "0" ]; then
  626. unmount_drive
  627. exit 725
  628. fi
  629. rm -rf /root/tempxmpp
  630. service prosody restart
  631. chown -R prosody:prosody /var/lib/prosody/*
  632. fi
  633. }
  634. function restore_gnu_social {
  635. if [[ $RESTORE_APP != 'all' ]]; then
  636. if [[ $RESTORE_APP != 'gnusocial' ]]; then
  637. return
  638. fi
  639. fi
  640. if [ $MICROBLOG_DOMAIN_NAME ]; then
  641. restore_database gnusocial ${MICROBLOG_DOMAIN_NAME}
  642. if [ -d /root/tempgnusocial ]; then
  643. rm -rf /root/tempgnusocial
  644. fi
  645. fi
  646. }
  647. function restore_hubzilla {
  648. if [[ $RESTORE_APP != 'all' ]]; then
  649. if [[ $RESTORE_APP != 'hubzilla' ]]; then
  650. return
  651. fi
  652. fi
  653. if [ $HUBZILLA_DOMAIN_NAME ]; then
  654. restore_database hubzilla ${HUBZILLA_DOMAIN_NAME}
  655. if [ -d $USB_MOUNT/backup/hubzilla ]; then
  656. if [ ! -d /var/www/${HUBZILLA_DOMAIN_NAME}/htdocs/store/[data]/smarty3 ]; then
  657. mkdir -p /var/www/${HUBZILLA_DOMAIN_NAME}/htdocs/store/[data]/smarty3
  658. fi
  659. chmod 777 /var/www/${HUBZILLA_DOMAIN_NAME}/htdocs/store/[data]/smarty3
  660. chown -R www-data:www-data /var/www/${HUBZILLA_DOMAIN_NAME}/htdocs/*
  661. if [ -d /root/temphubzilla ]; then
  662. rm -rf /root/temphubzilla
  663. fi
  664. fi
  665. fi
  666. }
  667. function restore_owncloud {
  668. if [[ $RESTORE_APP != 'all' ]]; then
  669. if [[ $RESTORE_APP != 'owncloud' ]]; then
  670. return
  671. fi
  672. fi
  673. if [ $OWNCLOUD_DOMAIN_NAME ]; then
  674. restore_database owncloud $OWNCLOUD_DOMAIN_NAME
  675. if [ -d $USB_MOUNT/backup/owncloud2 ]; then
  676. restore_directory_from_usb /root/tempowncloud2 owncloud2
  677. cp -r /root/tempowncloud2/etc/owncloud/* /etc/owncloud/
  678. if [ ! "$?" = "0" ]; then
  679. unmount_drive
  680. exit 982
  681. fi
  682. rm -rf /root/tempowncloud
  683. rm -rf /root/tempowncloud2
  684. chown -R www-data:www-data /var/lib/owncloud/data
  685. chown -R www-data:www-data /var/lib/owncloud/backup
  686. chown -R www-data:www-data /var/lib/owncloud/assets
  687. for d in /home/*/ ; do
  688. USERNAME=$(echo "$d" | awk -F '/' '{print $3}')
  689. if [[ $USERNAME != "git" ]]; then
  690. occ files:scan $USERNAME
  691. fi
  692. done
  693. ln -s /usr/share/owncloud /var/www/${OWNCLOUD_DOMAIN_NAME}/htdocs
  694. fi
  695. fi
  696. }
  697. function restore_gogs {
  698. if [[ $RESTORE_APP != 'all' ]]; then
  699. if [[ $RESTORE_APP != 'gogs' ]]; then
  700. return
  701. fi
  702. fi
  703. if [ $GIT_DOMAIN_NAME ]; then
  704. restore_database gogs ${GIT_DOMAIN_NAME}
  705. if [ -d $USB_MOUNT/backup/gogs ]; then
  706. echo $"Restoring Gogs settings"
  707. if [ ! -d /home/git/go/src/github.com/gogits/gogs/custom ]; then
  708. mkdir -p /home/git/go/src/github.com/gogits/gogs/custom
  709. fi
  710. cp -r /root/tempgogs/home/git/go/src/github.com/gogits/gogs/custom/* /home/git/go/src/github.com/gogits/gogs/custom
  711. if [ ! "$?" = "0" ]; then
  712. unmount_drive
  713. exit 981
  714. fi
  715. echo $"Restoring Gogs repos"
  716. restore_directory_from_usb /root/tempgogsrepos gogsrepos
  717. cp -r /root/tempgogsrepos/home/git/gogs-repositories/* /home/git/gogs-repositories/
  718. if [ ! "$?" = "0" ]; then
  719. unmount_drive
  720. exit 67574
  721. fi
  722. echo $"Restoring Gogs authorized_keys"
  723. restore_directory_from_usb /root/tempgogsssh gogsssh
  724. if [ ! -d /home/git/.ssh ]; then
  725. mkdir /home/git/.ssh
  726. fi
  727. cp -r /root/tempgogsssh/home/git/.ssh/* /home/git/.ssh/
  728. if [ ! "$?" = "0" ]; then
  729. unmount_drive
  730. exit 8463
  731. fi
  732. rm -rf /root/tempgogs
  733. rm -rf /root/tempgogsrepos
  734. rm -rf /root/tempgogsssh
  735. chown -R git:git /home/git
  736. fi
  737. fi
  738. }
  739. function restore_wiki {
  740. if [[ $RESTORE_APP != 'all' ]]; then
  741. if [[ $RESTORE_APP != 'wiki' ]]; then
  742. return
  743. fi
  744. fi
  745. if [ $WIKI_DOMAIN_NAME ]; then
  746. echo $"Restoring Wiki installation ${WIKI_DOMAIN_NAME}"
  747. restore_directory_from_usb /root/tempwiki wiki
  748. cp -r /root/tempwiki/var/lib/dokuwiki/* /var/lib/dokuwiki/
  749. if [ ! "$?" = "0" ]; then
  750. unmount_drive
  751. exit 868
  752. fi
  753. restore_directory_from_usb /root/tempwiki2 wiki2
  754. cp -r /root/tempwiki2/etc/dokuwiki/* /etc/dokuwiki/
  755. if [ ! "$?" = "0" ]; then
  756. unmount_drive
  757. exit 869
  758. fi
  759. rm -rf /root/tempwiki
  760. rm -rf /root/tempwiki2
  761. chown -R www-data:www-data /var/lib/dokuwiki/*
  762. # Ensure that the bundled SSL cert is being used
  763. if [ -f /etc/ssl/certs/${WIKI_DOMAIN_NAME}.bundle.crt ]; then
  764. sed -i "s|${WIKI_DOMAIN_NAME}.crt|${WIKI_DOMAIN_NAME}.bundle.crt|g" /etc/nginx/sites-available/${WIKI_DOMAIN_NAME}
  765. fi
  766. if [ -d /etc/letsencrypt/live/${WIKI_DOMAIN_NAME} ]; then
  767. ln -s /etc/letsencrypt/live/${WIKI_DOMAIN_NAME}/privkey.pem /etc/ssl/private/${WIKI_DOMAIN_NAME}.key
  768. ln -s /etc/letsencrypt/live/${WIKI_DOMAIN_NAME}/fullchain.pem /etc/ssl/certs/${WIKI_DOMAIN_NAME}.pem
  769. fi
  770. fi
  771. }
  772. function restore_blog {
  773. if [[ $RESTORE_APP != 'all' ]]; then
  774. if [[ $RESTORE_APP != 'blog' ]]; then
  775. return
  776. fi
  777. fi
  778. if [ $FULLBLOG_DOMAIN_NAME ]; then
  779. echo $"Restoring blog installation"
  780. restore_directory_from_usb /root/tempblog blog
  781. rm -rf /var/www/${FULLBLOG_DOMAIN_NAME}/htdocs
  782. cp -r /root/tempblog/var/www/${FULLBLOG_DOMAIN_NAME}/htdocs /var/www/${FULLBLOG_DOMAIN_NAME}/
  783. if [ ! "$?" = "0" ]; then
  784. unmount_drive
  785. exit 593
  786. fi
  787. rm -rf /root/tempblog
  788. if [ ! -d /var/www/${FULLBLOG_DOMAIN_NAME}/htdocs/content ]; then
  789. echo $"No content directory found after restoring blog"
  790. unmount_drive
  791. exit 287
  792. fi
  793. chown -R www-data:www-data /var/www/${FULLBLOG_DOMAIN_NAME}/htdocs
  794. # Ensure that the bundled SSL cert is being used
  795. if [ -f /etc/ssl/certs/${FULLBLOG_DOMAIN_NAME}.bundle.crt ]; then
  796. sed -i "s|${FULLBLOG_DOMAIN_NAME}.crt|${FULLBLOG_DOMAIN_NAME}.bundle.crt|g" /etc/nginx/sites-available/${FULLBLOG_DOMAIN_NAME}
  797. fi
  798. for d in /home/*/ ; do
  799. USERNAME=$(echo "$d" | awk -F '/' '{print $3}')
  800. if [[ $USERNAME != "git" ]]; then
  801. if [ -d /var/www/${FULLBLOG_DOMAIN_NAME}/htdocs/content/$USERNAME/blog/uncategorized/post ]; then
  802. mv /var/www/${FULLBLOG_DOMAIN_NAME}/htdocs/content/$USERNAME/blog/*.md /var/www/${FULLBLOG_DOMAIN_NAME}/htdocs/content/$USERNAME/blog/uncategorized/post
  803. fi
  804. fi
  805. done
  806. if [ -d /etc/letsencrypt/live/${FULLBLOG_DOMAIN_NAME} ]; then
  807. ln -s /etc/letsencrypt/live/${FULLBLOG_DOMAIN_NAME}/privkey.pem /etc/ssl/private/${FULLBLOG_DOMAIN_NAME}.key
  808. ln -s /etc/letsencrypt/live/${FULLBLOG_DOMAIN_NAME}/fullchain.pem /etc/ssl/certs/${FULLBLOG_DOMAIN_NAME}.pem
  809. fi
  810. fi
  811. }
  812. function restore_cjdns {
  813. if [[ $RESTORE_APP != 'all' ]]; then
  814. if [[ $RESTORE_APP != 'cjdns' ]]; then
  815. return
  816. fi
  817. fi
  818. if [ -d $USB_MOUNT/backup/cjdns ]; then
  819. echo $"Restoring cjdns installation"
  820. restore_directory_from_usb /root/tempcjdns cjdns
  821. rm -rf /etc/cjdns
  822. cp -r /root/tempcjdns/etc/cjdns /etc/
  823. if [ ! "$?" = "0" ]; then
  824. unmount_drive
  825. exit 8472
  826. fi
  827. rm -rf /root/tempcjdns
  828. fi
  829. }
  830. function restore_email {
  831. if [[ $RESTORE_APP != 'all' ]]; then
  832. if [[ $RESTORE_APP != 'email' ]]; then
  833. return
  834. fi
  835. fi
  836. if [ -d $USB_MOUNT/backup/mail ]; then
  837. for d in $USB_MOUNT/backup/mail/*/ ; do
  838. USERNAME=$(echo "$d" | awk -F '/' '{print $6}')
  839. if [[ $USERNAME != "git" ]]; then
  840. if [ ! -d /home/$USERNAME ]; then
  841. ${PROJECT_NAME}-adduser $USERNAME
  842. fi
  843. echo $"Restoring emails for $USERNAME"
  844. restore_directory_from_usb /root/tempmail mail/$USERNAME
  845. if [ ! -d /home/$USERNAME/Maildir ]; then
  846. mkdir /home/$USERNAME/Maildir
  847. fi
  848. tar -xzvf /root/tempmail/root/tempbackupemail/$USERNAME/maildir.tar.gz -C /
  849. if [ ! "$?" = "0" ]; then
  850. unmount_drive
  851. exit 927
  852. fi
  853. rm -rf /root/tempmail
  854. fi
  855. done
  856. fi
  857. }
  858. function restore_dlna {
  859. if [[ $RESTORE_APP != 'all' ]]; then
  860. if [[ $RESTORE_APP != 'dlna' ]]; then
  861. return
  862. fi
  863. fi
  864. if [ -d /var/cache/minidlna ]; then
  865. if [ -d $USB_MOUNT/backup/dlna ]; then
  866. echo $"Restoring DLNA cache"
  867. restore_directory_from_usb /root/tempdlna dlna
  868. cp -r /root/tempdlna/var/cache/minidlna/* /var/cache/minidlna/
  869. if [ ! "$?" = "0" ]; then
  870. rm -rf /root/tempdlna
  871. unmount_drive
  872. exit 982
  873. fi
  874. rm -rf /root/tempdlna
  875. fi
  876. fi
  877. }
  878. function restore_voip {
  879. if [[ $RESTORE_APP != 'all' ]]; then
  880. if [[ $RESTORE_APP != 'voip' ]]; then
  881. return
  882. fi
  883. fi
  884. if [ -d $USB_MOUNT/backup/voip ]; then
  885. echo $"Restoring VoIP settings"
  886. restore_directory_from_usb /root/tempvoip voip
  887. cp -f /root/tempvoip/home/$ADMIN_USERNAME/tempbackup/mumble-server.ini /etc/
  888. if [ ! "$?" = "0" ]; then
  889. rm -rf /root/tempvoip
  890. unmount_drive
  891. exit 3679
  892. fi
  893. cp -f /root/tempvoip/home/$ADMIN_USERNAME/tempbackup/sipwitch.conf /etc/sipwitch.conf
  894. if [ ! "$?" = "0" ]; then
  895. rm -rf /root/tempvoip
  896. unmount_drive
  897. exit 3679
  898. fi
  899. cp -f /root/tempvoip/home/$ADMIN_USERNAME/tempbackup/mumble-server.sqlite /var/lib/mumble-server/
  900. if [ ! "$?" = "0" ]; then
  901. rm -rf /root/tempvoip
  902. unmount_drive
  903. exit 276
  904. fi
  905. rm -rf /root/tempvoip
  906. cp /etc/ssl/certs/mumble* /var/lib/mumble-server
  907. cp /etc/ssl/private/mumble* /var/lib/mumble-server
  908. chown -R mumble-server:mumble-server /var/lib/mumble-server
  909. service sipwitch restart
  910. service mumble-server restart
  911. fi
  912. }
  913. function restore_tox {
  914. if [[ $RESTORE_APP != 'all' ]]; then
  915. if [[ $RESTORE_APP != 'tox' ]]; then
  916. return
  917. fi
  918. fi
  919. if [ -d $USB_MOUNT/backup/tox ]; then
  920. echo $"Restoring Tox node settings"
  921. restore_directory_from_usb / tox
  922. if [ ! "$?" = "0" ]; then
  923. unmount_drive
  924. exit 6393
  925. fi
  926. cp /var/lib/tox-bootstrapd/tox-bootstrapd.conf /etc/tox-bootstrapd.conf
  927. systemctl restart tox-bootstrapd.service
  928. if [ ! "$?" = "0" ]; then
  929. systemctl status tox-bootstrapd.service
  930. unmount_drive
  931. exit 59369
  932. fi
  933. fi
  934. }
  935. function get_restore_app {
  936. if [ ${1} ]; then
  937. if [ ! -d /home/${1} ]; then
  938. RESTORE_APP=${1}
  939. echo $"Restore $RESTORE_APP"
  940. fi
  941. fi
  942. }
  943. get_restore_app ${2}
  944. mount_drive ${1} ${2}
  945. check_backup_exists
  946. check_admin_user
  947. copy_gpg_keys
  948. restore_configuration
  949. same_admin_user
  950. update_domains
  951. restore_mariadb
  952. restore_letsencrypt
  953. restore_tor
  954. restore_mutt_settings
  955. restore_gpg
  956. restore_procmail
  957. restore_spamassassin
  958. restore_admin_readme
  959. restore_ipfs
  960. restore_user_ssh_keys
  961. restore_user_config
  962. restore_certs
  963. restore_personal_settings
  964. restore_mailing_list
  965. restore_xmpp
  966. restore_gnu_social
  967. restore_hubzilla
  968. restore_owncloud
  969. restore_gogs
  970. restore_wiki
  971. restore_blog
  972. restore_cjdns
  973. restore_email
  974. restore_dlna
  975. restore_voip
  976. restore_tox
  977. unmount_drive
  978. echo $"Restore from USB drive is complete. You can now unplug it."
  979. exit 0