freedombone-restore-local 27KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795
  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 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. export TEXTDOMAIN=${PROJECT_NAME}-restore-local
  32. export TEXTDOMAINDIR="/usr/share/locale"
  33. USB_DRIVE=/dev/sdb1
  34. USB_MOUNT=/mnt/usb
  35. # get default USB from config file
  36. CONFIG_FILE=$HOME/${PROJECT_NAME}.cfg
  37. if [ -f $CONFIG_FILE ]; then
  38. if grep -q "USB_DRIVE=" $CONFIG_FILE; then
  39. USB_DRIVE=$(cat $CONFIG_FILE | grep "USB_DRIVE=" | awk -F '=' '{print $2}')
  40. fi
  41. fi
  42. ADMIN_USERNAME=
  43. ADMIN_NAME=
  44. # MariaDB password
  45. DATABASE_PASSWORD=$(cat /root/dbpass)
  46. MICROBLOG_DOMAIN_NAME=
  47. HUBZILLA_DOMAIN_NAME=
  48. OWNCLOUD_DOMAIN_NAME=
  49. GIT_DOMAIN_NAME=
  50. WIKI_DOMAIN_NAME=
  51. FULLBLOG_DOMAIN_NAME=
  52. function mount_drive {
  53. if [ $1 ]; then
  54. USB_DRIVE=/dev/${1}1
  55. fi
  56. # get the admin user
  57. ADMIN_USERNAME=$(cat $COMPLETION_FILE | grep "Admin user" | awk -F ':' '{print $2}')
  58. if [ $2 ]; then
  59. ADMIN_USERNAME=$2
  60. fi
  61. ADMIN_NAME=$(getent passwd $ADMIN_USERNAME | cut -d: -f5 | cut -d, -f1)
  62. # check that the backup destination is available
  63. if [ ! -b $USB_DRIVE ]; then
  64. echo $"Please attach a USB drive"
  65. exit 1
  66. fi
  67. # unmount if already mounted
  68. umount -f $USB_MOUNT
  69. if [ ! -d $USB_MOUNT ]; then
  70. mkdir $USB_MOUNT
  71. fi
  72. if [ -f /dev/mapper/encrypted_usb ]; then
  73. rm -rf /dev/mapper/encrypted_usb
  74. fi
  75. cryptsetup luksClose encrypted_usb
  76. # mount the encrypted backup drive
  77. cryptsetup luksOpen $USB_DRIVE encrypted_usb
  78. if [ "$?" = "0" ]; then
  79. USB_DRIVE=/dev/mapper/encrypted_usb
  80. fi
  81. mount $USB_DRIVE $USB_MOUNT
  82. if [ ! "$?" = "0" ]; then
  83. echo $"There was a problem mounting the USB drive to $USB_MOUNT"
  84. rm -rf $USB_MOUNT
  85. exit 2
  86. fi
  87. }
  88. function unmount_drive {
  89. sync
  90. umount $USB_MOUNT
  91. if [ ! "$?" = "0" ]; then
  92. echo $"Unable to unmount the drive. This means that the backup did not work"
  93. rm -rf $USB_MOUNT
  94. exit 9
  95. fi
  96. rm -rf $USB_MOUNT
  97. echo $"Setting permissions"
  98. for d in /home/*/ ; do
  99. USERNAME=$(echo "$d" | awk -F '/' '{print $3}')
  100. if [[ $USERNAME != "git" ]]; then
  101. chown -R $USERNAME:$USERNAME /home/$USERNAME
  102. fi
  103. done
  104. if [[ $USB_DRIVE == /dev/mapper/encrypted_usb ]]; then
  105. echo $"Unmount encrypted USB"
  106. cryptsetup luksClose encrypted_usb
  107. fi
  108. if [ -f /dev/mapper/encrypted_usb ]; then
  109. rm -rf /dev/mapper/encrypted_usb
  110. fi
  111. }
  112. function check_backup_exists {
  113. if [ ! -d $USB_MOUNT/backup ]; then
  114. echo $"No backup directory found on the USB drive."
  115. unmount_drive
  116. exit 2
  117. fi
  118. }
  119. function check_admin_user {
  120. echo $"Checking that admin user exists"
  121. if [ ! -d /home/$ADMIN_USERNAME ]; then
  122. echo $"Username $ADMIN_USERNAME not found. Reinstall ${PROJECT_NAME} with this username."
  123. unmount_drive
  124. exit 295
  125. fi
  126. }
  127. function copy_gpg_keys {
  128. echo $"Copying GPG keys from admin user to root"
  129. cp -r /home/$ADMIN_USERNAME/.gnupg /root
  130. }
  131. function restore_directory_from_usb {
  132. if [ ! -d ${1} ]; then
  133. mkdir ${1}
  134. fi
  135. obnam restore -r $USB_MOUNT/backup/${2} --to ${1}
  136. }
  137. function restore_database {
  138. RESTORE_SUBDIR="root"
  139. if [ -d $USB_MOUNT/backup/${1} ]; then
  140. echo $"Restoring ${1} database"
  141. restore_directory_from_usb "/root/temp${1}data" "${1}data"
  142. if [ ! -f /root/temp${1}data/${RESTORE_SUBDIR}/temp${1}data/${1}.sql ]; then
  143. echo $"Unable to restore ${1} database"
  144. rm -rf /root/temp${1}data
  145. unmount_drive
  146. exit 503
  147. fi
  148. mysqlsuccess=$(mysql -u root --password=$DATABASE_PASSWORD ${1} -o < /root/temp${1}data/${RESTORE_SUBDIR}/temp${1}data/${1}.sql)
  149. if [ ! "$?" = "0" ]; then
  150. echo "$mysqlsuccess"
  151. unmount_drive
  152. exit 964
  153. fi
  154. shred -zu /root/temp${1}data/${RESTORE_SUBDIR}/temp${1}data/*
  155. rm -rf /root/temp${1}data
  156. echo $"Restoring ${1} installation"
  157. if [ ! -d /root/temp${1} ]; then
  158. mkdir /root/temp${1}
  159. fi
  160. restore_directory_from_usb "/root/temp${1}" "${1}"
  161. RESTORE_SUBDIR="var"
  162. if [ ${2} ]; then
  163. if [ -d /var/www/${2}/htdocs ]; then
  164. if [ -d /root/temp${1}/${RESTORE_SUBDIR}/www/${2}/htdocs ]; then
  165. rm -rf /var/www/${2}/htdocs
  166. mv /root/temp${1}/${RESTORE_SUBDIR}/www/${2}/htdocs /var/www/${2}/
  167. if [ ! "$?" = "0" ]; then
  168. unmount_drive
  169. exit 683
  170. fi
  171. if [ -d /etc/letsencrypt/live/${2} ]; then
  172. ln -s /etc/letsencrypt/live/${2}/privkey.pem /etc/ssl/private/${2}.key
  173. ln -s /etc/letsencrypt/live/${2}/fullchain.pem /etc/ssl/certs/${2}.pem
  174. else
  175. # Ensure that the bundled SSL cert is being used
  176. if [ -f /etc/ssl/certs/${2}.bundle.crt ]; then
  177. sed -i "s|${2}.crt|${2}.bundle.crt|g" /etc/nginx/sites-available/${2}
  178. fi
  179. fi
  180. fi
  181. fi
  182. fi
  183. fi
  184. }
  185. function update_domains {
  186. if grep -q "GNU Social domain" $COMPLETION_FILE; then
  187. MICROBLOG_DOMAIN_NAME=$(cat $COMPLETION_FILE | grep "GNU Social domain" | awk -F ':' '{print $2}')
  188. fi
  189. if grep -q "Hubzilla domain" $COMPLETION_FILE; then
  190. HUBZILLA_DOMAIN_NAME=$(cat $COMPLETION_FILE | grep "Hubzilla domain" | awk -F ':' '{print $2}')
  191. fi
  192. if grep -q "Owncloud domain" $COMPLETION_FILE; then
  193. OWNCLOUD_DOMAIN_NAME=$(cat $COMPLETION_FILE | grep "Owncloud domain" | awk -F ':' '{print $2}')
  194. fi
  195. if grep -q "Gogs domain" $COMPLETION_FILE; then
  196. GIT_DOMAIN_NAME=$(cat $COMPLETION_FILE | grep "Gogs domain" | awk -F ':' '{print $2}')
  197. fi
  198. if [ -d $USB_MOUNT/backup/wiki ]; then
  199. WIKI_DOMAIN_NAME=$(cat $COMPLETION_FILE | grep "Wiki domain" | awk -F ':' '{print $2}')
  200. fi
  201. if [ -d $USB_MOUNT/backup/blog ]; then
  202. FULLBLOG_DOMAIN_NAME=$(cat $COMPLETION_FILE | grep "Blog domain" | awk -F ':' '{print $2}')
  203. fi
  204. }
  205. function restore_configuration {
  206. # this restores *.cfg and COMPLETION_FILE
  207. if [ -d $USB_MOUNT/backup/config ]; then
  208. echo $"Restoring configuration files"
  209. restore_directory_from_usb /root/tempconfig config
  210. cp -f /root/tempconfig/root/${PROJECT_NAME}.cfg $CONFIG_FILE
  211. if [ ! "$?" = "0" ]; then
  212. unmount_drive
  213. rm -rf /root/tempconfig
  214. exit 5294
  215. fi
  216. cp -f /root/tempconfig/root/${PROJECT_NAME}-completed.txt $COMPLETION_FILE
  217. if [ ! "$?" = "0" ]; then
  218. unmount_drive
  219. rm -rf /root/tempconfig
  220. exit 6382
  221. fi
  222. rm -rf /root/tempconfig
  223. fi
  224. }
  225. function same_admin_user {
  226. PREV_ADMIN_USERNAME=$(cat $COMPLETION_FILE | grep "Admin user" | awk -F ':' '{print $2}')
  227. if [[ "$PREV_ADMIN_USERNAME" != "$ADMIN_USERNAME" ]]; then
  228. 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"
  229. unmount_drive
  230. exit 73265
  231. fi
  232. }
  233. mount_drive $1 $2
  234. check_backup_exists
  235. check_admin_user
  236. copy_gpg_keys
  237. restore_configuration
  238. same_admin_user
  239. update_domains
  240. # Make a backup of the original README file
  241. # incase old passwords need to be used
  242. if [ -f /home/$ADMIN_USERNAME/README ]; then
  243. if [ ! -f /home/$ADMIN_USERNAME/README_original ]; then
  244. cp /home/$ADMIN_USERNAME/README /home/$ADMIN_USERNAME/README_original
  245. fi
  246. fi
  247. if [ -d $USB_MOUNT/backup/mariadb ]; then
  248. echo $"Restoring mysql settings"
  249. restore_directory_from_usb /root/tempmariadb mariadb
  250. echo $"Get the MariaDB password from the backup"
  251. if [ ! -f /root/tempmariadb/root/tempmariadb/db ]; then
  252. echo $"MariaDB password file not found"
  253. exit 495
  254. fi
  255. BACKUP_MARIADB_PASSWORD=$(cat /root/tempmariadb/root/tempmariadb/db)
  256. if [[ $BACKUP_MARIADB_PASSWORD != $DATABASE_PASSWORD ]]; then
  257. echo $"Restore the MariaDB user table"
  258. mysqlsuccess=$(mysql -u root --password=$DATABASE_PASSWORD mysql -o < /root/tempmariadb/root/tempmariadb/mysql.sql)
  259. if [ ! "$?" = "0" ]; then
  260. echo $"Try again using the password obtained from backup"
  261. mysqlsuccess=$(mysql -u root --password=$BACKUP_MARIADB_PASSWORD mysql -o < /root/tempmariadb/root/tempmariadb/mysql.sql)
  262. fi
  263. if [ ! "$?" = "0" ]; then
  264. echo "$mysqlsuccess"
  265. unmount_drive
  266. exit 962
  267. fi
  268. echo $"Restarting database"
  269. service mysql restart
  270. echo $"Change the MariaDB password to the backup version"
  271. DATABASE_PASSWORD=$BACKUP_MARIADB_PASSWORD
  272. fi
  273. shred -zu /root/tempmariadb/root/tempmariadb/db
  274. rm -rf /root/tempmariadb
  275. # Change database password file
  276. echo "$DATABASE_PASSWORD" > /root/dbpass
  277. chmod 600 /root/dbpass
  278. fi
  279. if [ -d $USB_MOUNT/backup/letsencrypt ]; then
  280. echo $"Restoring Lets Encrypt settings"
  281. restore_directory_from_usb / letsencrypt
  282. fi
  283. if [ -d $USB_MOUNT/backup/mutt ]; then
  284. for d in $USB_MOUNT/backup/mutt/*/ ; do
  285. USERNAME=$(echo "$d" | awk -F '/' '{print $6}')
  286. if [[ $USERNAME != "git" ]]; then
  287. if [ ! -d /home/$USERNAME ]; then
  288. ${PROJECT_NAME}-adduser $USERNAME
  289. fi
  290. echo $"Restoring Mutt settings for $USERNAME"
  291. restore_directory_from_usb /root/tempmutt mutt/$USERNAME
  292. if [ -f /root/tempmutt/home/$USERNAME/tempbackup/.muttrc ]; then
  293. cp -f /root/tempmutt/home/$USERNAME/tempbackup/.muttrc /home/$USERNAME/.muttrc
  294. fi
  295. if [ -f /root/tempmutt/home/$USERNAME/tempbackup/Muttrc ]; then
  296. cp -f /root/tempmutt/home/$USERNAME/tempbackup/Muttrc /etc/Muttrc
  297. fi
  298. if [ ! "$?" = "0" ]; then
  299. rm -rf /root/tempmutt
  300. unmount_drive
  301. exit 276
  302. fi
  303. rm -rf /root/tempmutt
  304. fi
  305. done
  306. fi
  307. if [ -d $USB_MOUNT/backup/gnupg ]; then
  308. for d in $USB_MOUNT/backup/gnupg/*/ ; do
  309. USERNAME=$(echo "$d" | awk -F '/' '{print $6}')
  310. if [[ $USERNAME != "git" ]]; then
  311. if [ ! -d /home/$USERNAME ]; then
  312. ${PROJECT_NAME}-adduser $USERNAME
  313. fi
  314. echo $"Restoring gnupg settings for $USERNAME"
  315. restore_directory_from_usb /root/tempgnupg gnupg/$USERNAME
  316. cp -r /root/tempgnupg/home/$USERNAME/.gnupg /home/$USERNAME/
  317. if [ ! "$?" = "0" ]; then
  318. rm -rf /root/tempgnupg
  319. unmount_drive
  320. exit 276
  321. fi
  322. rm -rf /root/tempgnupg
  323. if [[ "$USERNAME" == "$ADMIN_USERNAME" ]]; then
  324. cp -r /home/$USERNAME/.gnupg /root
  325. if [ ! "$?" = "0" ]; then
  326. unmount_drive
  327. exit 283
  328. fi
  329. fi
  330. fi
  331. done
  332. fi
  333. if [ -d $USB_MOUNT/backup/procmail ]; then
  334. for d in $USB_MOUNT/backup/procmail/*/ ; do
  335. USERNAME=$(echo "$d" | awk -F '/' '{print $6}')
  336. if [[ $USERNAME != "git" ]]; then
  337. if [ ! -d /home/$USERNAME ]; then
  338. ${PROJECT_NAME}-adduser $USERNAME
  339. fi
  340. echo $"Restoring procmail settings for $USERNAME"
  341. restore_directory_from_usb /root/tempprocmail procmail/$USERNAME
  342. cp -f /root/tempprocmail/home/$USERNAME/tempbackup/.procmailrc /home/$USERNAME/
  343. if [ ! "$?" = "0" ]; then
  344. rm -rf /root/tempprocmail
  345. unmount_drive
  346. exit 276
  347. fi
  348. rm -rf /root/tempprocmail
  349. fi
  350. done
  351. fi
  352. if [ -d $USB_MOUNT/backup/spamassassin ]; then
  353. for d in $USB_MOUNT/backup/spamassassin/*/ ; do
  354. USERNAME=$(echo "$d" | awk -F '/' '{print $6}')
  355. if [[ $USERNAME != "git" ]]; then
  356. if [ -d $USB_MOUNT/backup/spamassassin/$USERNAME ]; then
  357. if [ ! -d /home/$USERNAME ]; then
  358. ${PROJECT_NAME}-adduser $USERNAME
  359. fi
  360. echo $"Restoring spamassassin settings for $USERNAME"
  361. restore_directory_from_usb /root/tempspamassassin spamassassin/$USERNAME
  362. cp -rf /root/tempspamassassin/home/$USERNAME/.spamassassin /home/$USERNAME/
  363. if [ ! "$?" = "0" ]; then
  364. rm -rf /root/tempspamassassin
  365. unmount_drive
  366. exit 276
  367. fi
  368. rm -rf /root/tempspamassassin
  369. fi
  370. fi
  371. done
  372. fi
  373. if [ -d $USB_MOUNT/backup/readme ]; then
  374. echo $"Restoring admin user README"
  375. restore_directory_from_usb /root/tempreadme readme
  376. cp -f /root/tempreadme/home/$ADMIN_USERNAME/tempbackup/README /home/$ADMIN_USERNAME/
  377. if [ ! "$?" = "0" ]; then
  378. rm -rf /root/tempreadme
  379. unmount_drive
  380. exit 276
  381. fi
  382. rm -rf /root/tempreadme
  383. fi
  384. if [ -d $USB_MOUNT/backup/ipfs ]; then
  385. echo $"Restoring IPFS"
  386. restore_directory_from_usb /root/tempipfs ipfs
  387. cp -rf /root/tempipfs/home/$ADMIN_USERNAME/.ipfs/* /home/$ADMIN_USERNAME/.ipfs
  388. if [ ! "$?" = "0" ]; then
  389. rm -rf /root/tempipfs
  390. unmount_drive
  391. exit 276
  392. fi
  393. rm -rf /root/tempipfs
  394. fi
  395. if [ -d $USB_MOUNT/backup/ssh ]; then
  396. for d in $USB_MOUNT/backup/ssh/*/ ; do
  397. USERNAME=$(echo "$d" | awk -F '/' '{print $6}')
  398. if [[ $USERNAME != "git" ]]; then
  399. if [ ! -d /home/$USERNAME ]; then
  400. ${PROJECT_NAME}-adduser $USERNAME
  401. fi
  402. echo $"Restoring ssh keys for $USERNAME"
  403. restore_directory_from_usb /root/tempssh ssh/$USERNAME
  404. cp -r /root/tempssh/home/$USERNAME/.ssh /home/$USERNAME/
  405. if [ ! "$?" = "0" ]; then
  406. rm -rf /root/tempssh
  407. unmount_drive
  408. exit 664
  409. fi
  410. rm -rf /root/tempssh
  411. fi
  412. done
  413. fi
  414. if [ -d $USB_MOUNT/backup/config ]; then
  415. for d in $USB_MOUNT/backup/config/*/ ; do
  416. USERNAME=$(echo "$d" | awk -F '/' '{print $6}')
  417. if [[ $USERNAME != "git" ]]; then
  418. if [ ! -d /home/$USERNAME ]; then
  419. ${PROJECT_NAME}-adduser $USERNAME
  420. fi
  421. echo $"Restoring config files for $USERNAME"
  422. restore_directory_from_usb /root/tempconfig config/$USERNAME
  423. cp -r /root/tempconfig/home/$USERNAME/.config /home/$USERNAME/
  424. if [ ! "$?" = "0" ]; then
  425. rm -rf /root/tempconfig
  426. unmount_drive
  427. exit 664
  428. fi
  429. rm -rf /root/tempconfig
  430. fi
  431. done
  432. fi
  433. if [ -d $USB_MOUNT/backup/ssl ]; then
  434. echo $"Restoring certificates"
  435. mkdir /root/tempssl
  436. restore_directory_from_usb /root/tempssl ssl
  437. cp -r /root/tempssl/etc/ssl/* /etc/ssl
  438. if [ ! "$?" = "0" ]; then
  439. unmount_drive
  440. exit 276
  441. fi
  442. rm -rf /root/tempssl
  443. fi
  444. if [ -d $USB_MOUNT/backup/projects ]; then
  445. for d in $USB_MOUNT/backup/projects/*/ ; do
  446. USERNAME=$(echo "$d" | awk -F '/' '{print $6}')
  447. if [[ $USERNAME != "git" ]]; then
  448. if [ ! -d /home/$USERNAME ]; then
  449. ${PROJECT_NAME}-adduser $USERNAME
  450. fi
  451. echo $"Restoring projects for $USERNAME"
  452. restore_directory_from_usb /root/tempprojects projects/$USERNAME
  453. if [ -d /home/$USERNAME/projects ]; then
  454. rm -rf /home/$USERNAME/projects
  455. fi
  456. mv /root/tempprojects/home/$USERNAME/projects /home/$USERNAME
  457. if [ ! "$?" = "0" ]; then
  458. unmount_drive
  459. exit 166
  460. fi
  461. rm -rf /root/tempprojects
  462. fi
  463. done
  464. fi
  465. if [ -d $USB_MOUNT/backup/personal ]; then
  466. for d in $USB_MOUNT/backup/personal/*/ ; do
  467. USERNAME=$(echo "$d" | awk -F '/' '{print $6}')
  468. if [[ $USERNAME != "git" ]]; then
  469. if [ -d $USB_MOUNT/backup/personal/$USERNAME ]; then
  470. if [ ! -d /home/$USERNAME ]; then
  471. ${PROJECT_NAME}-adduser $USERNAME
  472. fi
  473. echo $"Restoring personal settings for $USERNAME"
  474. restore_directory_from_usb /root/temppersonal personal/$USERNAME
  475. if [ -d /home/$USERNAME/personal ]; then
  476. rm -rf /home/$USERNAME/personal
  477. fi
  478. mv /root/temppersonal/home/$USERNAME/personal /home/$USERNAME
  479. if [ ! "$?" = "0" ]; then
  480. unmount_drive
  481. exit 184
  482. fi
  483. rm -rf /root/temppersonal
  484. fi
  485. fi
  486. done
  487. fi
  488. if [ -d /var/spool/mlmmj ]; then
  489. echo $"Restoring public mailing list"
  490. restore_directory_from_usb /root/tempmailinglist mailinglist
  491. cp -r /root/tempmailinglist/root/spool/mlmmj/* /var/spool/mlmmj
  492. if [ ! "$?" = "0" ]; then
  493. unmount_drive
  494. exit 526
  495. fi
  496. rm -rf /root/tempmailinglist
  497. fi
  498. if [ -d /var/lib/prosody ]; then
  499. echo $"Restoring XMPP settings"
  500. restore_directory_from_usb /root/tempxmpp xmpp
  501. cp -r /root/tempxmpp/var/lib/prosody/* /var/lib/prosody
  502. if [ ! "$?" = "0" ]; then
  503. unmount_drive
  504. exit 725
  505. fi
  506. rm -rf /root/tempxmpp
  507. service prosody restart
  508. chown -R prosody:prosody /var/lib/prosody/*
  509. fi
  510. # Restoring GNU Social
  511. if [ $MICROBLOG_DOMAIN_NAME ]; then
  512. restore_database gnusocial ${MICROBLOG_DOMAIN_NAME}
  513. if [ -d /root/tempgnusocial ]; then
  514. rm -rf /root/tempgnusocial
  515. fi
  516. fi
  517. # Restoring hubzilla
  518. if [ $HUBZILLA_DOMAIN_NAME ]; then
  519. restore_database hubzilla ${HUBZILLA_DOMAIN_NAME}
  520. if [ -d $USB_MOUNT/backup/hubzilla ]; then
  521. if [ ! -d /var/www/${HUBZILLA_DOMAIN_NAME}/htdocs/store/[data]/smarty3 ]; then
  522. mkdir -p /var/www/${HUBZILLA_DOMAIN_NAME}/htdocs/store/[data]/smarty3
  523. fi
  524. chmod 777 /var/www/${HUBZILLA_DOMAIN_NAME}/htdocs/store/[data]/smarty3
  525. chown -R www-data:www-data /var/www/${HUBZILLA_DOMAIN_NAME}/htdocs/*
  526. if [ -d /root/temphubzilla ]; then
  527. rm -rf /root/temphubzilla
  528. fi
  529. fi
  530. fi
  531. if [ $OWNCLOUD_DOMAIN_NAME ]; then
  532. restore_database owncloud $OWNCLOUD_DOMAIN_NAME
  533. if [ -d $USB_MOUNT/backup/owncloud2 ]; then
  534. restore_directory_from_usb /root/tempowncloud2 owncloud2
  535. cp -r /root/tempowncloud2/etc/owncloud/* /etc/owncloud/
  536. if [ ! "$?" = "0" ]; then
  537. unmount_drive
  538. exit 982
  539. fi
  540. rm -rf /root/tempowncloud
  541. rm -rf /root/tempowncloud2
  542. chown -R www-data:www-data /var/lib/owncloud/data
  543. chown -R www-data:www-data /var/lib/owncloud/backup
  544. chown -R www-data:www-data /var/lib/owncloud/assets
  545. for d in /home/*/ ; do
  546. USERNAME=$(echo "$d" | awk -F '/' '{print $3}')
  547. if [[ $USERNAME != "git" ]]; then
  548. occ files:scan $USERNAME
  549. fi
  550. done
  551. ln -s /usr/share/owncloud /var/www/${OWNCLOUD_DOMAIN_NAME}/htdocs
  552. fi
  553. fi
  554. if [ $GIT_DOMAIN_NAME ]; then
  555. restore_database gogs ${GIT_DOMAIN_NAME}
  556. if [ -d $USB_MOUNT/backup/gogs ]; then
  557. echo $"Restoring Gogs settings"
  558. if [ ! -d /home/git/go/src/github.com/gogits/gogs/custom ]; then
  559. mkdir -p /home/git/go/src/github.com/gogits/gogs/custom
  560. fi
  561. cp -r /root/tempgogs/home/git/go/src/github.com/gogits/gogs/custom/* /home/git/go/src/github.com/gogits/gogs/custom
  562. if [ ! "$?" = "0" ]; then
  563. unmount_drive
  564. exit 981
  565. fi
  566. echo $"Restoring Gogs repos"
  567. restore_directory_from_usb /root/tempgogsrepos gogsrepos
  568. cp -r /root/tempgogsrepos/home/git/gogs-repositories/* /home/git/gogs-repositories/
  569. if [ ! "$?" = "0" ]; then
  570. unmount_drive
  571. exit 67574
  572. fi
  573. echo $"Restoring Gogs authorized_keys"
  574. restore_directory_from_usb /root/tempgogsssh gogsssh
  575. if [ ! -d /home/git/.ssh ]; then
  576. mkdir /home/git/.ssh
  577. fi
  578. cp -r /root/tempgogsssh/home/git/.ssh/* /home/git/.ssh/
  579. if [ ! "$?" = "0" ]; then
  580. unmount_drive
  581. exit 8463
  582. fi
  583. rm -rf /root/tempgogs
  584. rm -rf /root/tempgogsrepos
  585. rm -rf /root/tempgogsssh
  586. chown -R git:git /home/git
  587. fi
  588. fi
  589. if [ $WIKI_DOMAIN_NAME ]; then
  590. echo $"Restoring Wiki installation ${WIKI_DOMAIN_NAME}"
  591. restore_directory_from_usb /root/tempwiki wiki
  592. cp -r /root/tempwiki/var/lib/dokuwiki/* /var/lib/dokuwiki/
  593. if [ ! "$?" = "0" ]; then
  594. unmount_drive
  595. exit 868
  596. fi
  597. restore_directory_from_usb /root/tempwiki2 wiki2
  598. cp -r /root/tempwiki2/etc/dokuwiki/* /etc/dokuwiki/
  599. if [ ! "$?" = "0" ]; then
  600. unmount_drive
  601. exit 869
  602. fi
  603. rm -rf /root/tempwiki
  604. rm -rf /root/tempwiki2
  605. chown -R www-data:www-data /var/lib/dokuwiki/*
  606. # Ensure that the bundled SSL cert is being used
  607. if [ -f /etc/ssl/certs/${WIKI_DOMAIN_NAME}.bundle.crt ]; then
  608. sed -i "s|${WIKI_DOMAIN_NAME}.crt|${WIKI_DOMAIN_NAME}.bundle.crt|g" /etc/nginx/sites-available/${WIKI_DOMAIN_NAME}
  609. fi
  610. if [ -d /etc/letsencrypt/live/${WIKI_DOMAIN_NAME} ]; then
  611. ln -s /etc/letsencrypt/live/${WIKI_DOMAIN_NAME}/privkey.pem /etc/ssl/private/${WIKI_DOMAIN_NAME}.key
  612. ln -s /etc/letsencrypt/live/${WIKI_DOMAIN_NAME}/fullchain.pem /etc/ssl/certs/${WIKI_DOMAIN_NAME}.pem
  613. fi
  614. fi
  615. if [ $FULLBLOG_DOMAIN_NAME ]; then
  616. echo $"Restoring blog installation"
  617. restore_directory_from_usb /root/tempblog blog
  618. rm -rf /var/www/${FULLBLOG_DOMAIN_NAME}/htdocs
  619. cp -r /root/tempblog/var/www/${FULLBLOG_DOMAIN_NAME}/htdocs /var/www/${FULLBLOG_DOMAIN_NAME}/
  620. if [ ! "$?" = "0" ]; then
  621. unmount_drive
  622. exit 593
  623. fi
  624. rm -rf /root/tempblog
  625. if [ ! -d /var/www/${FULLBLOG_DOMAIN_NAME}/htdocs/content ]; then
  626. echo $"No content directory found after restoring blog"
  627. unmount_drive
  628. exit 287
  629. fi
  630. chown -R www-data:www-data /var/www/${FULLBLOG_DOMAIN_NAME}/htdocs
  631. # Ensure that the bundled SSL cert is being used
  632. if [ -f /etc/ssl/certs/${FULLBLOG_DOMAIN_NAME}.bundle.crt ]; then
  633. sed -i "s|${FULLBLOG_DOMAIN_NAME}.crt|${FULLBLOG_DOMAIN_NAME}.bundle.crt|g" /etc/nginx/sites-available/${FULLBLOG_DOMAIN_NAME}
  634. fi
  635. for d in /home/*/ ; do
  636. USERNAME=$(echo "$d" | awk -F '/' '{print $3}')
  637. if [[ $USERNAME != "git" ]]; then
  638. if [ -d /var/www/${FULLBLOG_DOMAIN_NAME}/htdocs/content/$USERNAME/blog/uncategorized/post ]; then
  639. mv /var/www/${FULLBLOG_DOMAIN_NAME}/htdocs/content/$USERNAME/blog/*.md /var/www/${FULLBLOG_DOMAIN_NAME}/htdocs/content/$USERNAME/blog/uncategorized/post
  640. fi
  641. fi
  642. done
  643. if [ -d /etc/letsencrypt/live/${FULLBLOG_DOMAIN_NAME} ]; then
  644. ln -s /etc/letsencrypt/live/${FULLBLOG_DOMAIN_NAME}/privkey.pem /etc/ssl/private/${FULLBLOG_DOMAIN_NAME}.key
  645. ln -s /etc/letsencrypt/live/${FULLBLOG_DOMAIN_NAME}/fullchain.pem /etc/ssl/certs/${FULLBLOG_DOMAIN_NAME}.pem
  646. fi
  647. fi
  648. if [ -d $USB_MOUNT/backup/cjdns ]; then
  649. echo $"Restoring cjdns installation"
  650. restore_directory_from_usb /root/tempcjdns cjdns
  651. rm -rf /etc/cjdns
  652. cp -r /root/tempcjdns/etc/cjdns /etc/
  653. if [ ! "$?" = "0" ]; then
  654. unmount_drive
  655. exit 8472
  656. fi
  657. rm -rf /root/tempcjdns
  658. fi
  659. if [ -d $USB_MOUNT/backup/mail ]; then
  660. for d in $USB_MOUNT/backup/mail/*/ ; do
  661. USERNAME=$(echo "$d" | awk -F '/' '{print $6}')
  662. if [[ $USERNAME != "git" ]]; then
  663. if [ ! -d /home/$USERNAME ]; then
  664. ${PROJECT_NAME}-adduser $USERNAME
  665. fi
  666. echo $"Restoring emails for $USERNAME"
  667. restore_directory_from_usb /root/tempmail mail/$USERNAME
  668. if [ ! -d /home/$USERNAME/Maildir ]; then
  669. mkdir /home/$USERNAME/Maildir
  670. fi
  671. tar -xzvf /root/tempmail/root/tempbackupemail/$USERNAME/maildir.tar.gz -C /
  672. if [ ! "$?" = "0" ]; then
  673. unmount_drive
  674. exit 927
  675. fi
  676. rm -rf /root/tempmail
  677. fi
  678. done
  679. fi
  680. if [ -d /var/cache/minidlna ]; then
  681. if [ -d $USB_MOUNT/backup/dlna ]; then
  682. echo $"Restoring DLNA cache"
  683. restore_directory_from_usb /root/tempdlna dlna
  684. cp -r /root/tempdlna/var/cache/minidlna/* /var/cache/minidlna/
  685. if [ ! "$?" = "0" ]; then
  686. rm -rf /root/tempdlna
  687. unmount_drive
  688. exit 982
  689. fi
  690. rm -rf /root/tempdlna
  691. fi
  692. fi
  693. if [ -d $USB_MOUNT/backup/voip ]; then
  694. echo $"Restoring VoIP settings"
  695. restore_directory_from_usb /root/tempvoip voip
  696. cp -f /root/tempvoip/home/$ADMIN_USERNAME/tempbackup/mumble-server.ini /etc/
  697. if [ ! "$?" = "0" ]; then
  698. rm -rf /root/tempvoip
  699. unmount_drive
  700. exit 3679
  701. fi
  702. cp -f /root/tempvoip/home/$ADMIN_USERNAME/tempbackup/sipwitch.conf /etc/sipwitch.conf
  703. if [ ! "$?" = "0" ]; then
  704. rm -rf /root/tempvoip
  705. unmount_drive
  706. exit 3679
  707. fi
  708. cp -f /root/tempvoip/home/$ADMIN_USERNAME/tempbackup/mumble-server.sqlite /var/lib/mumble-server/
  709. if [ ! "$?" = "0" ]; then
  710. rm -rf /root/tempvoip
  711. unmount_drive
  712. exit 276
  713. fi
  714. rm -rf /root/tempvoip
  715. cp /etc/ssl/certs/mumble* /var/lib/mumble-server
  716. cp /etc/ssl/private/mumble* /var/lib/mumble-server
  717. chown -R mumble-server:mumble-server /var/lib/mumble-server
  718. service sipwitch restart
  719. service mumble-server restart
  720. fi
  721. if [ -d $USB_MOUNT/backup/tox ]; then
  722. echo $"Restoring Tox node settings"
  723. restore_directory_from_usb / tox
  724. if [ ! "$?" = "0" ]; then
  725. unmount_drive
  726. exit 6393
  727. fi
  728. cp /var/lib/tox-bootstrapd/tox-bootstrapd.conf /etc/tox-bootstrapd.conf
  729. systemctl restart tox-bootstrapd.service
  730. if [ ! "$?" = "0" ]; then
  731. systemctl status tox-bootstrapd.service
  732. unmount_drive
  733. exit 59369
  734. fi
  735. fi
  736. unmount_drive
  737. echo $"Restore from USB drive is complete. You can now unplug it."
  738. exit 0