freedombone-restore-remote 33KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905
  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-2018 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. MONGODB_APPS_FILE=$HOME/.mongodbapps
  33. BACKUP_EXTRA_DIRECTORIES=/root/backup-extra-dirs.csv
  34. export TEXTDOMAIN=${PROJECT_NAME}-restore-remote
  35. export TEXTDOMAINDIR="/usr/share/locale"
  36. UTILS_FILES="/usr/share/${PROJECT_NAME}/utils/${PROJECT_NAME}-utils-*"
  37. for f in $UTILS_FILES
  38. do
  39. source "$f"
  40. done
  41. SERVER_NAME="$1"
  42. # whether to restore everything or just a specific application
  43. RESTORE_APP='all'
  44. if [ "${2}" ]; then
  45. RESTORE_APP="${2}"
  46. fi
  47. ADMIN_USERNAME=$(get_completion_param "Admin user")
  48. ADMIN_EMAIL_ADDRESS=${ADMIN_USERNAME}@${HOSTNAME}
  49. # Temporary location for data to be backed up to other servers
  50. SERVER_DIRECTORY=/root/remoterestore
  51. BACKUP_LIST=/home/${ADMIN_USERNAME}/backup.list
  52. if [ ! "$SERVER_NAME" ]; then
  53. echo $'restorefromfriend [server]'
  54. exit 1
  55. fi
  56. if [ ! -f "$BACKUP_LIST" ]; then
  57. echo $"No friends list found at $BACKUP_LIST"
  58. exit 2
  59. fi
  60. if ! grep -q "$SERVER_NAME" "$BACKUP_LIST"; then
  61. echo $"Server not found within the friends list"
  62. exit 3
  63. fi
  64. REMOTE_SERVER=$(grep -i "$SERVER_NAME" "$BACKUP_LIST" | awk -F ' ' '{print $1}')
  65. REMOTE_SSH_PORT=$(grep -i "$SERVER_NAME" "$BACKUP_LIST" | awk -F ' ' '{print $2}')
  66. REMOTE_DIRECTORY=$(grep -i "$SERVER_NAME" "$BACKUP_LIST" | awk -F ' ' '{print $3}')
  67. REMOTE_PASSWORD=$(grep -i "$SERVER_NAME" "$BACKUP_LIST" | awk -F ' ' '{print $4}')
  68. REMOTE_SERVER=$REMOTE_SERVER:$REMOTE_DIRECTORY
  69. NOW=$(date +"%Y-%m-%d %H:%M:%S")
  70. echo "$NOW Starting restore from $REMOTE_SERVER" >> /var/log/remotebackups.log
  71. if ! rsync -ratlzv --rsh="/usr/bin/sshpass -p $REMOTE_PASSWORD ssh -p $REMOTE_SSH_PORT -o StrictHostKeyChecking=no" "$REMOTE_SERVER/backup" "$SERVER_DIRECTORY"; 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. gpg_set_permissions root
  85. }
  86. function restore_blocklist {
  87. if [[ $RESTORE_APP != 'all' ]]; then
  88. if [[ $RESTORE_APP != 'blocklist' ]]; then
  89. return
  90. fi
  91. fi
  92. if [ -d "$USB_MOUNT/backup/blocklist" ]; then
  93. echo $"Restoring blocklist"
  94. temp_restore_dir=/root/tempblocklist
  95. restore_directory_from_friend $temp_restore_dir blocklist
  96. if [ -d $temp_restore_dir/root/tempbackupblocklist ]; then
  97. cp -f $temp_restore_dir/root/tempbackupblocklist/${PROJECT_NAME}-firewall-domains.cfg /root/${PROJECT_NAME}-firewall-domains.cfg
  98. else
  99. cp -f $temp_restore_dir/${PROJECT_NAME}-firewall-domains.cfg /root/${PROJECT_NAME}-firewall-domains.cfg
  100. fi
  101. rm -rf $temp_restore_dir
  102. firewall_refresh_blocklist
  103. fi
  104. }
  105. function restore_configfiles {
  106. if [[ $RESTORE_APP != 'all' ]]; then
  107. if [[ $RESTORE_APP != 'configfiles' ]]; then
  108. return
  109. fi
  110. fi
  111. if [ -d $SERVER_DIRECTORY/backup/configfiles ]; then
  112. echo $"Restoring configuration files"
  113. temp_restore_dir=/root/tempconfigfiles
  114. restore_directory_from_friend $temp_restore_dir configfiles
  115. if [ -d $temp_restore_dir/root ]; then
  116. if [ -f $temp_restore_dir/root/.nostore ]; then
  117. if [ ! -f /root/.nostore ]; then
  118. touch /root/.nostore
  119. fi
  120. else
  121. if [ -f /root/.nostore ]; then
  122. rm /root/.nostore
  123. fi
  124. fi
  125. else
  126. if [ -f $temp_restore_dir/.nostore ]; then
  127. if [ ! -f /root/.nostore ]; then
  128. touch /root/.nostore
  129. fi
  130. else
  131. if [ -f /root/.nostore ]; then
  132. rm /root/.nostore
  133. fi
  134. fi
  135. fi
  136. #if [ -f $temp_restore_dir$NODEJS_INSTALLED_APPS_FILE ]; then
  137. # cp -f $temp_restore_dir$NODEJS_INSTALLED_APPS_FILE $NODEJS_INSTALLED_APPS_FILE
  138. #fi
  139. #if [ -f $temp_restore_dir/root/${PROJECT_NAME}.cfg ]; then
  140. # cp -f $temp_restore_dir/root/${PROJECT_NAME}.cfg $CONFIGURATION_FILE
  141. # if [ ! "$?" = "0" ]; then
  142. # unmount_drive
  143. # rm -rf $temp_restore_dir
  144. # exit 5372
  145. # fi
  146. #fi
  147. if [ -f "$temp_restore_dir$MONGODB_APPS_FILE" ]; then
  148. if ! cp -f "$temp_restore_dir$MONGODB_APPS_FILE" "$MONGODB_APPS_FILE"; then
  149. unmount_drive
  150. rm -rf $temp_restore_dir
  151. exit 7835335
  152. fi
  153. fi
  154. #if [ -f $CONFIGURATION_FILE ]; then
  155. # # install according to the config file
  156. # freedombone -c $CONFIGURATION_FILE
  157. #fi
  158. #if [ -f $temp_restore_dir/root/${PROJECT_NAME}-completed.txt ]; then
  159. # cp -f $temp_restore_dir/root/${PROJECT_NAME}-completed.txt $COMPLETION_FILE
  160. # if [ ! "$?" = "0" ]; then
  161. # unmount_drive
  162. # rm -rf $temp_restore_dir
  163. # exit 7252
  164. # fi
  165. #fi
  166. if [ -f "${temp_restore_dir}${BACKUP_EXTRA_DIRECTORIES}" ]; then
  167. if ! cp -f "${temp_restore_dir}${BACKUP_EXTRA_DIRECTORIES}" "${BACKUP_EXTRA_DIRECTORIES}"; then
  168. unmount_drive
  169. rm -rf $temp_restore_dir
  170. exit 62121
  171. fi
  172. fi
  173. # restore nginx password hashes
  174. if [ -f $temp_restore_dir/root/htpasswd ]; then
  175. cp -f $temp_restore_dir/root/htpasswd /etc/nginx/.htpasswd
  176. fi
  177. rm -rf $temp_restore_dir
  178. fi
  179. }
  180. function restore_mariadb {
  181. if [[ $RESTORE_APP != 'all' ]]; then
  182. if [[ $RESTORE_APP != 'mariadb' ]]; then
  183. return
  184. fi
  185. fi
  186. if [[ $(is_completed install_mariadb) == "0" ]]; then
  187. function_check install_mariadb
  188. install_mariadb
  189. fi
  190. if [ -d $SERVER_DIRECTORY/backup/mariadb ]; then
  191. echo $"Restoring MariaDB settings"
  192. temp_restore_dir=/root/tempmariadb
  193. restore_directory_from_friend $temp_restore_dir mariadb
  194. store_original_mariadb_password
  195. echo $'Obtaining MariaDB password'
  196. db_pass=$(cat /root/.mariadboriginal)
  197. if [ ${#db_pass} -gt 0 ]; then
  198. echo $"Restore the MariaDB user table"
  199. if [ -d ${temp_restore_dir}${temp_restore_dir} ]; then
  200. mysqlsuccess=$(mysql -u root --password="$db_pass" mysql -o < ${temp_restore_dir}${temp_restore_dir}/mysql.sql)
  201. else
  202. mysqlsuccess=$(mysql -u root --password="$db_pass" mysql -o < ${temp_restore_dir}/mysql.sql)
  203. fi
  204. # shellcheck disable=SC2181
  205. if [ ! "$?" = "0" ]; then
  206. echo $"Try again using the password obtained from backup"
  207. db_pass=$(${PROJECT_NAME}-pass -u root -a mariadb)
  208. if [ -d ${temp_restore_dir}${temp_restore_dir} ]; then
  209. mysqlsuccess=$(mysql -u root --password="$db_pass" mysql -o < ${temp_restore_dir}${temp_restore_dir}/mysql.sql)
  210. else
  211. mysqlsuccess=$(mysql -u root --password="$db_pass" mysql -o < ${temp_restore_dir}/mysql.sql)
  212. fi
  213. fi
  214. # shellcheck disable=SC2181
  215. if [ ! "$?" = "0" ]; then
  216. echo "$mysqlsuccess"
  217. exit 962
  218. fi
  219. echo $"Restarting database"
  220. systemctl restart mariadb
  221. echo $"Ensure MariaDB handles authentication"
  222. MARIADB_PASSWORD=$("${PROJECT_NAME}-pass" -u root -a mariadb)
  223. mariadb_fix_authentication
  224. fi
  225. rm -rf ${temp_restore_dir}
  226. fi
  227. }
  228. function restore_postgresql {
  229. if [[ $RESTORE_APP != 'all' ]]; then
  230. if [[ $RESTORE_APP != 'postgresql' ]]; then
  231. return
  232. fi
  233. fi
  234. if [[ $(is_completed install_postgresql) == "0" ]]; then
  235. function_check install_postgresql
  236. install_postgresql
  237. fi
  238. if [ -d $SERVER_DIRECTORY/backup/postgresql ]; then
  239. echo $"Restoring Postgresql settings"
  240. temp_restore_dir=/root/temppostgresql
  241. restore_directory_from_friend $temp_restore_dir postgresql
  242. store_original_postgresql_password
  243. echo $'Obtaining Postgresql password'
  244. db_pass=$(cat /root/.postgresqloriginal)
  245. if [ ${#db_pass} -gt 0 ]; then
  246. echo $"Restore the Postgresql user table"
  247. if [ -d ${temp_restore_dir}${temp_restore_dir} ]; then
  248. mysqlsuccess=$(sudo -u postgres pg_restore ${temp_restore_dir}${temp_restore_dir}/postgresql.sql)
  249. else
  250. mysqlsuccess=$(sudo -u postgres pg_restore ${temp_restore_dir}/postgresql.sql)
  251. fi
  252. # shellcheck disable=SC2181
  253. if [ ! "$?" = "0" ]; then
  254. echo $"Try again using the password obtained from backup"
  255. db_pass=$("${PROJECT_NAME}-pass" -u root -a postgresql)
  256. if [ -d ${temp_restore_dir}${temp_restore_dir} ]; then
  257. mysqlsuccess=$(sudo -u postgres pg_restore ${temp_restore_dir}${temp_restore_dir}/postgresql.sql)
  258. else
  259. mysqlsuccess=$(sudo -u postgres pg_restore ${temp_restore_dir}/postgresql.sql)
  260. fi
  261. fi
  262. # shellcheck disable=SC2181
  263. if [ ! "$?" = "0" ]; then
  264. echo "$mysqlsuccess"
  265. exit 962
  266. fi
  267. echo $"Restarting database"
  268. systemctl restart postgresql
  269. echo $"Ensure postgresql handles authentication"
  270. POSTGRESQL_PASSWORD=$("${PROJECT_NAME}-pass" -u root -a postgresql)
  271. fi
  272. rm -rf ${temp_restore_dir}
  273. fi
  274. }
  275. function restore_letsencrypt {
  276. if [[ $RESTORE_APP != 'all' ]]; then
  277. if [[ $RESTORE_APP != 'letsencrypt' ]]; then
  278. return
  279. fi
  280. fi
  281. if [ -d $SERVER_DIRECTORY/backup/letsencrypt ]; then
  282. echo $"Restoring Lets Encrypt settings"
  283. restore_directory_from_friend /etc/letsencrypt letsencrypt
  284. fi
  285. }
  286. function restore_passwordstore {
  287. if [[ $RESTORE_APP != 'all' ]]; then
  288. if [[ $RESTORE_APP != 'passwords' ]]; then
  289. return
  290. fi
  291. fi
  292. if [ -d $SERVER_DIRECTORY/backup/passwordstore ]; then
  293. store_original_mariadb_password
  294. echo $"Restoring password store"
  295. restore_directory_from_friend /root/.passwords passwordstore
  296. fi
  297. }
  298. function restore_tor {
  299. if [[ $RESTORE_APP != 'all' ]]; then
  300. if [[ $RESTORE_APP != 'tor' ]]; then
  301. return
  302. fi
  303. fi
  304. if [ -d $SERVER_DIRECTORY/backup/tor ]; then
  305. echo $"Restoring Tor settings"
  306. restore_directory_from_friend /var/lib/tor tor
  307. fi
  308. }
  309. function restore_mutt_settings {
  310. if [[ $RESTORE_APP != 'all' ]]; then
  311. if [[ $RESTORE_APP != 'mutt' ]]; then
  312. return
  313. fi
  314. fi
  315. for d in $SERVER_DIRECTORY/backup/mutt/*/ ; do
  316. USERNAME=$(echo "$d" | awk -F '/' '{print $6}')
  317. # skip over configurations
  318. if [[ "$USERNAME" == *'configs' ]]; then
  319. continue
  320. fi
  321. if [[ $(is_valid_user "$USERNAME") == "1" ]]; then
  322. if [ -d "$SERVER_DIRECTORY/backup/mutt/$USERNAME" ]; then
  323. if [ ! -d "/home/$USERNAME" ]; then
  324. ${PROJECT_NAME}-adduser "$USERNAME"
  325. fi
  326. echo $"Restoring Mutt configurations for $USERNAME"
  327. restore_directory_from_friend "/home/$USERNAME/.mutt" "mutt/${USERNAME}configs"
  328. echo $"Restoring Mutt settings for $USERNAME"
  329. temp_restore_dir=/root/tempmutt
  330. restore_directory_from_friend ${temp_restore_dir} "mutt/$USERNAME"
  331. if [ -d "${temp_restore_dir}/home/$USERNAME/tempbackup" ]; then
  332. if [ -f "${temp_restore_dir}/home/$USERNAME/tempbackup/.muttrc" ]; then
  333. cp -f "${temp_restore_dir}/home/$USERNAME/tempbackup/.muttrc" "/home/$USERNAME/.muttrc"
  334. sed -i '/set sidebar_delim/d' "/home/$USERNAME/.muttrc"
  335. sed -i '/set sidebar_sort/d' "/home/$USERNAME/.muttrc"
  336. fi
  337. if [ -f "${temp_restore_dir}/home/$USERNAME/tempbackup/Muttrc" ]; then
  338. cp -f "${temp_restore_dir}/home/$USERNAME/tempbackup/Muttrc" /etc/Muttrc
  339. sed -i '/set sidebar_delim/d' /etc/Muttrc
  340. sed -i '/set sidebar_sort/d' /etc/Muttrc
  341. fi
  342. else
  343. if [ -f ${temp_restore_dir}/.muttrc ]; then
  344. cp -f "${temp_restore_dir}/.muttrc" "/home/$USERNAME/.muttrc"
  345. sed -i '/set sidebar_delim/d' "/home/$USERNAME/.muttrc"
  346. sed -i '/set sidebar_sort/d' "/home/$USERNAME/.muttrc"
  347. fi
  348. if [ -f ${temp_restore_dir}/Muttrc ]; then
  349. cp -f ${temp_restore_dir}/Muttrc /etc/Muttrc
  350. sed -i '/set sidebar_delim/d' /etc/Muttrc
  351. sed -i '/set sidebar_sort/d' /etc/Muttrc
  352. fi
  353. fi
  354. # shellcheck disable=SC2181
  355. if [ ! "$?" = "0" ]; then
  356. rm -rf ${temp_restore_dir}
  357. exit 276
  358. fi
  359. rm -rf ${temp_restore_dir}
  360. fi
  361. fi
  362. done
  363. }
  364. function restore_gpg {
  365. if [[ $RESTORE_APP != 'gpg' ]]; then
  366. return
  367. fi
  368. for d in $SERVER_DIRECTORY/backup/gnupg/*/ ; do
  369. USERNAME=$(echo "$d" | awk -F '/' '{print $6}')
  370. if [[ $(is_valid_user "$USERNAME") == "1" ]]; then
  371. if [ -d "$SERVER_DIRECTORY/backup/gnupg/$USERNAME" ]; then
  372. if [ ! -d "/home/$USERNAME" ]; then
  373. ${PROJECT_NAME}-adduser "$USERNAME"
  374. fi
  375. echo $"Restoring gnupg settings for $USERNAME"
  376. temp_restore_dir=/root/tempgnupg
  377. restore_directory_from_friend ${temp_restore_dir} "gnupg/$USERNAME"
  378. if [ -d "${temp_restore_dir}/home/$USERNAME/.gnupg" ]; then
  379. cp -r "${temp_restore_dir}/home/$USERNAME/.gnupg" "/home/$USERNAME/"
  380. else
  381. if [ ! -d "/home/$USERNAME/.gnupg" ]; then
  382. mkdir "/home/$USERNAME/.gnupg"
  383. fi
  384. cp -r "${temp_restore_dir}/"* "/home/$USERNAME/.gnupg/"
  385. fi
  386. # shellcheck disable=SC2181
  387. if [ ! "$?" = "0" ]; then
  388. rm -rf ${temp_restore_dir}
  389. exit 276
  390. fi
  391. rm -rf ${temp_restore_dir}
  392. if [[ "$USERNAME" == "$ADMIN_USERNAME" ]]; then
  393. if ! cp -r "/home/$USERNAME/.gnupg" /root; then
  394. exit 283
  395. fi
  396. gpg_set_permissions root
  397. fi
  398. fi
  399. fi
  400. done
  401. }
  402. function restore_procmail {
  403. if [[ $RESTORE_APP != 'all' ]]; then
  404. if [[ $RESTORE_APP != 'procmail' ]]; then
  405. return
  406. fi
  407. fi
  408. for d in $SERVER_DIRECTORY/backup/procmail/*/ ; do
  409. USERNAME=$(echo "$d" | awk -F '/' '{print $6}')
  410. if [[ $(is_valid_user "$USERNAME") == "1" ]]; then
  411. if [ -d "$SERVER_DIRECTORY/backup/procmail/$USERNAME" ]; then
  412. if [ ! -d "/home/$USERNAME" ]; then
  413. ${PROJECT_NAME}-adduser "$USERNAME"
  414. fi
  415. echo $"Restoring procmail settings for $USERNAME"
  416. temp_restore_dir=/root/tempprocmail
  417. restore_directory_from_friend ${temp_restore_dir} "procmail/$USERNAME"
  418. if [ -d "${temp_restore_dir}/home/$USERNAME/tempbackup" ]; then
  419. cp -f "${temp_restore_dir}/home/$USERNAME/tempbackup/.procmailrc" "/home/$USERNAME/"
  420. else
  421. cp -f "${temp_restore_dir}/.procmailrc" "/home/$USERNAME/.procmailrc"
  422. fi
  423. # shellcheck disable=SC2181
  424. if [ ! "$?" = "0" ]; then
  425. rm -rf ${temp_restore_dir}
  426. exit 276
  427. fi
  428. rm -rf ${temp_restore_dir}
  429. fi
  430. fi
  431. done
  432. }
  433. function restore_spamassassin {
  434. if [[ $RESTORE_APP != 'all' ]]; then
  435. if [[ $RESTORE_APP != 'spamassassin' ]]; then
  436. return
  437. fi
  438. fi
  439. for d in $SERVER_DIRECTORY/backup/spamassassin/*/ ; do
  440. USERNAME=$(echo "$d" | awk -F '/' '{print $6}')
  441. if [[ $(is_valid_user "$USERNAME") == "1" ]]; then
  442. if [ -d "$SERVER_DIRECTORY/backup/spamassassin/$USERNAME" ]; then
  443. if [ ! -d "/home/$USERNAME" ]; then
  444. ${PROJECT_NAME}-adduser "$USERNAME"
  445. fi
  446. echo $"Restoring spamassassin settings for $USERNAME"
  447. temp_restore_dir=/root/tempspamassassin
  448. restore_directory_from_friend $temp_restore_dir "spamassassin/$USERNAME"
  449. if [ -d "$temp_restore_dir/home/$USERNAME" ]; then
  450. cp -rf "$temp_restore_dir/home/$USERNAME/.spamassassin" "/home/$USERNAME/"
  451. else
  452. if [ ! -d "/home/$USERNAME/.spamassassin" ]; then
  453. mkdir "/home/$USERNAME/.spamassassin"
  454. fi
  455. cp -rf $temp_restore_dir/* "/home/$USERNAME/.spamassassin/"
  456. fi
  457. # shellcheck disable=SC2181
  458. if [ ! "$?" = "0" ]; then
  459. rm -rf $temp_restore_dir
  460. exit 276
  461. fi
  462. rm -rf $temp_restore_dir
  463. fi
  464. fi
  465. done
  466. }
  467. function restore_admin_readme {
  468. if [[ $RESTORE_APP != 'all' ]]; then
  469. if [[ $RESTORE_APP != 'readme' ]]; then
  470. return
  471. fi
  472. fi
  473. if [ -d $SERVER_DIRECTORY/backup/readme ]; then
  474. echo $"Restoring README"
  475. temp_restore_dir=/root/tempreadme
  476. restore_directory_from_friend $temp_restore_dir readme
  477. if [ -d "$temp_restore_dir/home/$ADMIN_USERNAME/tempbackup" ]; then
  478. cp -f "$temp_restore_dir/home/$ADMIN_USERNAME/tempbackup/README" "/home/$ADMIN_USERNAME/"
  479. else
  480. cp -f "$temp_restore_dir/README" "/home/$ADMIN_USERNAME/README"
  481. fi
  482. # shellcheck disable=SC2181
  483. if [ ! "$?" = "0" ]; then
  484. rm -rf $temp_restore_dir
  485. exit 276
  486. fi
  487. rm -rf $temp_restore_dir
  488. fi
  489. }
  490. function restore_ssh_keys {
  491. if [[ $RESTORE_APP != 'all' ]]; then
  492. if [[ $RESTORE_APP != 'ssh' ]]; then
  493. return
  494. fi
  495. fi
  496. for d in $SERVER_DIRECTORY/backup/ssh/*/ ; do
  497. USERNAME=$(echo "$d" | awk -F '/' '{print $6}')
  498. if [[ $(is_valid_user "$USERNAME") == "1" ]]; then
  499. if [ -d "$SERVER_DIRECTORY/backup/ssh/$USERNAME" ]; then
  500. if [ ! -d "/home/$USERNAME" ]; then
  501. ${PROJECT_NAME}-adduser "$USERNAME"
  502. fi
  503. echo $"Restoring ssh keys for $USERNAME"
  504. temp_restore_dir=/root/tempssh
  505. restore_directory_from_friend $temp_restore_dir "ssh/$USERNAME"
  506. if [ -d "$temp_restore_dir/home/$USERNAME/.ssh" ]; then
  507. cp -r "$temp_restore_dir/home/$USERNAME/.ssh" "/home/$USERNAME/"
  508. else
  509. if [ ! -d "/home/$USERNAME/.ssh" ]; then
  510. mkdir "/home/$USERNAME/.ssh"
  511. fi
  512. cp -r $temp_restore_dir/* "/home/$USERNAME/.ssh/"
  513. fi
  514. # shellcheck disable=SC2181
  515. if [ ! "$?" = "0" ]; then
  516. rm -rf $temp_restore_dir
  517. exit 664
  518. fi
  519. rm -rf $temp_restore_dir
  520. fi
  521. fi
  522. done
  523. }
  524. function restore_user_config {
  525. if [[ $RESTORE_APP != 'all' ]]; then
  526. if [[ $RESTORE_APP != 'userconfig' ]]; then
  527. return
  528. fi
  529. fi
  530. for d in $SERVER_DIRECTORY/backup/config/*/ ; do
  531. USERNAME=$(echo "$d" | awk -F '/' '{print $6}')
  532. if [[ $(is_valid_user "$USERNAME") == "1" ]]; then
  533. if [ -d "$SERVER_DIRECTORY/backup/config/$USERNAME" ]; then
  534. if [ ! -d "/home/$USERNAME" ]; then
  535. ${PROJECT_NAME}-adduser "$USERNAME"
  536. fi
  537. echo $"Restoring config files for $USERNAME"
  538. temp_restore_dir=/root/tempconfig
  539. restore_directory_from_friend $temp_restore_dir "config/$USERNAME"
  540. if [ -d "$temp_restore_dir/home/$USERNAME" ]; then
  541. cp -r "$temp_restore_dir/home/$USERNAME/.config" "/home/$USERNAME/"
  542. else
  543. if [ ! -d "/home/$USERNAME/.config" ]; then
  544. mkdir "/home/$USERNAME/.config"
  545. fi
  546. cp -r "$temp_restore_dir/"* "/home/$USERNAME/.config/"
  547. fi
  548. # shellcheck disable=SC2181
  549. if [ ! "$?" = "0" ]; then
  550. rm -rf $temp_restore_dir
  551. exit 664
  552. fi
  553. rm -rf $temp_restore_dir
  554. fi
  555. fi
  556. done
  557. }
  558. function restore_user_monkeysphere {
  559. if [[ $RESTORE_APP != 'all' ]]; then
  560. if [[ $RESTORE_APP != 'usermonkeysphere' ]]; then
  561. return
  562. fi
  563. fi
  564. for d in $SERVER_DIRECTORY/backup/monkeysphere/*/ ; do
  565. USERNAME=$(echo "$d" | awk -F '/' '{print $6}')
  566. if [[ $(is_valid_user "$USERNAME") == "1" ]]; then
  567. if [ -d "$SERVER_DIRECTORY/backup/monkeysphere/$USERNAME" ]; then
  568. if [ ! -d "/home/$USERNAME" ]; then
  569. ${PROJECT_NAME}-adduser "$USERNAME"
  570. fi
  571. echo $"Restoring monkeysphere ids for $USERNAME"
  572. temp_restore_dir=/root/tempmonkeysphere
  573. restore_directory_from_friend $temp_restore_dir "monkeysphere/$USERNAME"
  574. if [ -d "$temp_restore_dir/home/$USERNAME/.monkeysphere" ]; then
  575. cp -r "$temp_restore_dir/home/$USERNAME/.monkeysphere" "/home/$USERNAME/"
  576. else
  577. if [ ! -d "/home/$USERNAME/.monkeysphere" ]; then
  578. mkdir "/home/$USERNAME/.monkeysphere"
  579. fi
  580. cp -r $temp_restore_dir/* "/home/$USERNAME/.monkeysphere/"
  581. fi
  582. # shellcheck disable=SC2181
  583. if [ ! "$?" = "0" ]; then
  584. rm -rf $temp_restore_dir
  585. exit 664
  586. fi
  587. rm -rf $temp_restore_dir
  588. fi
  589. fi
  590. # The admin user is the identity certifier
  591. MY_EMAIL_ADDRESS="${ADMIN_USERNAME}@${HOSTNAME}"
  592. read_config_param MY_EMAIL_ADDRESS
  593. MY_GPG_PUBLIC_KEY_ID=$(gpg_pubkey_from_email "$ADMIN_USERNAME" "$MY_EMAIL_ADDRESS")
  594. fpr=$(gpg --with-colons --fingerprint "$MY_GPG_PUBLIC_KEY_ID" | grep fpr | head -n 1 | awk -F ':' '{print $10}')
  595. monkeysphere-authentication add-identity-certifier "$fpr"
  596. monkeysphere-authentication update-users
  597. done
  598. }
  599. function restore_user_fin {
  600. if [[ $RESTORE_APP != 'all' ]]; then
  601. if [[ $RESTORE_APP != 'userfin' ]]; then
  602. return
  603. fi
  604. fi
  605. for d in $SERVER_DIRECTORY/backup/fin/*/ ; do
  606. USERNAME=$(echo "$d" | awk -F '/' '{print $6}')
  607. if [[ $(is_valid_user "$USERNAME") == "1" ]]; then
  608. if [ -d "$SERVER_DIRECTORY/backup/fin/$USERNAME" ]; then
  609. if [ ! -d "/home/$USERNAME" ]; then
  610. ${PROJECT_NAME}-adduser "$USERNAME"
  611. fi
  612. echo $"Restoring fin files for $USERNAME"
  613. temp_restore_dir=/root/tempfin
  614. restore_directory_from_friend $temp_restore_dir "fin/$USERNAME"
  615. if [ -d "$temp_restore_dir/home/$USERNAME/.fin" ]; then
  616. cp -r "$temp_restore_dir/home/$USERNAME/.fin" "/home/$USERNAME/"
  617. else
  618. if [ ! -d "/home/$USERNAME/.fin" ]; then
  619. mkdir "/home/$USERNAME/.fin"
  620. fi
  621. cp -r $temp_restore_dir/* "/home/$USERNAME/.fin/"
  622. fi
  623. # shellcheck disable=SC2181
  624. if [ ! "$?" = "0" ]; then
  625. rm -rf $temp_restore_dir
  626. exit 664
  627. fi
  628. rm -rf $temp_restore_dir
  629. fi
  630. fi
  631. done
  632. }
  633. function restore_user_local {
  634. if [[ $RESTORE_APP != 'all' ]]; then
  635. if [[ $RESTORE_APP != 'userlocal' ]]; then
  636. return
  637. fi
  638. fi
  639. for d in $SERVER_DIRECTORY/backup/local/*/ ; do
  640. USERNAME=$(echo "$d" | awk -F '/' '{print $6}')
  641. if [[ $(is_valid_user "$USERNAME") == "1" ]]; then
  642. if [ -d "$SERVER_DIRECTORY/backup/local/$USERNAME" ]; then
  643. if [ ! -d "/home/$USERNAME" ]; then
  644. ${PROJECT_NAME}-adduser "$USERNAME"
  645. fi
  646. echo $"Restoring local files for $USERNAME"
  647. temp_restore_dir=/root/templocal
  648. restore_directory_from_friend $temp_restore_dir "local/$USERNAME"
  649. if [ -d "$temp_restore_dir/home/$USERNAME/.local" ]; then
  650. cp -r "$temp_restore_dir/home/$USERNAME/.local" "/home/$USERNAME/"
  651. else
  652. if [ ! -d "/home/$USERNAME/.local" ]; then
  653. mkdir "/home/$USERNAME/.local"
  654. fi
  655. cp -r $temp_restore_dir/* "/home/$USERNAME/.local/"
  656. fi
  657. # shellcheck disable=SC2181
  658. if [ ! "$?" = "0" ]; then
  659. rm -rf $temp_restore_dir
  660. exit 664
  661. fi
  662. rm -rf $temp_restore_dir
  663. fi
  664. fi
  665. done
  666. }
  667. function restore_certs {
  668. if [[ $RESTORE_APP != 'all' ]]; then
  669. if [[ $RESTORE_APP != 'certs' ]]; then
  670. return
  671. fi
  672. fi
  673. if [ -d $SERVER_DIRECTORY/backup/ssl ]; then
  674. echo $"Restoring certificates"
  675. restore_directory_from_friend /root/tempssl ssl
  676. if [ -d /root/tempssl/etc/ssl ]; then
  677. cp -r /root/tempssl/etc/ssl/* /etc/ssl
  678. else
  679. cp -r /root/tempssl/* /etc/ssl/
  680. fi
  681. # shellcheck disable=SC2181
  682. if [ ! "$?" = "0" ]; then
  683. exit 276
  684. fi
  685. rm -rf /root/tempssl
  686. update-ca-certificates
  687. # restore ownership
  688. if [ -f /etc/ssl/private/xmpp.key ]; then
  689. chown prosody:prosody /etc/ssl/private/xmpp.key
  690. chown prosody:prosody /etc/ssl/certs/xmpp.*
  691. fi
  692. if [ -d /etc/dovecot ]; then
  693. chown root:dovecot /etc/ssl/private/dovecot.*
  694. chown root:dovecot /etc/ssl/certs/dovecot.*
  695. fi
  696. if [ -f /etc/ssl/private/exim.key ]; then
  697. cp /etc/ssl/private/exim.key /etc/exim4
  698. cp /etc/ssl/certs/exim.crt /etc/exim4
  699. cp /etc/ssl/certs/exim.dhparam /etc/exim4
  700. chown root:Debian-exim /etc/exim4/exim.key /etc/exim4/exim.crt /etc/exim4/exim.dhparam
  701. chmod 640 /etc/exim4/exim.key /etc/exim4/exim.crt /etc/exim4/exim.dhparam
  702. fi
  703. if [ -f /etc/ssl/private/mumble.key ]; then
  704. if [ -d /var/lib/mumble-server ]; then
  705. cp /etc/ssl/certs/mumble.* /var/lib/mumble-server
  706. cp /etc/ssl/private/mumble.key /var/lib/mumble-server
  707. chown -R mumble-server:mumble-server /var/lib/mumble-server
  708. fi
  709. fi
  710. fi
  711. }
  712. function restore_personal_settings {
  713. if [[ $RESTORE_APP != 'all' ]]; then
  714. if [[ $RESTORE_APP != 'personal' ]]; then
  715. return
  716. fi
  717. fi
  718. for d in $SERVER_DIRECTORY/backup/personal/*/ ; do
  719. USERNAME=$(echo "$d" | awk -F '/' '{print $6}')
  720. if [[ $(is_valid_user "$USERNAME") == "1" ]]; then
  721. if [ -d "$SERVER_DIRECTORY/backup/personal/$USERNAME" ]; then
  722. if [ ! -d "/home/$USERNAME" ]; then
  723. ${PROJECT_NAME}-adduser "$USERNAME"
  724. fi
  725. echo $"Restoring personal settings for $USERNAME"
  726. temp_restore_dir=/root/temppersonal
  727. restore_directory_from_friend $temp_restore_dir "personal/$USERNAME"
  728. if [ -d "$temp_restore_dir/home/$USERNAME/personal" ]; then
  729. if [ -d "/home/$USERNAME/personal" ]; then
  730. rm -rf "/home/$USERNAME/personal"
  731. fi
  732. # shellcheck disable=SC2086
  733. mv $temp_restore_dir/home/$USERNAME/personal /home/$USERNAME
  734. else
  735. if [ ! -d "/home/$USERNAME/personal" ]; then
  736. mkdir "/home/$USERNAME/personal"
  737. fi
  738. cp -r $temp_restore_dir/* "/home/$USERNAME/personal/"
  739. fi
  740. # shellcheck disable=SC2181
  741. if [ ! "$?" = "0" ]; then
  742. exit 18437643
  743. fi
  744. rm -rf $temp_restore_dir
  745. fi
  746. fi
  747. done
  748. }
  749. function restore_mailing_list {
  750. if [[ $RESTORE_APP != 'all' ]]; then
  751. if [[ $RESTORE_APP != 'mailinglist' ]]; then
  752. return
  753. fi
  754. fi
  755. if [ -d /var/spool/mlmmj ]; then
  756. echo $"Restoring public mailing list"
  757. temp_restore_dir=/root/tempmailinglist
  758. restore_directory_from_friend $temp_restore_dir mailinglist
  759. if [ -d $temp_restore_dir/root/spool/mlmmj ]; then
  760. cp -r $temp_restore_dir/root/spool/mlmmj/* /var/spool/mlmmj
  761. else
  762. cp -r $temp_restore_dir/* /var/spool/mlmmj/
  763. fi
  764. # shellcheck disable=SC2181
  765. if [ ! "$?" = "0" ]; then
  766. exit 526
  767. fi
  768. rm -rf $temp_restore_dir
  769. fi
  770. }
  771. function restore_email {
  772. if [[ $RESTORE_APP != 'all' ]]; then
  773. if [[ $RESTORE_APP != 'email' ]]; then
  774. return
  775. fi
  776. fi
  777. for d in $SERVER_DIRECTORY/backup/mail/*/ ; do
  778. USERNAME=$(echo "$d" | awk -F '/' '{print $6}')
  779. if [[ $(is_valid_user "$USERNAME") == "1" ]]; then
  780. if [ -d "$SERVER_DIRECTORY/backup/mail/$USERNAME" ]; then
  781. if [ ! -d "/home/$USERNAME" ]; then
  782. ${PROJECT_NAME}-adduser "$USERNAME"
  783. fi
  784. echo $"Restoring emails for $USERNAME"
  785. temp_restore_dir=/root/tempmail
  786. restore_directory_from_friend $temp_restore_dir "mail/$USERNAME"
  787. if [ ! -d "/home/$USERNAME/Maildir" ]; then
  788. mkdir "/home/$USERNAME/Maildir"
  789. fi
  790. if [ -d "$temp_restore_dir/root/tempbackupemail/$USERNAME" ]; then
  791. tar -xzvf "$temp_restore_dir/root/tempbackupemail/$USERNAME/maildir.tar.gz" -C /
  792. else
  793. tar -xzvf $temp_restore_dir/maildir.tar.gz -C /
  794. fi
  795. # shellcheck disable=SC2181
  796. if [ ! "$?" = "0" ]; then
  797. exit 927
  798. fi
  799. rm -rf $temp_restore_dir
  800. fi
  801. fi
  802. done
  803. }
  804. # Social key management
  805. # Recover any key fragments and reconstruct the gpg key
  806. ${PROJECT_NAME}-recoverkey -u "${ADMIN_USERNAME}" -l "$BACKUP_LIST"
  807. copy_gpg_keys
  808. gpg_agent_setup root
  809. restore_blocklist
  810. restore_configfiles
  811. restore_passwordstore
  812. restore_mariadb
  813. restore_postgresql
  814. restore_letsencrypt
  815. restore_mutt_settings
  816. restore_gpg
  817. restore_procmail
  818. restore_spamassassin
  819. restore_admin_readme
  820. restore_ssh_keys
  821. restore_user_config
  822. restore_user_monkeysphere
  823. restore_user_fin
  824. restore_user_local
  825. restore_certs
  826. restore_personal_settings
  827. restore_mailing_list
  828. restore_email
  829. restore_apps remote
  830. set_user_permissions
  831. update_default_domain
  832. # ensure that all TLS certificates are pinned
  833. #${PROJECT_NAME}-pin-cert all
  834. echo $"*** Remote restore was successful ***"
  835. exit 0