freedombone-restore-remote 24KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688
  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@freedombone.net>
  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. CONFIGURATION_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. UTILS_FILES=/usr/share/${PROJECT_NAME}/utils/${PROJECT_NAME}-utils-*
  36. for f in $UTILS_FILES
  37. do
  38. source $f
  39. done
  40. SERVER_NAME=$1
  41. # whether to restore everything or just a specific application
  42. RESTORE_APP='all'
  43. if [ ${2} ]; then
  44. RESTORE_APP=${2}
  45. fi
  46. ADMIN_USERNAME=$(get_completion_param "Admin user")
  47. ADMIN_EMAIL_ADDRESS=${ADMIN_USERNAME}@${HOSTNAME}
  48. # Temporary location for data to be backed up to other servers
  49. SERVER_DIRECTORY=/root/remoterestore
  50. BACKUP_LIST=/home/${ADMIN_USERNAME}/backup.list
  51. if [ ! $SERVER_NAME ]; then
  52. echo $'restorefromfriend [server]'
  53. exit 1
  54. fi
  55. if [ ! -f $BACKUP_LIST ]; then
  56. echo $"No friends list found at $BACKUP_LIST"
  57. exit 2
  58. fi
  59. if ! grep -q "$SERVER_NAME" $BACKUP_LIST; then
  60. echo $"Server not found within the friends list"
  61. exit 3
  62. fi
  63. REMOTE_SERVER=$(grep -i "$SERVER_NAME" $BACKUP_LIST | awk -F ' ' '{print $1}')
  64. REMOTE_SSH_PORT=$(grep -i "$SERVER_NAME" $BACKUP_LIST | awk -F ' ' '{print $2}')
  65. REMOTE_DIRECTORY=$(grep -i "$SERVER_NAME" $BACKUP_LIST | awk -F ' ' '{print $3}')
  66. REMOTE_PASSWORD=$(grep -i "$SERVER_NAME" $BACKUP_LIST | awk -F ' ' '{print $4}')
  67. REMOTE_SERVER=$REMOTE_SERVER:$REMOTE_DIRECTORY
  68. NOW=$(date +"%Y-%m-%d %H:%M:%S")
  69. echo "$NOW Starting restore from $REMOTE_SERVER" >> /var/log/remotebackups.log
  70. rsync -ratlzv --rsh="/usr/bin/sshpass -p $REMOTE_PASSWORD ssh -p $REMOTE_SSH_PORT -o StrictHostKeyChecking=no" $REMOTE_SERVER/backup $SERVER_DIRECTORY
  71. if [ ! "$?" = "0" ]; then
  72. echo "$NOW Restore from $REMOTE_SERVER failed" >> /var/log/remotebackups.log
  73. # Send a warning email
  74. echo "Restore from $REMOTE_SERVER failed" | mail -s "${PROJECT_NAME} restore from friend" $ADMIN_EMAIL_ADDRESS
  75. exit 790
  76. else
  77. echo "$NOW Restored encrypted data from $REMOTE_SERVER" >> /var/log/remotebackups.log
  78. fi
  79. # MariaDB password
  80. DATABASE_PASSWORD=$(${PROJECT_NAME}-pass -u root -a mariadb)
  81. function copy_gpg_keys {
  82. echo $"Copying GPG keys from admin user to root"
  83. cp -r /home/$ADMIN_USERNAME/.gnupg /root
  84. }
  85. function restore_configfiles {
  86. if [[ $RESTORE_APP != 'all' ]]; then
  87. if [[ $RESTORE_APP != 'configfiles' ]]; then
  88. return
  89. fi
  90. fi
  91. if [ -d $SERVER_DIRECTORY/backup/config ]; then
  92. echo $"Restoring configuration files"
  93. temp_restore_dir=/root/tempconfig
  94. restore_directory_from_friend $temp_restore_dir configfiles
  95. if [ -f $temp_restore_dir/root/.nostore ]; then
  96. if [ ! -f /root/.nostore ]; then
  97. touch /root/.nostore
  98. fi
  99. else
  100. if [ -f /root/.nostore ]; then
  101. rm /root/.nostore
  102. fi
  103. fi
  104. if [ -f $temp_restore_dir$NODEJS_INSTALLED_APPS_FILE ]; then
  105. cp -f $temp_restore_dir$NODEJS_INSTALLED_APPS_FILE $NODEJS_INSTALLED_APPS_FILE
  106. fi
  107. if [ -f $temp_restore_dir/root/${PROJECT_NAME}.cfg ]; then
  108. cp -f $temp_restore_dir/root/${PROJECT_NAME}.cfg $CONFIGURATION_FILE
  109. if [ ! "$?" = "0" ]; then
  110. unmount_drive
  111. rm -rf $temp_restore_dir
  112. exit 5372
  113. fi
  114. fi
  115. if [ -f $CONFIGURATION_FILE ]; then
  116. # install according to the config file
  117. freedombone -c $CONFIGURATION_FILE
  118. fi
  119. if [ -f $temp_restore_dir/root/${PROJECT_NAME}-completed.txt ]; then
  120. cp -f $temp_restore_dir/root/${PROJECT_NAME}-completed.txt $COMPLETION_FILE
  121. if [ ! "$?" = "0" ]; then
  122. unmount_drive
  123. rm -rf $temp_restore_dir
  124. exit 7252
  125. fi
  126. fi
  127. if [ -f ${temp_restore_dir}${BACKUP_EXTRA_DIRECTORIES} ]; then
  128. cp -f ${temp_restore_dir}${BACKUP_EXTRA_DIRECTORIES} ${BACKUP_EXTRA_DIRECTORIES}
  129. if [ ! "$?" = "0" ]; then
  130. unmount_drive
  131. rm -rf $temp_restore_dir
  132. exit 62121
  133. fi
  134. fi
  135. # restore nginx password hashes
  136. if [ -f $temp_restore_dir/root/htpasswd ]; then
  137. cp -f $temp_restore_dir/root/htpasswd /etc/nginx/.htpasswd
  138. fi
  139. rm -rf $temp_restore_dir
  140. fi
  141. }
  142. function restore_mariadb {
  143. if [[ $RESTORE_APP != 'all' ]]; then
  144. if [[ $RESTORE_APP != 'mariadb' ]]; then
  145. return
  146. fi
  147. fi
  148. if [[ $(is_completed install_mariadb) == "0" ]]; then
  149. function_check install_mariadb
  150. install_mariadb
  151. fi
  152. if [ -d $SERVER_DIRECTORY/backup/mariadb ]; then
  153. echo $"Restoring MariaDB settings"
  154. temp_restore_dir=/root/tempmariadb
  155. restore_directory_from_friend $temp_restore_dir mariadb
  156. echo $'Obtaining MariaDB password'
  157. db_pass=$(${PROJECT_NAME}-pass -u root -a mariadb)
  158. if [ ${#db_pass} -gt 0 ]; then
  159. echo $"Restore the MariaDB user table"
  160. mysqlsuccess=$(mysql -u root --password="$DATABASE_PASSWORD" mysql -o < ${temp_restore_dir}${temp_restore_dir}/mysql.sql)
  161. if [ ! "$?" = "0" ]; then
  162. echo $"Try again using the password obtained from backup"
  163. mysqlsuccess=$(mysql -u root --password="$db_pass" mysql -o < ${temp_restore_dir}${temp_restore_dir}/mysql.sql)
  164. fi
  165. if [ ! "$?" = "0" ]; then
  166. echo "$mysqlsuccess"
  167. exit 962
  168. fi
  169. echo $"Restarting database"
  170. service mysql restart
  171. echo $"Change the MariaDB password to the backup version"
  172. DATABASE_PASSWORD="$db_pass"
  173. ${PROJECT_NAME}-pass -u root -a mariadb -p "$DATABASE_PASSWORD"
  174. fi
  175. rm -rf ${temp_restore_dir}
  176. fi
  177. }
  178. function restore_letsencrypt {
  179. if [[ $RESTORE_APP != 'all' ]]; then
  180. if [[ $RESTORE_APP != 'letsencrypt' ]]; then
  181. return
  182. fi
  183. fi
  184. if [ -d $SERVER_DIRECTORY/backup/letsencrypt ]; then
  185. echo $"Restoring Lets Encrypt settings"
  186. restore_directory_from_friend / letsencrypt
  187. fi
  188. }
  189. function restore_passwordstore {
  190. if [[ $RESTORE_APP != 'all' ]]; then
  191. if [[ $RESTORE_APP != 'passwords' ]]; then
  192. return
  193. fi
  194. fi
  195. if [ -d $SERVER_DIRECTORY/backup/passwordstore ]; then
  196. echo $"Restoring password store"
  197. restore_directory_from_friend / passwordstore
  198. fi
  199. }
  200. function restore_tor {
  201. if [[ $RESTORE_APP != 'all' ]]; then
  202. if [[ $RESTORE_APP != 'tor' ]]; then
  203. return
  204. fi
  205. fi
  206. if [ -d $SERVER_DIRECTORY/backup/tor ]; then
  207. echo $"Restoring Tor settings"
  208. restore_directory_from_friend / tor
  209. fi
  210. }
  211. function restore_mutt_settings {
  212. if [[ $RESTORE_APP != 'all' ]]; then
  213. if [[ $RESTORE_APP != 'mutt' ]]; then
  214. return
  215. fi
  216. fi
  217. for d in $SERVER_DIRECTORY/backup/mutt/*/ ; do
  218. USERNAME=$(echo "$d" | awk -F '/' '{print $6}')
  219. if [[ $(is_valid_user "$USERNAME") == "1" ]]; then
  220. if [ -d $SERVER_DIRECTORY/backup/mutt/$USERNAME ]; then
  221. if [ ! -d /home/$USERNAME ]; then
  222. ${PROJECT_NAME}-adduser $USERNAME
  223. fi
  224. echo $"Restoring Mutt settings for $USERNAME"
  225. temp_restore_dir=/root/tempmutt
  226. restore_directory_from_friend ${temp_restore_dir} mutt/$USERNAME
  227. if [ -f ${temp_restore_dir}/home/$USERNAME/tempbackup/.muttrc ]; then
  228. cp -f ${temp_restore_dir}/home/$USERNAME/tempbackup/.muttrc /home/$USERNAME/.muttrc
  229. fi
  230. if [ -f ${temp_restore_dir}/home/$USERNAME/tempbackup/Muttrc ]; then
  231. cp -f ${temp_restore_dir}/home/$USERNAME/tempbackup/Muttrc /etc/Muttrc
  232. fi
  233. if [ ! "$?" = "0" ]; then
  234. rm -rf ${temp_restore_dir}
  235. exit 276
  236. fi
  237. rm -rf ${temp_restore_dir}
  238. fi
  239. fi
  240. done
  241. }
  242. function restore_gpg {
  243. if [[ $RESTORE_APP != 'all' ]]; then
  244. if [[ $RESTORE_APP != 'gpg' ]]; then
  245. return
  246. fi
  247. fi
  248. for d in $SERVER_DIRECTORY/backup/gnupg/*/ ; do
  249. USERNAME=$(echo "$d" | awk -F '/' '{print $6}')
  250. if [[ $(is_valid_user "$USERNAME") == "1" ]]; then
  251. if [ -d $SERVER_DIRECTORY/backup/gnupg/$USERNAME ]; then
  252. if [ ! -d /home/$USERNAME ]; then
  253. ${PROJECT_NAME}-adduser $USERNAME
  254. fi
  255. echo $"Restoring gnupg settings for $USERNAME"
  256. temp_restore_dir=/root/tempgnupg
  257. restore_directory_from_friend ${temp_restore_dir} gnupg/$USERNAME
  258. cp -r ${temp_restore_dir}/home/$USERNAME/.gnupg /home/$USERNAME/
  259. if [ ! "$?" = "0" ]; then
  260. rm -rf ${temp_restore_dir}
  261. exit 276
  262. fi
  263. rm -rf ${temp_restore_dir}
  264. if [[ "$USERNAME" == "$ADMIN_USERNAME" ]]; then
  265. cp -r /home/$USERNAME/.gnupg /root
  266. if [ ! "$?" = "0" ]; then
  267. exit 283
  268. fi
  269. fi
  270. fi
  271. fi
  272. done
  273. }
  274. function restore_procmail {
  275. if [[ $RESTORE_APP != 'all' ]]; then
  276. if [[ $RESTORE_APP != 'procmail' ]]; then
  277. return
  278. fi
  279. fi
  280. for d in $SERVER_DIRECTORY/backup/procmail/*/ ; do
  281. USERNAME=$(echo "$d" | awk -F '/' '{print $6}')
  282. if [[ $(is_valid_user "$USERNAME") == "1" ]]; then
  283. if [ -d $SERVER_DIRECTORY/backup/procmail/$USERNAME ]; then
  284. if [ ! -d /home/$USERNAME ]; then
  285. ${PROJECT_NAME}-adduser $USERNAME
  286. fi
  287. echo $"Restoring procmail settings for $USERNAME"
  288. temp_restore_dir=/root/tempprocmail
  289. restore_directory_from_friend ${temp_restore_dir} procmail/$USERNAME
  290. cp -f ${temp_restore_dir}/home/$USERNAME/tempbackup/.procmailrc /home/$USERNAME/
  291. if [ ! "$?" = "0" ]; then
  292. rm -rf ${temp_restore_dir}
  293. exit 276
  294. fi
  295. rm -rf ${temp_restore_dir}
  296. fi
  297. fi
  298. done
  299. }
  300. function restore_spamassassin {
  301. if [[ $RESTORE_APP != 'all' ]]; then
  302. if [[ $RESTORE_APP != 'spamassassin' ]]; then
  303. return
  304. fi
  305. fi
  306. for d in $SERVER_DIRECTORY/backup/spamassassin/*/ ; do
  307. USERNAME=$(echo "$d" | awk -F '/' '{print $6}')
  308. if [[ $(is_valid_user "$USERNAME") == "1" ]]; then
  309. if [ -d $SERVER_DIRECTORY/backup/spamassassin/$USERNAME ]; then
  310. if [ ! -d /home/$USERNAME ]; then
  311. ${PROJECT_NAME}-adduser $USERNAME
  312. fi
  313. echo $"Restoring spamassassin settings for $USERNAME"
  314. temp_restore_dir=/root/tempspamassassin
  315. restore_directory_from_friend $temp_restore_dir spamassassin/$USERNAME
  316. cp -rf $temp_restore_dir/home/$USERNAME/.spamassassin /home/$USERNAME/
  317. if [ ! "$?" = "0" ]; then
  318. rm -rf $temp_restore_dir
  319. exit 276
  320. fi
  321. rm -rf $temp_restore_dir
  322. fi
  323. fi
  324. done
  325. }
  326. function restore_admin_readme {
  327. if [[ $RESTORE_APP != 'all' ]]; then
  328. if [[ $RESTORE_APP != 'readme' ]]; then
  329. return
  330. fi
  331. fi
  332. if [ -d $SERVER_DIRECTORY/backup/readme ]; then
  333. echo $"Restoring README"
  334. temp_restore_dir=/root/tempreadme
  335. restore_directory_from_friend $temp_restore_dir readme
  336. cp -f $temp_restore_dir/home/$ADMIN_USERNAME/tempbackup/README /home/$ADMIN_USERNAME/
  337. if [ ! "$?" = "0" ]; then
  338. rm -rf $temp_restore_dir
  339. exit 276
  340. fi
  341. rm -rf $temp_restore_dir
  342. fi
  343. }
  344. function restore_ssh_keys {
  345. if [[ $RESTORE_APP != 'all' ]]; then
  346. if [[ $RESTORE_APP != 'ssh' ]]; then
  347. return
  348. fi
  349. fi
  350. for d in $SERVER_DIRECTORY/backup/ssh/*/ ; do
  351. USERNAME=$(echo "$d" | awk -F '/' '{print $6}')
  352. if [[ $(is_valid_user "$USERNAME") == "1" ]]; then
  353. if [ -d $SERVER_DIRECTORY/backup/ssh/$USERNAME ]; then
  354. if [ ! -d /home/$USERNAME ]; then
  355. ${PROJECT_NAME}-adduser $USERNAME
  356. fi
  357. echo $"Restoring ssh keys for $USERNAME"
  358. temp_restore_dir=/root/tempssh
  359. restore_directory_from_friend $temp_restore_dir ssh/$USERNAME
  360. cp -r $temp_restore_dir/home/$USERNAME/.ssh /home/$USERNAME/
  361. if [ ! "$?" = "0" ]; then
  362. rm -rf $temp_restore_dir
  363. exit 664
  364. fi
  365. rm -rf $temp_restore_dir
  366. fi
  367. fi
  368. done
  369. }
  370. function restore_user_config {
  371. if [[ $RESTORE_APP != 'all' ]]; then
  372. if [[ $RESTORE_APP != 'userconfig' ]]; then
  373. return
  374. fi
  375. fi
  376. for d in $SERVER_DIRECTORY/backup/config/*/ ; do
  377. USERNAME=$(echo "$d" | awk -F '/' '{print $6}')
  378. if [[ $(is_valid_user "$USERNAME") == "1" ]]; then
  379. if [ -d $SERVER_DIRECTORY/backup/config/$USERNAME ]; then
  380. if [ ! -d /home/$USERNAME ]; then
  381. ${PROJECT_NAME}-adduser $USERNAME
  382. fi
  383. echo $"Restoring config files for $USERNAME"
  384. temp_restore_dir=/root/tempconfig
  385. restore_directory_from_friend $temp_restore_dir config/$USERNAME
  386. cp -r $temp_restore_dir/home/$USERNAME/.config /home/$USERNAME/
  387. if [ ! "$?" = "0" ]; then
  388. rm -rf $temp_restore_dir
  389. exit 664
  390. fi
  391. rm -rf $temp_restore_dir
  392. fi
  393. fi
  394. done
  395. }
  396. function gpg_pubkey_from_email {
  397. key_owner_username=$1
  398. key_email_address=$2
  399. key_id=
  400. if [[ $key_owner_username != "root" ]]; then
  401. key_id=$(su -c "gpg --list-keys $key_email_address | grep 'pub '" - $key_owner_username | awk -F ' ' '{print $2}' | awk -F '/' '{print $2}')
  402. else
  403. key_id=$(gpg --list-keys $key_email_address | grep 'pub ' | awk -F ' ' '{print $2}' | awk -F '/' '{print $2}')
  404. fi
  405. echo $key_id
  406. }
  407. function restore_user_monkeysphere {
  408. if [[ $RESTORE_APP != 'all' ]]; then
  409. if [[ $RESTORE_APP != 'usermonkeysphere' ]]; then
  410. return
  411. fi
  412. fi
  413. for d in $SERVER_DIRECTORY/backup/monkeysphere/*/ ; do
  414. USERNAME=$(echo "$d" | awk -F '/' '{print $6}')
  415. if [[ $(is_valid_user "$USERNAME") == "1" ]]; then
  416. if [ -d $SERVER_DIRECTORY/backup/monkeysphere/$USERNAME ]; then
  417. if [ ! -d /home/$USERNAME ]; then
  418. ${PROJECT_NAME}-adduser $USERNAME
  419. fi
  420. echo $"Restoring monkeysphere ids for $USERNAME"
  421. temp_restore_dir=/root/tempmonkeysphere
  422. restore_directory_from_friend $temp_restore_dir monkeysphere/$USERNAME
  423. cp -r $temp_restore_dir/home/$USERNAME/.monkeysphere /home/$USERNAME/
  424. if [ ! "$?" = "0" ]; then
  425. rm -rf $temp_restore_dir
  426. exit 664
  427. fi
  428. rm -rf $temp_restore_dir
  429. fi
  430. fi
  431. # The admin user is the identity certifier
  432. MY_EMAIL_ADDRESS="${ADMIN_USERNAME}@${HOSTNAME}"
  433. read_config_param MY_EMAIL_ADDRESS
  434. MY_GPG_PUBLIC_KEY_ID=$(gpg_pubkey_from_email "$ADMIN_USERNAME" "$MY_EMAIL_ADDRESS")
  435. fpr=$(gpg --with-colons --fingerprint $MY_GPG_PUBLIC_KEY_ID | grep fpr | head -n 1 | awk -F ':' '{print $10}')
  436. monkeysphere-authentication add-identity-certifier $fpr
  437. monkeysphere-authentication update-users
  438. done
  439. }
  440. function restore_user_fin {
  441. if [[ $RESTORE_APP != 'all' ]]; then
  442. if [[ $RESTORE_APP != 'userfin' ]]; then
  443. return
  444. fi
  445. fi
  446. for d in $SERVER_DIRECTORY/backup/fin/*/ ; do
  447. USERNAME=$(echo "$d" | awk -F '/' '{print $6}')
  448. if [[ $(is_valid_user "$USERNAME") == "1" ]]; then
  449. if [ -d $SERVER_DIRECTORY/backup/fin/$USERNAME ]; then
  450. if [ ! -d /home/$USERNAME ]; then
  451. ${PROJECT_NAME}-adduser $USERNAME
  452. fi
  453. echo $"Restoring fin files for $USERNAME"
  454. temp_restore_dir=/root/tempfin
  455. restore_directory_from_friend $temp_restore_dir fin/$USERNAME
  456. cp -r $temp_restore_dir/home/$USERNAME/.fin /home/$USERNAME/
  457. if [ ! "$?" = "0" ]; then
  458. rm -rf $temp_restore_dir
  459. exit 664
  460. fi
  461. rm -rf $temp_restore_dir
  462. fi
  463. fi
  464. done
  465. }
  466. function restore_user_local {
  467. if [[ $RESTORE_APP != 'all' ]]; then
  468. if [[ $RESTORE_APP != 'userlocal' ]]; then
  469. return
  470. fi
  471. fi
  472. for d in $SERVER_DIRECTORY/backup/local/*/ ; do
  473. USERNAME=$(echo "$d" | awk -F '/' '{print $6}')
  474. if [[ $(is_valid_user "$USERNAME") == "1" ]]; then
  475. if [ -d $SERVER_DIRECTORY/backup/local/$USERNAME ]; then
  476. if [ ! -d /home/$USERNAME ]; then
  477. ${PROJECT_NAME}-adduser $USERNAME
  478. fi
  479. echo $"Restoring local files for $USERNAME"
  480. temp_restore_dir=/root/templocal
  481. restore_directory_from_friend $temp_restore_dir local/$USERNAME
  482. cp -r $temp_restore_dir/home/$USERNAME/.local /home/$USERNAME/
  483. if [ ! "$?" = "0" ]; then
  484. rm -rf $temp_restore_dir
  485. exit 664
  486. fi
  487. rm -rf $temp_restore_dir
  488. fi
  489. fi
  490. done
  491. }
  492. function restore_certs {
  493. if [[ $RESTORE_APP != 'all' ]]; then
  494. if [[ $RESTORE_APP != 'certs' ]]; then
  495. return
  496. fi
  497. fi
  498. if [ -d $SERVER_DIRECTORY/backup/ssl ]; then
  499. echo $"Restoring certificates"
  500. restore_directory_from_friend /root/tempssl ssl
  501. cp -r /root/tempssl/etc/ssl/* /etc/ssl
  502. if [ ! "$?" = "0" ]; then
  503. exit 276
  504. fi
  505. rm -rf /root/tempssl
  506. # restore ownership
  507. if [ -f /etc/ssl/private/xmpp.key ]; then
  508. chown prosody:prosody /etc/ssl/private/xmpp.key
  509. chown prosody:prosody /etc/ssl/certs/xmpp.*
  510. fi
  511. if [ -d /etc/dovecot ]; then
  512. chown root:dovecot /etc/ssl/private/dovecot.*
  513. chown root:dovecot /etc/ssl/certs/dovecot.*
  514. fi
  515. if [ -f /etc/ssl/private/exim.key ]; then
  516. cp /etc/ssl/private/exim.key /etc/exim4
  517. cp /etc/ssl/certs/exim.crt /etc/exim4
  518. cp /etc/ssl/certs/exim.dhparam /etc/exim4
  519. chown root:Debian-exim /etc/exim4/exim.key /etc/exim4/exim.crt /etc/exim4/exim.dhparam
  520. chmod 640 /etc/exim4/exim.key /etc/exim4/exim.crt /etc/exim4/exim.dhparam
  521. fi
  522. if [ -f /etc/ssl/private/mumble.key ]; then
  523. if [ -d /var/lib/mumble-server ]; then
  524. cp /etc/ssl/certs/mumble.* /var/lib/mumble-server
  525. cp /etc/ssl/private/mumble.key /var/lib/mumble-server
  526. chown -R mumble-server:mumble-server /var/lib/mumble-server
  527. fi
  528. fi
  529. fi
  530. }
  531. function restore_personal_settings {
  532. if [[ $RESTORE_APP != 'all' ]]; then
  533. if [[ $RESTORE_APP != 'personal' ]]; then
  534. return
  535. fi
  536. fi
  537. for d in $SERVER_DIRECTORY/backup/personal/*/ ; do
  538. USERNAME=$(echo "$d" | awk -F '/' '{print $6}')
  539. if [[ $(is_valid_user "$USERNAME") == "1" ]]; then
  540. if [ -d $SERVER_DIRECTORY/backup/personal/$USERNAME ]; then
  541. if [ ! -d /home/$USERNAME ]; then
  542. ${PROJECT_NAME}-adduser $USERNAME
  543. fi
  544. echo $"Restoring personal settings for $USERNAME"
  545. temp_restore_dir=/root/temppersonal
  546. restore_directory_from_friend $temp_restore_dir personal/$USERNAME
  547. if [ -d /home/$USERNAME/personal ]; then
  548. rm -rf /home/$USERNAME/personal
  549. fi
  550. mv $temp_restore_dir/home/$USERNAME/personal /home/$USERNAME
  551. if [ ! "$?" = "0" ]; then
  552. exit 184
  553. fi
  554. rm -rf $temp_restore_dir
  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. temp_restore_dir=/root/tempmailinglist
  568. restore_directory_from_friend $temp_restore_dir mailinglist
  569. cp -r $temp_restore_dir/root/spool/mlmmj/* /var/spool/mlmmj
  570. if [ ! "$?" = "0" ]; then
  571. exit 526
  572. fi
  573. rm -rf $temp_restore_dir
  574. fi
  575. }
  576. function restore_email {
  577. if [[ $RESTORE_APP != 'all' ]]; then
  578. if [[ $RESTORE_APP != 'email' ]]; then
  579. return
  580. fi
  581. fi
  582. for d in $SERVER_DIRECTORY/backup/mail/*/ ; do
  583. USERNAME=$(echo "$d" | awk -F '/' '{print $6}')
  584. if [[ $(is_valid_user "$USERNAME") == "1" ]]; then
  585. if [ -d $SERVER_DIRECTORY/backup/mail/$USERNAME ]; then
  586. if [ ! -d /home/$USERNAME ]; then
  587. ${PROJECT_NAME}-adduser $USERNAME
  588. fi
  589. echo $"Restoring emails for $USERNAME"
  590. temp_restore_dir=/root/tempmail
  591. restore_directory_from_friend $temp_restore_dir mail/$USERNAME
  592. if [ ! -d /home/$USERNAME/Maildir ]; then
  593. mkdir /home/$USERNAME/Maildir
  594. fi
  595. tar -xzvf $temp_restore_dir/root/tempbackupemail/$USERNAME/maildir.tar.gz -C /
  596. if [ ! "$?" = "0" ]; then
  597. exit 927
  598. fi
  599. rm -rf $temp_restore_dir
  600. fi
  601. fi
  602. done
  603. }
  604. # Social key management
  605. # Recover any key fragments and reconstruct the gpg key
  606. ${PROJECT_NAME}-recoverkey -u ${ADMIN_USERNAME} -l $BACKUP_LIST
  607. copy_gpg_keys
  608. restore_configfiles
  609. restore_passwordstore
  610. restore_mariadb
  611. restore_letsencrypt
  612. restore_mutt_settings
  613. restore_gpg
  614. restore_procmail
  615. restore_spamassassin
  616. restore_admin_readme
  617. restore_ssh_keys
  618. restore_user_config
  619. restore_user_monkeysphere
  620. restore_user_fin
  621. restore_user_local
  622. restore_certs
  623. restore_personal_settings
  624. restore_mailing_list
  625. restore_email
  626. restore_apps remote
  627. set_user_permissions
  628. update_default_domain
  629. # ensure that all TLS certificates are pinned
  630. #${PROJECT_NAME}-pin-cert all
  631. echo $"*** Remote restore was successful ***"
  632. exit 0