freedombone-restore-local 22KB

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