freedombone-restore-remote 35KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004
  1. #!/bin/bash
  2. #
  3. # .---. . .
  4. # | | |
  5. # |--- .--. .-. .-. .-.| .-. .--.--. |.-. .-. .--. .-.
  6. # | | (.-' (.-' ( | ( )| | | | )( )| | (.-'
  7. # ' ' --' --' -' - -' ' ' -' -' -' ' - --'
  8. #
  9. # Freedom in the Cloud
  10. #
  11. # Restore from a given remote server
  12. # License
  13. # =======
  14. #
  15. # Copyright (C) 2015-2016 Bob Mottram <bob@robotics.uk.to>
  16. #
  17. # This program is free software: you can redistribute it and/or modify
  18. # it under the terms of the GNU Affero General Public License as published by
  19. # the Free Software Foundation, either version 3 of the License, or
  20. # (at your option) any later version.
  21. #
  22. # This program is distributed in the hope that it will be useful,
  23. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  24. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  25. # GNU Affero General Public License for more details.
  26. #
  27. # You should have received a copy of the GNU Affero General Public License
  28. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  29. PROJECT_NAME='freedombone'
  30. COMPLETION_FILE=$HOME/${PROJECT_NAME}-completed.txt
  31. CONFIG_FILE=$HOME/${PROJECT_NAME}.cfg
  32. BACKUP_EXTRA_DIRECTORIES=/root/backup-extra-dirs.csv
  33. export TEXTDOMAIN=${PROJECT_NAME}-restore-remote
  34. export TEXTDOMAINDIR="/usr/share/locale"
  35. SERVER_NAME=$1
  36. # whether to restore everything or just a specific application
  37. RESTORE_APP='all'
  38. if [ ${2} ]; then
  39. RESTORE_APP=${2}
  40. fi
  41. ADMIN_USERNAME=$(cat $COMPLETION_FILE | grep "Admin user" | -nawk -F ':' '{print $2}')
  42. ADMIN_EMAIL_ADDRESS=${ADMIN_USERNAME}@${HOSTNAME}
  43. # Temporary location for data to be backed up to other servers
  44. SERVER_DIRECTORY=/root/remoterestore
  45. BACKUP_LIST=/home/${ADMIN_USERNAME}/backup.list
  46. if [ ! $SERVER_NAME ]; then
  47. echo $'restorefromfriend [server]'
  48. exit 1
  49. fi
  50. if [ ! -f $BACKUP_LIST ]; then
  51. echo $"No friends list found at $BACKUP_LIST"
  52. exit 2
  53. fi
  54. if ! grep -q "$SERVER_NAME" $BACKUP_LIST; then
  55. echo $"Server not found within the friends list"
  56. exit 3
  57. fi
  58. REMOTE_SERVER=$(grep -i "$SERVER_NAME" $BACKUP_LIST | awk -F ' ' '{print $1}')
  59. REMOTE_SSH_PORT=$(grep -i "$SERVER_NAME" $BACKUP_LIST | awk -F ' ' '{print $2}')
  60. REMOTE_DIRECTORY=$(grep -i "$SERVER_NAME" $BACKUP_LIST | awk -F ' ' '{print $3}')
  61. REMOTE_PASSWORD=$(grep -i "$SERVER_NAME" $BACKUP_LIST | awk -F ' ' '{print $4}')
  62. REMOTE_SERVER=$REMOTE_SERVER:$REMOTE_DIRECTORY
  63. NOW=$(date +"%Y-%m-%d %H:%M:%S")
  64. echo "$NOW Starting restore from $REMOTE_SERVER" >> /var/log/remotebackups.log
  65. rsync -ratlzv --rsh="/usr/bin/sshpass -p $REMOTE_PASSWORD ssh -p $REMOTE_SSH_PORT -o StrictHostKeyChecking=no" $REMOTE_SERVER/backup $SERVER_DIRECTORY
  66. if [ ! "$?" = "0" ]; then
  67. echo "$NOW Restore from $REMOTE_SERVER failed" >> /var/log/remotebackups.log
  68. # Send a warning email
  69. echo "Restore from $REMOTE_SERVER failed" | mail -s "${PROJECT_NAME} restore from friend" $ADMIN_EMAIL_ADDRESS
  70. exit 790
  71. else
  72. echo "$NOW Restored encrypted data from $REMOTE_SERVER" >> /var/log/remotebackups.log
  73. fi
  74. # MariaDB password
  75. DATABASE_PASSWORD=$(cat /root/dbpass)
  76. function restore_directory_from_friend {
  77. if [ ! -d ${1} ]; then
  78. mkdir ${1}
  79. fi
  80. obnam restore -r $SERVER_DIRECTORY/backup/${2} --to ${1}
  81. }
  82. function copy_gpg_keys {
  83. echo $"Copying GPG keys from admin user to root"
  84. cp -r /home/$ADMIN_USERNAME/.gnupg /root
  85. }
  86. function restore_database_from_friend {
  87. DATABASE_PASSWORD=
  88. RESTORE_SUBDIR="root"
  89. if [ -d $SERVER_DIRECTORY/backup/${1} ]; then
  90. echo $"Restoring ${1} database"
  91. restore_directory_from_friend /root/temp${1}data ${1}data
  92. if [ ! -f /root/temp${1}data/${RESTORE_SUBDIR}/temp${1}data/${1}.sql ]; then
  93. echo $"Unable to restore ${1} database"
  94. rm -rf /root/temp${1}data
  95. exit 503
  96. fi
  97. mysqlsuccess=$(mysql -u root --password="$DATABASE_PASSWORD" ${1} -o < /root/temp${1}data/${RESTORE_SUBDIR}/temp${1}data/${1}.sql)
  98. if [ ! "$?" = "0" ]; then
  99. echo "$mysqlsuccess"
  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. restore_directory_from_friend /root/temp${1} ${1}
  106. RESTORE_SUBDIR="var"
  107. if [ ${1} ]; then
  108. # special handling of ttrss
  109. if [[ ${2} == "ttrss" ]]; then
  110. if [ -d /etc/share/tt-rss ]; then
  111. rm -rf /etc/share/tt-rss
  112. mv /root/temp${1}/etc/share/tt-rss /etc/share/
  113. if [ ! "$?" = "0" ]; then
  114. exit 639
  115. fi
  116. if [ -d /etc/letsencrypt/live/${2} ]; then
  117. ln -s /etc/letsencrypt/live/${2}/privkey.pem /etc/ssl/private/${2}.key
  118. ln -s /etc/letsencrypt/live/${2}/fullchain.pem /etc/ssl/certs/${2}.pem
  119. else
  120. # Ensure that the bundled SSL cert is being used
  121. if [ -f /etc/ssl/certs/${2}.bundle.crt ]; then
  122. sed -i "s|${2}.crt|${2}.bundle.crt|g" /etc/nginx/sites-available/${2}
  123. fi
  124. fi
  125. fi
  126. fi
  127. if [ -d /var/www/${2}/htdocs ]; then
  128. if [ -d /root/temp${1}/${RESTORE_SUBDIR}/www/${2}/htdocs ]; then
  129. rm -rf /var/www/${2}/htdocs
  130. mv /root/temp${1}/${RESTORE_SUBDIR}/www/${2}/htdocs /var/www/${2}/
  131. if [ ! "$?" = "0" ]; then
  132. exit 683
  133. fi
  134. if [ -d /etc/letsencrypt/live/${2} ]; then
  135. ln -s /etc/letsencrypt/live/${2}/privkey.pem /etc/ssl/private/${2}.key
  136. ln -s /etc/letsencrypt/live/${2}/fullchain.pem /etc/ssl/certs/${2}.pem
  137. else
  138. # Ensure that the bundled SSL cert is being used
  139. if [ -f /etc/ssl/certs/${2}.bundle.crt ]; then
  140. sed -i "s|${2}.crt|${2}.bundle.crt|g" /etc/nginx/sites-available/${2}
  141. fi
  142. fi
  143. fi
  144. fi
  145. fi
  146. fi
  147. }
  148. function restore_configuration {
  149. if [[ $RESTORE_APP != 'all' ]]; then
  150. if [[ $RESTORE_APP != 'configuration' ]]; then
  151. return
  152. fi
  153. fi
  154. if [ -d $SERVER_DIRECTORY/backup/config ]; then
  155. echo $"Restoring configuration files"
  156. restore_directory_from_friend /root/tempconfig config
  157. cp -f /root/tempconfig/root/${PROJECT_NAME}.cfg $CONFIG_FILE
  158. if [ ! "$?" = "0" ]; then
  159. unmount_drive
  160. rm -rf /root/tempconfig
  161. exit 5372
  162. fi
  163. if [ -f $CONFIG_FILE ]; then
  164. # install according to the config file
  165. freedombone -c $CONFIG_FILE
  166. fi
  167. cp -f /root/tempconfig/root/${PROJECT_NAME}-completed.txt $COMPLETION_FILE
  168. if [ ! "$?" = "0" ]; then
  169. unmount_drive
  170. rm -rf /root/tempconfig
  171. exit 7252
  172. fi
  173. if [ -f /root/tempconfig${BACKUP_EXTRA_DIRECTORIES} ]; then
  174. cp -f /root/tempconfig${BACKUP_EXTRA_DIRECTORIES} ${BACKUP_EXTRA_DIRECTORIES}
  175. if [ ! "$?" = "0" ]; then
  176. unmount_drive
  177. rm -rf /root/tempconfig
  178. exit 62121
  179. fi
  180. fi
  181. # restore nginx password hashes
  182. if [ -f /root/tempconfig/root/htpasswd ]; then
  183. cp -f /root/tempconfig/root/htpasswd /etc/nginx/.htpasswd
  184. fi
  185. rm -rf /root/tempconfig
  186. fi
  187. }
  188. function restore_mariadb {
  189. if [[ $RESTORE_APP != 'all' ]]; then
  190. if [[ $RESTORE_APP != 'mariadb' ]]; then
  191. return
  192. fi
  193. fi
  194. if [ -d $SERVER_DIRECTORY/backup/mariadb ]; then
  195. echo $"Restoring MariaDB settings"
  196. restore_directory_from_friend /root/tempmariadb mariadb
  197. echo $"Get the MariaDB password from the backup"
  198. if [ ! -f /root/tempmariadb/root/tempmariadb/db ]; then
  199. echo $"MariaDB password file not found"
  200. exit 495
  201. fi
  202. BACKUP_MARIADB_PASSWORD=$(cat /root/tempmariadb/root/tempmariadb/db)
  203. if [[ "$BACKUP_MARIADB_PASSWORD" != "$DATABASE_PASSWORD" ]]; then
  204. echo $"Restore the MariaDB user table"
  205. mysqlsuccess=$(mysql -u root --password="$DATABASE_PASSWORD" mysql -o < /root/tempmariadb/root/tempmariadb/mysql.sql)
  206. if [ ! "$?" = "0" ]; then
  207. echo $"Try again using the password obtained from backup"
  208. mysqlsuccess=$(mysql -u root --password="$BACKUP_MARIADB_PASSWORD" mysql -o < /root/tempmariadb/root/tempmariadb/mysql.sql)
  209. fi
  210. if [ ! "$?" = "0" ]; then
  211. echo "$mysqlsuccess"
  212. exit 962
  213. fi
  214. echo $"Restarting database"
  215. service mysql restart
  216. echo $"Change the MariaDB password to the backup version"
  217. DATABASE_PASSWORD=$BACKUP_MARIADB_PASSWORD
  218. fi
  219. shred -zu /root/tempmariadb/root/tempmariadb/db
  220. rm -rf /root/tempmariadb
  221. # Change database password file
  222. echo "$DATABASE_PASSWORD" > /root/dbpass
  223. chmod 600 /root/dbpass
  224. fi
  225. }
  226. function restore_letsencrypt {
  227. if [[ $RESTORE_APP != 'all' ]]; then
  228. if [[ $RESTORE_APP != 'letsencrypt' ]]; then
  229. return
  230. fi
  231. fi
  232. if [ -d $SERVER_DIRECTORY/backup/letsencrypt ]; then
  233. echo $"Restoring Lets Encrypt settings"
  234. restore_directory_from_friend / letsencrypt
  235. fi
  236. }
  237. function restore_tor {
  238. if [[ $RESTORE_APP != 'all' ]]; then
  239. if [[ $RESTORE_APP != 'tor' ]]; then
  240. return
  241. fi
  242. fi
  243. if [ -d $SERVER_DIRECTORY/backup/tor ]; then
  244. echo $"Restoring Tor settings"
  245. restore_directory_from_friend / tor
  246. fi
  247. }
  248. function restore_mutt_settings {
  249. if [[ $RESTORE_APP != 'all' ]]; then
  250. if [[ $RESTORE_APP != 'mutt' ]]; then
  251. return
  252. fi
  253. fi
  254. for d in $SERVER_DIRECTORY/backup/mutt/*/ ; do
  255. USERNAME=$(echo "$d" | awk -F '/' '{print $6}')
  256. if [[ $USERNAME != "git" && $USRNAME != "mirrors" ]]; then
  257. if [ -d $SERVER_DIRECTORY/backup/mutt/$USERNAME ]; then
  258. if [ ! -d /home/$USERNAME ]; then
  259. ${PROJECT_NAME}-adduser $USERNAME
  260. fi
  261. echo $"Restoring Mutt settings for $USERNAME"
  262. restore_directory_from_friend /root/tempmutt mutt/$USERNAME
  263. if [ -f /root/tempmutt/home/$USERNAME/tempbackup/.muttrc ]; then
  264. cp -f /root/tempmutt/home/$USERNAME/tempbackup/.muttrc /home/$USERNAME/.muttrc
  265. fi
  266. if [ -f /root/tempmutt/home/$USERNAME/tempbackup/Muttrc ]; then
  267. cp -f /root/tempmutt/home/$USERNAME/tempbackup/Muttrc /etc/Muttrc
  268. fi
  269. if [ ! "$?" = "0" ]; then
  270. rm -rf /root/tempmutt
  271. exit 276
  272. fi
  273. rm -rf /root/tempmutt
  274. fi
  275. fi
  276. done
  277. }
  278. function restore_gpg {
  279. if [[ $RESTORE_APP != 'all' ]]; then
  280. if [[ $RESTORE_APP != 'gpg' ]]; then
  281. return
  282. fi
  283. fi
  284. for d in $SERVER_DIRECTORY/backup/gnupg/*/ ; do
  285. USERNAME=$(echo "$d" | awk -F '/' '{print $6}')
  286. if [[ $USERNAME != "git" && $USRNAME != "mirrors" ]]; then
  287. if [ -d $SERVER_DIRECTORY/backup/gnupg/$USERNAME ]; then
  288. if [ ! -d /home/$USERNAME ]; then
  289. ${PROJECT_NAME}-adduser $USERNAME
  290. fi
  291. echo $"Restoring gnupg settings for $USERNAME"
  292. restore_directory_from_friend /root/tempgnupg gnupg/$USERNAME
  293. cp -r /root/tempgnupg/home/$USERNAME/.gnupg /home/$USERNAME/
  294. if [ ! "$?" = "0" ]; then
  295. rm -rf /root/tempgnupg
  296. exit 276
  297. fi
  298. rm -rf /root/tempgnupg
  299. if [[ "$USERNAME" == "$ADMIN_USERNAME" ]]; then
  300. cp -r /home/$USERNAME/.gnupg /root
  301. if [ ! "$?" = "0" ]; then
  302. exit 283
  303. fi
  304. fi
  305. fi
  306. fi
  307. done
  308. }
  309. function restore_procmail {
  310. if [[ $RESTORE_APP != 'all' ]]; then
  311. if [[ $RESTORE_APP != 'procmail' ]]; then
  312. return
  313. fi
  314. fi
  315. for d in $SERVER_DIRECTORY/backup/procmail/*/ ; do
  316. USERNAME=$(echo "$d" | awk -F '/' '{print $6}')
  317. if [[ $USERNAME != "git" && $USRNAME != "mirrors" ]]; then
  318. if [ -d $SERVER_DIRECTORY/backup/procmail/$USERNAME ]; then
  319. if [ ! -d /home/$USERNAME ]; then
  320. ${PROJECT_NAME}-adduser $USERNAME
  321. fi
  322. echo $"Restoring procmail settings for $USERNAME"
  323. restore_directory_from_friend /root/tempprocmail procmail/$USERNAME
  324. cp -f /root/tempprocmail/home/$USERNAME/tempbackup/.procmailrc /home/$USERNAME/
  325. if [ ! "$?" = "0" ]; then
  326. rm -rf /root/tempprocmail
  327. exit 276
  328. fi
  329. rm -rf /root/tempprocmail
  330. fi
  331. fi
  332. done
  333. }
  334. function restore_spamassassin {
  335. if [[ $RESTORE_APP != 'all' ]]; then
  336. if [[ $RESTORE_APP != 'spamassassin' ]]; then
  337. return
  338. fi
  339. fi
  340. for d in $SERVER_DIRECTORY/backup/spamassassin/*/ ; do
  341. USERNAME=$(echo "$d" | awk -F '/' '{print $6}')
  342. if [[ $USERNAME != "git" && $USRNAME != "mirrors" ]]; then
  343. if [ -d $SERVER_DIRECTORY/backup/spamassassin/$USERNAME ]; then
  344. if [ ! -d /home/$USERNAME ]; then
  345. ${PROJECT_NAME}-adduser $USERNAME
  346. fi
  347. echo $"Restoring spamassassin settings for $USERNAME"
  348. restore_directory_from_friend /root/tempspamassassin spamassassin/$USERNAME
  349. cp -rf /root/tempspamassassin/home/$USERNAME/.spamassassin /home/$USERNAME/
  350. if [ ! "$?" = "0" ]; then
  351. rm -rf /root/tempspamassassin
  352. exit 276
  353. fi
  354. rm -rf /root/tempspamassassin
  355. fi
  356. fi
  357. done
  358. }
  359. function restore_admin_readme {
  360. if [[ $RESTORE_APP != 'all' ]]; then
  361. if [[ $RESTORE_APP != 'readme' ]]; then
  362. return
  363. fi
  364. fi
  365. if [ -d $SERVER_DIRECTORY/backup/readme ]; then
  366. echo $"Restoring README"
  367. restore_directory_from_friend /root/tempreadme readme
  368. cp -f /root/tempreadme/home/$ADMIN_USERNAME/tempbackup/README /home/$ADMIN_USERNAME/
  369. if [ ! "$?" = "0" ]; then
  370. rm -rf /root/tempreadme
  371. exit 276
  372. fi
  373. rm -rf /root/tempreadme
  374. fi
  375. }
  376. function restore_ipfs {
  377. if [[ $RESTORE_APP != 'all' ]]; then
  378. if [[ $RESTORE_APP != 'ipfs' ]]; then
  379. return
  380. fi
  381. fi
  382. if [ -d $SERVER_DIRECTORY/backup/ipfs ]; then
  383. echo $"Restoring IPFS"
  384. restore_directory_from_friend /root/tempipfs ipfs
  385. cp -rf /root/tempipfs/home/$ADMIN_USERNAME/.ipfs/* /home/$ADMIN_USERNAME/.ipfs
  386. if [ ! "$?" = "0" ]; then
  387. rm -rf /root/tempipfs
  388. exit 276
  389. fi
  390. rm -rf /root/tempipfs
  391. fi
  392. }
  393. function restore_ssh_keys {
  394. if [[ $RESTORE_APP != 'all' ]]; then
  395. if [[ $RESTORE_APP != 'ssh' ]]; then
  396. return
  397. fi
  398. fi
  399. for d in $SERVER_DIRECTORY/backup/ssh/*/ ; do
  400. USERNAME=$(echo "$d" | awk -F '/' '{print $6}')
  401. if [[ $USERNAME != "git" && $USRNAME != "mirrors" ]]; then
  402. if [ -d $SERVER_DIRECTORY/backup/ssh/$USERNAME ]; then
  403. if [ ! -d /home/$USERNAME ]; then
  404. ${PROJECT_NAME}-adduser $USERNAME
  405. fi
  406. echo $"Restoring ssh keys for $USERNAME"
  407. restore_directory_from_friend /root/tempssh ssh/$USERNAME
  408. cp -r /root/tempssh/home/$USERNAME/.ssh /home/$USERNAME/
  409. if [ ! "$?" = "0" ]; then
  410. rm -rf /root/tempssh
  411. exit 664
  412. fi
  413. rm -rf /root/tempssh
  414. fi
  415. fi
  416. done
  417. }
  418. function restore_user_config {
  419. if [[ $RESTORE_APP != 'all' ]]; then
  420. if [[ $RESTORE_APP != 'userconfig' ]]; then
  421. return
  422. fi
  423. fi
  424. for d in $SERVER_DIRECTORY/backup/config/*/ ; do
  425. USERNAME=$(echo "$d" | awk -F '/' '{print $6}')
  426. if [[ $USERNAME != "git" && $USRNAME != "mirrors" ]]; then
  427. if [ -d $SERVER_DIRECTORY/backup/config/$USERNAME ]; then
  428. if [ ! -d /home/$USERNAME ]; then
  429. ${PROJECT_NAME}-adduser $USERNAME
  430. fi
  431. echo $"Restoring config files for $USERNAME"
  432. restore_directory_from_friend /root/tempconfig config/$USERNAME
  433. cp -r /root/tempconfig/home/$USERNAME/.config /home/$USERNAME/
  434. if [ ! "$?" = "0" ]; then
  435. rm -rf /root/tempconfig
  436. exit 664
  437. fi
  438. rm -rf /root/tempconfig
  439. fi
  440. fi
  441. done
  442. }
  443. function restore_user_fin {
  444. if [[ $RESTORE_APP != 'all' ]]; then
  445. if [[ $RESTORE_APP != 'userfin' ]]; then
  446. return
  447. fi
  448. fi
  449. for d in $SERVER_DIRECTORY/backup/fin/*/ ; do
  450. USERNAME=$(echo "$d" | awk -F '/' '{print $6}')
  451. if [[ $USERNAME != "git" && $USRNAME != "mirrors" ]]; then
  452. if [ -d $SERVER_DIRECTORY/backup/fin/$USERNAME ]; then
  453. if [ ! -d /home/$USERNAME ]; then
  454. ${PROJECT_NAME}-adduser $USERNAME
  455. fi
  456. echo $"Restoring fin files for $USERNAME"
  457. restore_directory_from_friend /root/tempfin fin/$USERNAME
  458. cp -r /root/tempfin/home/$USERNAME/.fin /home/$USERNAME/
  459. if [ ! "$?" = "0" ]; then
  460. rm -rf /root/tempfin
  461. exit 664
  462. fi
  463. rm -rf /root/tempfin
  464. fi
  465. fi
  466. done
  467. }
  468. function restore_user_local {
  469. if [[ $RESTORE_APP != 'all' ]]; then
  470. if [[ $RESTORE_APP != 'userlocal' ]]; then
  471. return
  472. fi
  473. fi
  474. for d in $SERVER_DIRECTORY/backup/local/*/ ; do
  475. USERNAME=$(echo "$d" | awk -F '/' '{print $6}')
  476. if [[ $USERNAME != "git" && $USRNAME != "mirrors" ]]; then
  477. if [ -d $SERVER_DIRECTORY/backup/local/$USERNAME ]; then
  478. if [ ! -d /home/$USERNAME ]; then
  479. ${PROJECT_NAME}-adduser $USERNAME
  480. fi
  481. echo $"Restoring local files for $USERNAME"
  482. restore_directory_from_friend /root/templocal local/$USERNAME
  483. cp -r /root/templocal/home/$USERNAME/.local /home/$USERNAME/
  484. if [ ! "$?" = "0" ]; then
  485. rm -rf /root/templocal
  486. exit 664
  487. fi
  488. rm -rf /root/templocal
  489. fi
  490. fi
  491. done
  492. }
  493. function restore_certs {
  494. if [[ $RESTORE_APP != 'all' ]]; then
  495. if [[ $RESTORE_APP != 'certs' ]]; then
  496. return
  497. fi
  498. fi
  499. if [ -d $SERVER_DIRECTORY/backup/ssl ]; then
  500. echo $"Restoring certificates"
  501. restore_directory_from_friend /root/tempssl ssl
  502. cp -r /root/tempssl/etc/ssl/* /etc/ssl
  503. if [ ! "$?" = "0" ]; then
  504. exit 276
  505. fi
  506. rm -rf /root/tempssl
  507. # restore ownership
  508. if [ -f /etc/ssl/private/xmpp.key ]; then
  509. chown prosody:prosody /etc/ssl/private/xmpp.key
  510. chown prosody:prosody /etc/ssl/certs/xmpp.*
  511. fi
  512. if [ -d /etc/dovecot ]; then
  513. chown root:dovecot /etc/ssl/private/dovecot.*
  514. chown root:dovecot /etc/ssl/certs/dovecot.*
  515. fi
  516. if [ -f /etc/ssl/private/exim.key ]; then
  517. cp /etc/ssl/private/exim.key /etc/exim4
  518. cp /etc/ssl/certs/exim.crt /etc/exim4
  519. cp /etc/ssl/certs/exim.dhparam /etc/exim4
  520. chown root:Debian-exim /etc/exim4/exim.key /etc/exim4/exim.crt /etc/exim4/exim.dhparam
  521. chmod 640 /etc/exim4/exim.key /etc/exim4/exim.crt /etc/exim4/exim.dhparam
  522. fi
  523. if [ -f /etc/ssl/private/mumble.key ]; then
  524. if [ -d /var/lib/mumble-server ]; then
  525. cp /etc/ssl/certs/mumble.* /var/lib/mumble-server
  526. cp /etc/ssl/private/mumble.key /var/lib/mumble-server
  527. chown -R mumble-server:mumble-server /var/lib/mumble-server
  528. fi
  529. fi
  530. fi
  531. }
  532. function restore_personal_settings {
  533. if [[ $RESTORE_APP != 'all' ]]; then
  534. if [[ $RESTORE_APP != 'personal' ]]; then
  535. return
  536. fi
  537. fi
  538. for d in $SERVER_DIRECTORY/backup/personal/*/ ; do
  539. USERNAME=$(echo "$d" | awk -F '/' '{print $6}')
  540. if [[ $USERNAME != "git" && $USRNAME != "mirrors" ]]; then
  541. if [ -d $SERVER_DIRECTORY/backup/personal/$USERNAME ]; then
  542. if [ ! -d /home/$USERNAME ]; then
  543. ${PROJECT_NAME}-adduser $USERNAME
  544. fi
  545. echo $"Restoring personal settings for $USERNAME"
  546. restore_directory_from_friend /root/temppersonal personal/$USERNAME
  547. if [ -d /home/$USERNAME/personal ]; then
  548. rm -rf /home/$USERNAME/personal
  549. fi
  550. mv /root/temppersonal/home/$USERNAME/personal /home/$USERNAME
  551. if [ ! "$?" = "0" ]; then
  552. exit 184
  553. fi
  554. rm -rf /root/temppersonal
  555. fi
  556. fi
  557. done
  558. }
  559. function restore_mailing_list {
  560. if [[ $RESTORE_APP != 'all' ]]; then
  561. if [[ $RESTORE_APP != 'mailinglist' ]]; then
  562. return
  563. fi
  564. fi
  565. if [ -d /var/spool/mlmmj ]; then
  566. echo $"Restoring public mailing list"
  567. restore_directory_from_friend /root/tempmailinglist mailinglist
  568. cp -r /root/tempmailinglist/root/spool/mlmmj/* /var/spool/mlmmj
  569. if [ ! "$?" = "0" ]; then
  570. exit 526
  571. fi
  572. rm -rf /root/tempmailinglist
  573. fi
  574. }
  575. function restore_xmpp {
  576. if [[ $RESTORE_APP != 'all' ]]; then
  577. if [[ $RESTORE_APP != 'xmpp' ]]; then
  578. return
  579. fi
  580. fi
  581. if [ -d /var/lib/prosody ]; then
  582. echo $"Restoring XMPP settings"
  583. restore_directory_from_friend /root/tempxmpp xmpp
  584. cp -r /root/tempxmpp/var/lib/prosody/* /var/lib/prosody
  585. if [ ! "$?" = "0" ]; then
  586. exit 725
  587. fi
  588. rm -rf /root/tempxmpp
  589. service prosody restart
  590. chown -R prosody:prosody /var/lib/prosody/*
  591. fi
  592. }
  593. function restore_gnu_social {
  594. if [[ $RESTORE_APP != 'all' ]]; then
  595. if [[ $RESTORE_APP != 'gnusocial' ]]; then
  596. return
  597. fi
  598. fi
  599. if grep -q "GNU Social domain" $COMPLETION_FILE; then
  600. MICROBLOG_DOMAIN_NAME=$(cat $COMPLETION_FILE | grep "GNU Social domain" | awk -F ':' '{print $2}')
  601. restore_database_from_friend gnusocial ${MICROBLOG_DOMAIN_NAME}
  602. if [ -d /root/tempgnusocial ]; then
  603. rm -rf /root/tempgnusocial
  604. fi
  605. fi
  606. }
  607. function restore_rss_reader {
  608. if [[ $RESTORE_APP != 'all' ]]; then
  609. if [[ $RESTORE_APP != 'ttrss' ]]; then
  610. return
  611. fi
  612. fi
  613. if grep -q "RSS reader domain" $COMPLETION_FILE; then
  614. RSS_READER_DOMAIN_NAME=$(cat $COMPLETION_FILE | grep "RSS reader domain" | awk -F ':' '{print $2}')
  615. restore_database_from_friend ttrss ${RSS_READER_DOMAIN_NAME}
  616. if [ -d $SERVER_DIRECTORY/backup/ttrss ]; then
  617. chown -R www-data:www-data /etc/share/tt-rss
  618. fi
  619. if [ -d /root/tempttrss ]; then
  620. rm -rf /root/tempttrss
  621. fi
  622. fi
  623. }
  624. function restore_hubzilla {
  625. if [[ $RESTORE_APP != 'all' ]]; then
  626. if [[ $RESTORE_APP != 'hubzilla' ]]; then
  627. return
  628. fi
  629. fi
  630. if grep -q "Hubzilla domain" $COMPLETION_FILE; then
  631. HUBZILLA_DOMAIN_NAME=$(cat $COMPLETION_FILE | grep "Hubzilla domain" | awk -F ':' '{print $2}')
  632. restore_database_from_friend hubzilla ${HUBZILLA_DOMAIN_NAME}
  633. if [ -d $SERVER_DIRECTORY/backup/hubzilla ]; then
  634. if [ ! -d /var/www/${HUBZILLA_DOMAIN_NAME}/htdocs/store/[data]/smarty3 ]; then
  635. mkdir -p /var/www/${HUBZILLA_DOMAIN_NAME}/htdocs/store/[data]/smarty3
  636. fi
  637. chmod 777 /var/www/${HUBZILLA_DOMAIN_NAME}/htdocs/store/[data]/smarty3
  638. chown -R www-data:www-data /var/www/${HUBZILLA_DOMAIN_NAME}/htdocs/*
  639. fi
  640. if [ -d /root/temphubzilla ]; then
  641. rm -rf /root/temphubzilla
  642. fi
  643. fi
  644. }
  645. function restore_owncloud {
  646. if [[ $RESTORE_APP != 'all' ]]; then
  647. if [[ $RESTORE_APP != 'owncloud' ]]; then
  648. return
  649. fi
  650. fi
  651. if grep -q "Owncloud domain" $COMPLETION_FILE; then
  652. OWNCLOUD_DOMAIN_NAME=$(cat $COMPLETION_FILE | grep "Owncloud domain" | awk -F ':' '{print $2}')
  653. restore_database_from_friend owncloudrepo $OWNCLOUD_DOMAIN_NAME
  654. if [ -d $SERVER_DIRECTORY/backup/owncloudrepofiles ]; then
  655. echo $"Restoring Owncloud installation"
  656. cp -r /root/tempowncloudrepofiles/* /
  657. if [ ! "$?" = "0" ]; then
  658. exit 1458
  659. fi
  660. rm -rf /root/tempowncloudrepofiles
  661. fi
  662. if [ -d $SERVER_DIRECTORY/backup/owncloudrepoconfig ]; then
  663. echo $"Restoring Owncloud installation"
  664. cp -r /root/tempowncloudrepoconfig/* /
  665. if [ ! "$?" = "0" ]; then
  666. exit 2571
  667. fi
  668. rm -rf /root/tempowncloudrepoconfig
  669. fi
  670. chown -R www-data:www-data /var/www/${OWNCLOUD_DOMAIN_NAME}/htdocs
  671. # re-index files
  672. for d in /home/*/ ; do
  673. USERNAME=$(echo "$d" | awk -F '/' '{print $3}')
  674. if [[ $USERNAME != "git" && $USRNAME != "mirrors" ]]; then
  675. /var/www/${OWNCLOUD_DOMAIN_NAME}/htdocs/occ files:scan $USERNAME
  676. fi
  677. done
  678. fi
  679. }
  680. function restore_gogs {
  681. if [[ $RESTORE_APP != 'all' ]]; then
  682. if [[ $RESTORE_APP != 'gogs' ]]; then
  683. return
  684. fi
  685. fi
  686. if grep -q "Gogs domain" $COMPLETION_FILE; then
  687. GIT_DOMAIN_NAME=$(cat $COMPLETION_FILE | grep "Gogs domain" | awk -F ':' '{print $2}')
  688. restore_database_from_friend gogs $GIT_DOMAIN_NAME
  689. if [ -d $SERVER_DIRECTORY/backup/gogs ]; then
  690. if [ ! -d /home/git/go/src/github.com/gogits/gogs/custom ]; then
  691. mkdir -p /home/git/go/src/github.com/gogits/gogs/custom
  692. fi
  693. cp -r /root/tempgogs/home/git/go/src/github.com/gogits/gogs/custom/* /home/git/go/src/github.com/gogits/gogs/custom/
  694. if [ ! "$?" = "0" ]; then
  695. exit 5885
  696. fi
  697. echo $"Restoring Gogs repos"
  698. restore_directory_from_friend /root/tempgogsrepos gogsrepos
  699. cp -r /root/tempgogsrepos/home/git/gogs-repositories/* /home/git/gogs-repositories/
  700. if [ ! "$?" = "0" ]; then
  701. exit 7649
  702. fi
  703. echo $"Restoring Gogs authorized_keys"
  704. restore_directory_from_friend /root/tempgogsssh gogsssh
  705. if [ ! -d /home/git/.ssh ]; then
  706. mkdir /home/git/.ssh
  707. fi
  708. cp -r /root/tempgogsssh/home/git/.ssh/* /home/git/.ssh/
  709. if [ ! "$?" = "0" ]; then
  710. exit 74239
  711. fi
  712. rm -rf /root/tempgogs
  713. rm -rf /root/tempgogsrepos
  714. rm -rf /root/tempgogsssh
  715. chown -R git:git /home/git
  716. fi
  717. fi
  718. }
  719. function restore_wiki {
  720. if [[ $RESTORE_APP != 'all' ]]; then
  721. if [[ $RESTORE_APP != 'wiki' ]]; then
  722. return
  723. fi
  724. fi
  725. if [ -d $SERVER_DIRECTORY/backup/wiki ]; then
  726. WIKI_DOMAIN_NAME=$(cat $COMPLETION_FILE | grep "Wiki domain" | awk -F ':' '{print $2}')
  727. echo $"Restoring Wiki installation $WIKI_DOMAIN_NAME"
  728. restore_directory_from_friend /root/tempwiki wiki
  729. cp -r /root/tempwiki/var/lib/dokuwiki/* /var/lib/dokuwiki/
  730. if [ ! "$?" = "0" ]; then
  731. exit 868
  732. fi
  733. restore_directory_from_friend /root/tempwiki2 wiki2
  734. cp -r /root/tempwiki2/etc/dokuwiki/* /etc/dokuwiki/
  735. if [ ! "$?" = "0" ]; then
  736. exit 869
  737. fi
  738. rm -rf /root/tempwiki
  739. rm -rf /root/tempwiki2
  740. chown -R www-data:www-data /var/lib/dokuwiki/*
  741. # Ensure that the bundled SSL cert is being used
  742. if [ -f /etc/ssl/certs/${WIKI_DOMAIN_NAME}.bundle.crt ]; then
  743. sed -i "s|${WIKI_DOMAIN_NAME}.crt|${WIKI_DOMAIN_NAME}.bundle.crt|g" /etc/nginx/sites-available/${WIKI_DOMAIN_NAME}
  744. fi
  745. if [ -d /etc/letsencrypt/live/${WIKI_DOMAIN_NAME} ]; then
  746. ln -s /etc/letsencrypt/live/${WIKI_DOMAIN_NAME}/privkey.pem /etc/ssl/private/${WIKI_DOMAIN_NAME}.key
  747. ln -s /etc/letsencrypt/live/${WIKI_DOMAIN_NAME}/fullchain.pem /etc/ssl/certs/${WIKI_DOMAIN_NAME}.pem
  748. fi
  749. fi
  750. }
  751. function restore_blog {
  752. if [[ $RESTORE_APP != 'all' ]]; then
  753. if [[ $RESTORE_APP != 'blog' ]]; then
  754. return
  755. fi
  756. fi
  757. if [ -d $SERVER_DIRECTORY/backup/blog ]; then
  758. FULLBLOG_DOMAIN_NAME=$(cat $COMPLETION_FILE | grep "Blog domain" | awk -F ':' '{print $2}')
  759. echo $"Restoring blog installation $FULLBLOG_DOMAIN_NAME"
  760. mkdir /root/tempblog
  761. restore_directory_from_friend /root/tempblog blog
  762. rm -rf /var/www/${FULLBLOG_DOMAIN_NAME}/htdocs
  763. cp -r /root/tempblog/var/www/${FULLBLOG_DOMAIN_NAME}/htdocs /var/www/${FULLBLOG_DOMAIN_NAME}/
  764. if [ ! "$?" = "0" ]; then
  765. exit 593
  766. fi
  767. rm -rf /root/tempblog
  768. if [ ! -d /var/www/${FULLBLOG_DOMAIN_NAME}/htdocs/content ]; then
  769. echo $"No content directory found after restoring blog"
  770. exit 287
  771. fi
  772. # Ensure that the bundled SSL cert is being used
  773. if [ -f /etc/ssl/certs/${FULLBLOG_DOMAIN_NAME}.bundle.crt ]; then
  774. sed -i "s|${FULLBLOG_DOMAIN_NAME}.crt|${FULLBLOG_DOMAIN_NAME}.bundle.crt|g" /etc/nginx/sites-available/${FULLBLOG_DOMAIN_NAME}
  775. fi
  776. for d in /home/*/ ; do
  777. USERNAME=$(echo "$d" | awk -F '/' '{print $3}')
  778. if [[ $USERNAME != "git" && $USRNAME != "mirrors" ]]; then
  779. if [ -d /var/www/${FULLBLOG_DOMAIN_NAME}/htdocs/content/$USERNAME/blog/uncategorized/post ]; then
  780. mv /var/www/${FULLBLOG_DOMAIN_NAME}/htdocs/content/$USERNAME/blog/*.md /var/www/${FULLBLOG_DOMAIN_NAME}/htdocs/content/$USERNAME/blog/uncategorized/post
  781. fi
  782. done
  783. if [ -d /etc/letsencrypt/live/${FULLBLOG_DOMAIN_NAME} ]; then
  784. ln -s /etc/letsencrypt/live/${FULLBLOG_DOMAIN_NAME}/privkey.pem /etc/ssl/private/${FULLBLOG_DOMAIN_NAME}.key
  785. ln -s /etc/letsencrypt/live/${FULLBLOG_DOMAIN_NAME}/fullchain.pem /etc/ssl/certs/${FULLBLOG_DOMAIN_NAME}.pem
  786. fi
  787. fi
  788. }
  789. function restore_cjdns {
  790. if [[ $RESTORE_APP != 'all' ]]; then
  791. if [[ $RESTORE_APP != 'cjdns' ]]; then
  792. return
  793. fi
  794. fi
  795. if [ -d $SERVER_DIRECTORY/backup/cjdns ]; then
  796. echo $"Restoring cjdns installation"
  797. restore_directory_from_friend /root/tempcjdns cjdns
  798. rm -rf /etc/cjdns
  799. cp -r /root/tempcjdns/etc/cjdns /etc/
  800. if [ ! "$?" = "0" ]; then
  801. exit 7438
  802. fi
  803. rm -rf /root/tempcjdns
  804. fi
  805. }
  806. function restore_voip {
  807. if [[ $RESTORE_APP != 'all' ]]; then
  808. if [[ $RESTORE_APP != 'voip' ]]; then
  809. return
  810. fi
  811. fi
  812. if [ -d $SERVER_DIRECTORY/backup/voip ]; then
  813. echo $"Restoring VoIP settings"
  814. restore_directory_from_friend /root/tempvoip voip
  815. cp -f /root/tempvoip/home/$ADMIN_USERNAME/tempbackup/mumble-server.ini /etc/
  816. if [ ! "$?" = "0" ]; then
  817. rm -rf /root/tempvoip
  818. exit 7823
  819. fi
  820. cp -f /root/tempvoip/home/$ADMIN_USERNAME/tempbackup/sipwitch.conf /etc/sipwitch.conf
  821. if [ ! "$?" = "0" ]; then
  822. rm -rf /root/tempvoip
  823. exit 7823
  824. fi
  825. cp -f /root/tempvoip/home/$ADMIN_USERNAME/tempbackup/mumble-server.sqlite /var/lib/mumble-server/
  826. if [ ! "$?" = "0" ]; then
  827. rm -rf /root/tempvoip
  828. exit 276
  829. fi
  830. rm -rf /root/tempvoip
  831. cp /etc/ssl/certs/mumble* /var/lib/mumble-server
  832. cp /etc/ssl/private/mumble* /var/lib/mumble-server
  833. chown -R mumble-server:mumble-server /var/lib/mumble-server
  834. service sipwitch restart
  835. service mumble-server restart
  836. fi
  837. }
  838. function restore_tox {
  839. if [[ $RESTORE_APP != 'all' ]]; then
  840. if [[ $RESTORE_APP != 'tox' ]]; then
  841. return
  842. fi
  843. fi
  844. if [ -d $SERVER_DIRECTORY/backup/tox ]; then
  845. echo $"Restoring Tox node settings"
  846. restore_directory_from_friend / tox
  847. if [ ! "$?" = "0" ]; then
  848. exit 93653
  849. fi
  850. cp /var/lib/tox-bootstrapd/tox-bootstrapd.conf /etc/tox-bootstrapd.conf
  851. systemctl restart tox-bootstrapd.service
  852. if [ ! "$?" = "0" ]; then
  853. systemctl status tox-bootstrapd.service
  854. exit 59369
  855. fi
  856. fi
  857. }
  858. function restore_email {
  859. if [[ $RESTORE_APP != 'all' ]]; then
  860. if [[ $RESTORE_APP != 'email' ]]; then
  861. return
  862. fi
  863. fi
  864. for d in $SERVER_DIRECTORY/backup/mail/*/ ; do
  865. USERNAME=$(echo "$d" | awk -F '/' '{print $6}')
  866. if [[ $USERNAME != "git" && $USRNAME != "mirrors" ]]; then
  867. if [ -d $SERVER_DIRECTORY/backup/mail/$USERNAME ]; then
  868. if [ ! -d /home/$USERNAME ]; then
  869. ${PROJECT_NAME}-adduser $USERNAME
  870. fi
  871. echo $"Restoring emails for $USERNAME"
  872. restore_directory_from_friend /root/tempmail mail/$USERNAME
  873. if [ ! -d /home/$USERNAME/Maildir ]; then
  874. mkdir /home/$USERNAME/Maildir
  875. fi
  876. tar -xzvf /root/tempmail/root/tempbackupemail/$USERNAME/maildir.tar.gz -C /
  877. if [ ! "$?" = "0" ]; then
  878. exit 927
  879. fi
  880. rm -rf /root/tempmail
  881. fi
  882. fi
  883. done
  884. }
  885. function restore_dlna {
  886. if [[ $RESTORE_APP != 'all' ]]; then
  887. if [[ $RESTORE_APP != 'dlna' ]]; then
  888. return
  889. fi
  890. fi
  891. if [ -d /var/cache/minidlna ]; then
  892. if [ -d $SERVER_DIRECTORY/backup/dlna ]; then
  893. echo $"Restoring DLNA cache"
  894. restore_directory_from_friend /root/tempdlna dlna
  895. cp -r /root/tempdlna/var/cache/minidlna/* /var/cache/minidlna/
  896. if [ ! "$?" = "0" ]; then
  897. exit 982
  898. fi
  899. rm -rf /root/tempdlna
  900. fi
  901. fi
  902. }
  903. # Social key management
  904. # Recover any key fragments and reconstruct the gpg key
  905. ${PROJECT_NAME}-recoverkey -u ${ADMIN_USERNAME} -l $BACKUP_LIST
  906. copy_gpg_keys
  907. restore_configuration
  908. restore_mariadb
  909. restore_letsencrypt
  910. restore_mutt_settings
  911. restore_gpg
  912. restore_procmail
  913. restore_spamassassin
  914. restore_admin_readme
  915. restore_ipfs
  916. restore_ssh_keys
  917. restore_user_config
  918. restore_user_fin
  919. restore_user_local
  920. restore_certs
  921. restore_personal_settings
  922. restore_mailing_list
  923. restore_xmpp
  924. restore_gnu_social
  925. restore_hubzilla
  926. restore_rss_reader
  927. restore_owncloud
  928. restore_gogs
  929. restore_wiki
  930. restore_blog
  931. restore_cjdns
  932. restore_voip
  933. restore_tox
  934. restore_email
  935. restore_dlna
  936. echo $"*** Remote restore was successful ***"
  937. exit 0