freedombone-backup-remote 23KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644
  1. #!/bin/bash
  2. #
  3. # .---. . .
  4. # | | |
  5. # |--- .--. .-. .-. .-.| .-. .--.--. |.-. .-. .--. .-.
  6. # | | (.-' (.-' ( | ( )| | | | )( )| | (.-'
  7. # ' ' --' --' -' - -' ' ' -' -' -' ' - --'
  8. #
  9. # Freedom in the Cloud
  10. #
  11. # Backup to remote servers - the web of backups
  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 General Public License as published by
  19. # the Free Software Foundation, either version 3 of the License, or
  20. # (at your option) any later version.
  21. #
  22. # This program is distributed in the hope that it will be useful,
  23. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  24. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  25. # GNU General Public License for more details.
  26. #
  27. # You should have received a copy of the GNU General Public License
  28. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  29. PROJECT_NAME='freedombone'
  30. COMPLETION_FILE=$HOME/${PROJECT_NAME}-completed.txt
  31. CONFIG_FILE=$HOME/${PROJECT_NAME}.cfg
  32. BACKUP_EXTRA_DIRECTORIES=/root/backup-extra-dirs.csv
  33. ENABLE_VERIFICATION="no"
  34. export TEXTDOMAIN=${PROJECT_NAME}-backup-remote
  35. export TEXTDOMAINDIR="/usr/share/locale"
  36. # Temporary location for data to be backed up to other servers
  37. SERVER_DIRECTORY=/root/remotebackup
  38. ADMIN_USERNAME=$(cat $COMPLETION_FILE | grep "Admin user" | awk -F ':' '{print $2}')
  39. ADMIN_NAME=$(getent passwd $ADMIN_USERNAME | cut -d: -f5 | cut -d, -f1)
  40. ADMIN_EMAIL_ADDRESS=${ADMIN_USERNAME}@${HOSTNAME}
  41. if [ ! -f /etc/ssl/private/backup.key ]; then
  42. echo $"Creating backup key"
  43. ${PROJECT_NAME}-addcert -h backup --dhkey 2048
  44. fi
  45. if [ ! -f /home/${ADMIN_USERNAME}/backup.list ]; then
  46. exit 1
  47. fi
  48. # MariaDB password
  49. DATABASE_PASSWORD=''
  50. if [ -f /root/dbpass ]; then
  51. DATABASE_PASSWORD=$(cat /root/dbpass)
  52. fi
  53. # local directory where the backup will be made
  54. if [ ! -d $SERVER_DIRECTORY ]; then
  55. mkdir $SERVER_DIRECTORY
  56. fi
  57. if [ ! -d $SERVER_DIRECTORY/backup ]; then
  58. mkdir -p $SERVER_DIRECTORY/backup
  59. fi
  60. # The name of a currently suspended site
  61. # Sites are suspended so that verification should work
  62. SUSPENDED_SITE=
  63. function suspend_site {
  64. # suspends a given website
  65. if [[ $ENABLE_VERIFICATION != "yes" ]]; then
  66. return
  67. fi
  68. SUSPENDED_SITE="$1"
  69. nginx_dissite $SUSPENDED_SITE
  70. service nginx reload
  71. }
  72. function restart_site {
  73. # restarts a given website
  74. if [ ! $SUSPENDED_SITE ]; then
  75. return
  76. fi
  77. nginx_ensite $SUSPENDED_SITE
  78. service nginx reload
  79. SUSPENDED_SITE=
  80. }
  81. function backup_directory_to_friend {
  82. BACKUP_KEY_EXISTS=$(gpg --list-keys "$ADMIN_NAME (backup key)")
  83. if [ ! "$?" = "0" ]; then
  84. echo $"Backup key could not be found"
  85. restart_site
  86. exit 43382
  87. fi
  88. ADMIN_BACKUP_KEY_ID=$(gpg --list-keys "$ADMIN_NAME (backup key)" | grep 'pub ' | awk -F ' ' '{print $2}' | awk -F '/' '{print $2}')
  89. if [ ! -d $SERVER_DIRECTORY/backup/${2} ]; then
  90. mkdir -p $SERVER_DIRECTORY/backup/${2}
  91. fi
  92. obnam force-lock -r $SERVER_DIRECTORY/backup/${2} --encrypt-with ${ADMIN_BACKUP_KEY_ID} ${1}
  93. obnam backup -r $SERVER_DIRECTORY/backup/${2} --encrypt-with ${ADMIN_BACKUP_KEY_ID} ${1}
  94. if [[ $ENABLE_VERIFICATION == "yes" ]]; then
  95. obnam verify -r $SERVER_DIRECTORY/backup/${2} --encrypt-with ${ADMIN_BACKUP_KEY_ID} ${1}
  96. if [ ! "$?" = "0" ]; then
  97. if [[ ${1} == "/root/temp"* || ${1} == *"tempbackup" ]]; then
  98. shred -zu /root/temp${2}/*
  99. rm -rf /root/temp${2}
  100. fi
  101. # Send a warning email
  102. echo "Unable to verify ${2}" | mail -s "${PROJECT_NAME} backup to friends" ${ADMIN_EMAIL_ADDRESS}
  103. restart_site
  104. exit 953
  105. fi
  106. fi
  107. obnam forget --keep=30d -r $SERVER_DIRECTORY/backup/${2} --encrypt-with ${ADMIN_BACKUP_KEY_ID}
  108. if [ ! "$?" = "0" ]; then
  109. if [[ ${1} == "/root/temp"* || ${1} == *"tempbackup" ]]; then
  110. shred -zu /root/temp${2}/*
  111. rm -rf /root/temp${2}
  112. fi
  113. # Send a warning email
  114. echo "Unable to backup ${2}" | mail -s "${PROJECT_NAME} backup to friends" ${ADMIN_EMAIL_ADDRESS}
  115. restart_site
  116. exit 853
  117. fi
  118. if [[ ${1} == "/root/temp"* || ${1} == *"tempbackup" ]]; then
  119. shred -zu /root/temp${2}/*
  120. rm -rf /root/temp${2}
  121. fi
  122. }
  123. function backup_database_to_friend {
  124. if [ ${#DATABASE_PASSWORD} -lt 2 ]; then
  125. echo $"No MariaDB password was given"
  126. restart_site
  127. exit 5783
  128. fi
  129. if [ ! -d $SERVER_DIRECTORY/backup/${1} ]; then
  130. mkdir -p $SERVER_DIRECTORY/backup/${1}
  131. fi
  132. if [ ! -d $SERVER_DIRECTORY/backup/${1}data ]; then
  133. mkdir -p $SERVER_DIRECTORY/backup/${1}data
  134. fi
  135. if [ ! -d /root/temp${1}data ]; then
  136. mkdir -p /root/temp${1}data
  137. fi
  138. echo "Obtaining ${1} database backup"
  139. mysqldump --password=$DATABASE_PASSWORD ${1} > /root/temp${1}data/${1}.sql
  140. if [ ! -s /root/temp${1}data/${1}.sql ]; then
  141. echo $"${1} database could not be saved"
  142. shred -zu /root/temp${1}data/*
  143. rm -rf /root/temp${1}data
  144. # Send a warning email
  145. echo $"Unable to export ${1} database" | mail -s $"${PROJECT_NAME} backup to friends" $ADMIN_EMAIL_ADDRESS
  146. restart_site
  147. exit 5738
  148. fi
  149. }
  150. function backup_configuration {
  151. echo $"Backing up ${PROJECT_NAME} configuration files"
  152. if [ ! -d /root/tempbackupconfig ]; then
  153. mkdir -p /root/tempbackupconfig
  154. fi
  155. cp -f $CONFIG_FILE /root/tempbackupconfig
  156. cp -f $COMPLETION_FILE /root/tempbackupconfig
  157. if [ -f $BACKUP_EXTRA_DIRECTORIES ]; then
  158. cp -f $BACKUP_EXTRA_DIRECTORIES /root/tempbackupconfig
  159. fi
  160. backup_directory_to_friend /root/tempbackupconfig config
  161. }
  162. function backup_users {
  163. for d in /home/*/ ; do
  164. USERNAME=$(echo "$d" | awk -F '/' '{print $3}')
  165. if [[ $USERNAME != "git" && $USERNAME != "mirrors" ]]; then
  166. # personal settings
  167. if [ -d /home/$USERNAME/personal ]; then
  168. echo $"Backing up personal settings for $USERNAME"
  169. backup_directory_to_friend /home/$USERNAME/personal personal/$USERNAME
  170. fi
  171. # gpg keys
  172. if [ -d /home/$USERNAME/.gnupg ]; then
  173. echo $"Backing up gpg keys for $USERNAME"
  174. backup_directory_to_friend /home/$USERNAME/.gnupg gnupg/$USERNAME
  175. fi
  176. # ssh keys
  177. if [ -d /home/$USERNAME/.ssh ]; then
  178. echo $"Backing up ssh keys for $USERNAME"
  179. backup_directory_to_friend /home/$USERNAME/.ssh ssh/$USERNAME
  180. fi
  181. # config files
  182. if [ -d /home/$USERNAME/.config ]; then
  183. echo $"Backing up config files for $USERNAME"
  184. backup_directory_to_friend /home/$USERNAME/.config config/$USERNAME
  185. fi
  186. # mutt settings
  187. if [ -f /home/$USERNAME/.muttrc ]; then
  188. echo $"Backing up Mutt settings for $USERNAME"
  189. if [ ! -d /home/$USERNAME/tempbackup ]; then
  190. mkdir -p /home/$USERNAME/tempbackup
  191. fi
  192. cp /home/$USERNAME/.muttrc /home/$USERNAME/tempbackup
  193. if [ -f /etc/Muttrc ]; then
  194. cp /etc/Muttrc /home/$USERNAME/tempbackup
  195. fi
  196. backup_directory_to_friend /home/$USERNAME/tempbackup mutt/$USERNAME
  197. fi
  198. # procmail settings
  199. if [ -f /home/$USERNAME/.procmailrc ]; then
  200. echo $"Backing up procmail settings for $USERNAME"
  201. if [ ! -d /home/$USERNAME/tempbackup ]; then
  202. mkdir -p /home/$USERNAME/tempbackup
  203. fi
  204. cp /home/$USERNAME/.procmailrc /home/$USERNAME/tempbackup
  205. backup_directory_to_friend /home/$USERNAME/tempbackup procmail/$USERNAME
  206. fi
  207. # spamassassin settings
  208. if [ -d /home/$USERNAME/.spamassassin ]; then
  209. echo $"Backing up spamassassin settings for $USERNAME"
  210. backup_directory_to_friend /home/$USERNAME/.spamassassin spamassassin/$USERNAME
  211. fi
  212. # email
  213. if [ -d /home/$USERNAME/Maildir ]; then
  214. echo $"Creating an email archive"
  215. if [ ! -d /root/backupemail/$USERNAME ]; then
  216. mkdir -p /root/backupemail/$USERNAME
  217. fi
  218. tar -czvf /root/backupemail/$USERNAME/maildir.tar.gz /home/$USERNAME/Maildir
  219. echo $"Backing up emails for $USERNAME"
  220. backup_directory_to_friend /root/backupemail/$USERNAME mail/$USERNAME
  221. fi
  222. fi
  223. done
  224. }
  225. function backup_letsencrypt {
  226. if [ -d /etc/letsencrypt ]; then
  227. echo $"Backing up Lets Encrypt settings"
  228. backup_directory_to_friend /etc/letsencrypt letsencrypt
  229. fi
  230. }
  231. function backup_tor {
  232. if [ -d /etc/letsencrypt ]; then
  233. echo $"Backing up Tor settings"
  234. backup_directory_to_friend /var/lib/tor tor
  235. fi
  236. }
  237. function backup_rss_reader {
  238. if grep -q "RSS reader domain" $COMPLETION_FILE; then
  239. RSS_READER_DOMAIN_NAME=$(cat $COMPLETION_FILE | grep "RSS reader domain" | awk -F ':' '{print $2}')
  240. if [ -d /etc/share/ttrss ]; then
  241. suspend_site ${RSS_READER_DOMAIN_NAME}
  242. backup_database_to_friend ttrss
  243. backup_directory_to_friend /root/tempttrssdata ttrssdata
  244. echo $"Backing up RSS reader installation"
  245. backup_directory_to_friend /etc/share/ttrss ttrss
  246. restart_site
  247. else
  248. echo $"RSS reader domain specified but not found in /etc/share/ttrss}"
  249. fi
  250. fi
  251. }
  252. function backup_gnusocial {
  253. if grep -q "GNU Social domain" $COMPLETION_FILE; then
  254. MICROBLOG_DOMAIN_NAME=$(cat $COMPLETION_FILE | grep "GNU Social domain" | awk -F ':' '{print $2}')
  255. if [ -d /var/www/${MICROBLOG_DOMAIN_NAME}/htdocs ]; then
  256. suspend_site ${MICROBLOG_DOMAIN_NAME}
  257. backup_database_to_friend gnusocial
  258. backup_directory_to_friend /root/tempgnusocialdata gnusocialdata
  259. echo $"Backing up GNU social installation"
  260. backup_directory_to_friend /var/www/${MICROBLOG_DOMAIN_NAME}/htdocs gnusocial
  261. restart_site
  262. else
  263. echo $"GNU Social domain specified but not found in /var/www/${MICROBLOG_DOMAIN_NAME}"
  264. fi
  265. fi
  266. }
  267. function backup_hubzilla {
  268. if grep -q "Hubzilla domain" $COMPLETION_FILE; then
  269. HUBZILLA_DOMAIN_NAME=$(cat $COMPLETION_FILE | grep "Hubzilla domain" | awk -F ':' '{print $2}')
  270. if [ -d /var/www/${HUBZILLA_DOMAIN_NAME} ]; then
  271. suspend_site ${HUBZILLA_DOMAIN_NAME}
  272. backup_database_to_friend hubzilla
  273. backup_directory_to_friend /root/temphubzilladata hubzilladata
  274. echo "Backing up Hubzilla installation"
  275. backup_directory_to_friend /var/www/${HUBZILLA_DOMAIN_NAME}/htdocs hubzilla
  276. restart_site
  277. else
  278. echo $"Hubzilla domain specified but not found in /var/www/${HUBZILLA_DOMAIN_NAME}"
  279. exit 2578
  280. fi
  281. fi
  282. }
  283. function backup_owncloud {
  284. if [ -d /etc/owncloud ]; then
  285. OWNCLOUD_DOMAIN_NAME=$(cat $COMPLETION_FILE | grep "Owncloud domain" | awk -F ':' '{print $2}')
  286. suspend_site ${OWNCLOUD_DOMAIN_NAME}
  287. backup_database_to_friend owncloud
  288. backup_directory_to_friend /root/tempownclouddata ownclouddata
  289. echo $"Backing up Owncloud data"
  290. backup_directory_to_friend /var/lib/owncloud owncloud
  291. backup_directory_to_friend /etc/owncloud owncloud2
  292. restart_site
  293. fi
  294. }
  295. function backup_gogs {
  296. if [ -d /home/git/go/src/github.com/gogits ]; then
  297. GIT_DOMAIN_NAME=$(cat $COMPLETION_FILE | grep "Gogs domain" | awk -F ':' '{print $2}')
  298. suspend_site ${GIT_DOMAIN_NAME}
  299. backup_database_to_friend gogs
  300. backup_directory_to_friend /root/tempgogsdata gogsdata
  301. echo $"Obtaining Gogs settings backup"
  302. backup_directory_to_friend /home/git/go/src/github.com/gogits/gogs/custom gogs
  303. echo $"Obtaining Gogs repos backup"
  304. mv /home/git/gogs-repositories/*.git /home/git/gogs-repositories/bob
  305. backup_directory_to_friend /home/git/gogs-repositories gogsrepos
  306. echo $"Obtaining Gogs authorized_keys backup"
  307. backup_directory_to_friend /home/git/.ssh gogsssh
  308. restart_site
  309. fi
  310. }
  311. function backup_wiki {
  312. if [ -d /etc/dokuwiki ]; then
  313. echo $"Backing up wiki"
  314. backup_directory_to_friend /var/lib/dokuwiki wiki
  315. backup_directory_to_friend /etc/dokuwiki wiki2
  316. fi
  317. }
  318. function backup_blog {
  319. if grep -q "Blog domain" $COMPLETION_FILE; then
  320. FULLBLOG_DOMAIN_NAME=$(cat $COMPLETION_FILE | grep "Blog domain" | awk -F ':' '{print $2}')
  321. if [ -d /var/www/${FULLBLOG_DOMAIN_NAME} ]; then
  322. echo $"Backing up blog"
  323. backup_directory_to_friend /var/www/${FULLBLOG_DOMAIN_NAME}/htdocs blog
  324. else
  325. echo $"Blog domain specified but not found in /var/www/${FULLBLOG_DOMAIN_NAME}"
  326. exit 2578
  327. fi
  328. fi
  329. }
  330. function backup_certs {
  331. if [ -d /etc/ssl ]; then
  332. echo $"Backing up certificates"
  333. backup_directory_to_friend /etc/ssl ssl
  334. fi
  335. }
  336. function backup_mailing_list {
  337. if [ -d /var/spool/mlmmj ]; then
  338. echo $"Backing up the public mailing list"
  339. backup_directory_to_friend /var/spool/mlmmj mailinglist
  340. fi
  341. }
  342. function backup_xmpp {
  343. if [ -d /var/lib/prosody ]; then
  344. echo $"Backing up the XMPP settings"
  345. backup_directory_to_friend /var/lib/prosody xmpp
  346. fi
  347. }
  348. function backup_web_server {
  349. if [ -d /etc/nginx ]; then
  350. echo $"Backing up web settings"
  351. backup_directory_to_friend /etc/nginx/sites-available web
  352. fi
  353. }
  354. function backup_admin_readme {
  355. if [ -f /home/$ADMIN_USERNAME/README ]; then
  356. echo $"Backing up README"
  357. if [ ! -d /home/$ADMIN_USERNAME/tempbackup ]; then
  358. mkdir -p /home/$ADMIN_USERNAME/tempbackup
  359. fi
  360. cp -f /home/$ADMIN_USERNAME/README /home/$ADMIN_USERNAME/tempbackup
  361. backup_directory_to_friend /home/$ADMIN_USERNAME/tempbackup readme
  362. fi
  363. }
  364. function backup_ipfs {
  365. if [ -d /home/$ADMIN_USERNAME/.ipfs ]; then
  366. echo $"Backing up IPFS"
  367. backup_directory_to_friend /home/$ADMIN_USERNAME/.ipfs ipfs
  368. fi
  369. }
  370. function backup_dlna {
  371. if [ -d /var/cache/minidlna ]; then
  372. echo $"Backing up DLNA cache"
  373. backup_directory_to_friend /var/cache/minidlna dlna
  374. fi
  375. }
  376. function backup_voip {
  377. if [ -f /etc/mumble-server.ini ]; then
  378. echo $"Backing up VoIP settings"
  379. if [ ! -d /root/tempvoipbackup ]; then
  380. mkdir -p /root/tempvoipbackup
  381. fi
  382. cp -f /etc/mumble-server.ini /root/tempvoipbackup
  383. cp -f /var/lib/mumble-server/mumble-server.sqlite /root/tempvoipbackup
  384. cp -f /etc/sipwitch.conf /root/tempvoipbackup
  385. backup_directory_to_friend /root/tempvoipbackup voip
  386. fi
  387. }
  388. function backup_tox {
  389. if [ -d /var/lib/tox-bootstrapd ]; then
  390. echo "Backing up Tox node settings"
  391. if [ -d /var/lib/tox-bootstrapd/Maildir ]; then
  392. rm -rf /var/lib/tox-bootstrapd/Maildir
  393. fi
  394. cp /etc/tox-bootstrapd.conf /var/lib/tox-bootstrapd
  395. backup_directory_to_friend /var/lib/tox-bootstrapd tox
  396. fi
  397. }
  398. function backup_mariadb {
  399. if [ ${#DATABASE_PASSWORD} -gt 1 ]; then
  400. if [ ! -d /root/tempmariadb ]; then
  401. mkdir /root/tempmariadb
  402. fi
  403. mysqldump --password=$DATABASE_PASSWORD mysql user > /root/tempmariadb/mysql.sql
  404. if [ ! -s /root/tempmariadb/mysql.sql ]; then
  405. echo $"Unable to backup MariaDB settings"
  406. rm -rf /root/tempmariadb
  407. # Send a warning email
  408. echo $"Unable to export database settings" | mail -s "${PROJECT_NAME} backup to friends" $ADMIN_EMAIL_ADDRESS
  409. exit 653
  410. fi
  411. echo "$DATABASE_PASSWORD" > /root/tempmariadb/db
  412. chmod 400 /root/tempmariadb/db
  413. backup_directory_to_friend /root/tempmariadb mariadb
  414. fi
  415. }
  416. # Returns the filename of a key share
  417. function get_key_share {
  418. no_of_shares=$1
  419. USERNAME="$2"
  420. REMOTE_DOMAIN="$3"
  421. # Get a share index based on the supplied domain name
  422. # This ensures that the same share is always given to the same domain
  423. sharenumstr=$(md5sum <<< "$REMOTE_DOMAIN")
  424. share_index=$(echo $((0x${sharenumstr%% *} % ${no_of_shares})) | tr -d -)
  425. # get the filename
  426. share_files=(/home/$USERNAME/.gnupg_fragments/keyshare.asc.*)
  427. share_filename=${share_files[share_index]}
  428. echo "$share_filename"
  429. }
  430. function disperse_key_shares {
  431. USERNAME=$1
  432. REMOTE_DOMAIN=$2
  433. REMOTE_SSH_PORT=$3
  434. REMOTE_PASSWORD=$4
  435. REMOTE_SERVER=$5
  436. if [ -d /home/$USERNAME/.gnupg_fragments ]; then
  437. if [ $REMOTE_DOMAIN ]; then
  438. cd /home/$USERNAME/.gnupg_fragments
  439. no_of_shares=$(ls -afq keyshare.asc.* | wc -l)
  440. if (( no_of_shares > 1 )); then
  441. share_filename=$(get_key_share $no_of_shares "$USERNAME" "$REMOTE_DOMAIN")
  442. # create a temp directory containing the share
  443. temp_key_share_dir=/home/$USERNAME/tempkey
  444. temp_key_share_fragments=$temp_key_share_dir/.gnupg_fragments_${USERNAME}
  445. mkdir -p $temp_key_share_fragments
  446. cp $share_filename $temp_key_share_fragments/
  447. # copy the fragments directory to the remote server
  448. /usr/bin/sshpass -p "$REMOTE_PASSWORD" \
  449. scp -r -P $REMOTE_SSH_PORT $temp_key_share_fragments $REMOTE_SERVER
  450. if [ ! "$?" = "0" ]; then
  451. # Send a warning email
  452. echo "Key share to $REMOTE_SERVER failed" | \
  453. mail -s "${PROJECT_NAME} social key management" $MY_EMAIL_ADDRESS
  454. else
  455. # Send a confirmation email
  456. echo "Key ${share_filename} shared to $REMOTE_SERVER" | \
  457. mail -s "${PROJECT_NAME} social key management" $MY_EMAIL_ADDRESS
  458. fi
  459. # remove the temp file/directory
  460. shred -zu $temp_key_share_fragments/*
  461. rm -rf $temp_key_share_dir
  462. fi
  463. fi
  464. fi
  465. }
  466. function valid_backup_destination {
  467. destination_dir="$1"
  468. is_valid="yes"
  469. if [[ "$destination_dir" == "hubzilla" || \
  470. "$destination_dir" == "hubzilladata" || \
  471. "$destination_dir" == "gogs" || \
  472. "$destination_dir" == "gogsrepos" || \
  473. "$destination_dir" == "gogsssh" || \
  474. "$destination_dir" == "gnusocial" || \
  475. "$destination_dir" == "gnusocialdata" || \
  476. "$destination_dir" == "mariadb" || \
  477. "$destination_dir" == "config" || \
  478. "$destination_dir" == "letsencrypt" || \
  479. "$destination_dir" == "wiki" || \
  480. "$destination_dir" == "wiki2" || \
  481. "$destination_dir" == "xmpp" || \
  482. "$destination_dir" == "ipfs" || \
  483. "$destination_dir" == "dlna" || \
  484. "$destination_dir" == "tox" || \
  485. "$destination_dir" == "ssl" || \
  486. "$destination_dir" == "blog" || \
  487. "$destination_dir" == "owncloud" || \
  488. "$destination_dir" == "owncloud2" || \
  489. "$destination_dir" == "ownclouddata" || \
  490. "$destination_dir" == "mailinglist" ]]; then
  491. is_valid="no"
  492. fi
  493. echo $is_valid
  494. }
  495. function backup_extra_directories {
  496. if [ ! -f $BACKUP_EXTRA_DIRECTORIES ]; then
  497. return
  498. fi
  499. echo $"Backing up some additional directories"
  500. while read backup_line
  501. do
  502. backup_dir=$(echo "$backup_line" | awk -F ',' '{print $1}' | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//')
  503. if [ -d "$backup_dir" ]; then
  504. destination_dir=$(echo "$backup_line" | awk -F ',' '{print $2}' | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//')
  505. if [[ $(valid_backup_destination "$destination_dir") == "yes" ]]; then
  506. backup_directory_to_friend "$backup_dir" "$destination_dir"
  507. else
  508. echo $"WARNING: The backup directory $destination_dir is already used."
  509. echo $"Choose a different destination name for backing up $backup_dir"
  510. fi
  511. else
  512. echo $"WARNING: Directory $backup_dir does not exist"
  513. fi
  514. done <$BACKUP_EXTRA_DIRECTORIES
  515. }
  516. TEST_MODE="no"
  517. if [[ "$1" == "test" ]]; then
  518. TEST_MODE="yes"
  519. fi
  520. backup_configuration
  521. if [[ $TEST_MODE == "no" ]]; then
  522. backup_users
  523. backup_letsencrypt
  524. backup_tor
  525. backup_gnusocial
  526. backup_rss_reader
  527. backup_hubzilla
  528. backup_owncloud
  529. backup_gogs
  530. backup_wiki
  531. backup_blog
  532. backup_certs
  533. backup_mailing_list
  534. backup_xmpp
  535. backup_web_server
  536. backup_admin_readme
  537. backup_ipfs
  538. backup_dlna
  539. backup_voip
  540. backup_tox
  541. backup_mariadb
  542. backup_extra_directories
  543. fi
  544. # For each remote server
  545. while read remote_server
  546. do
  547. # Get the server and its password
  548. # Format is:
  549. # username@domain <port number> /home/username <ssh password>
  550. REMOTE_SERVER=$(echo "${remote_server}" | awk -F ' ' '{print $1}')
  551. if [ $REMOTE_SERVER ]; then
  552. REMOTE_DOMAIN=$(echo "${remote_server}" | awk -F ' ' '{print $1}' | awk -F '@' '{print $2}')
  553. REMOTE_SSH_PORT=$(echo "${remote_server}" | awk -F ' ' '{print $2}')
  554. REMOTE_DIRECTORY=$(echo "${remote_server}" | awk -F ' ' '{print $3}')
  555. REMOTE_PASSWORD=$(echo "${remote_server}" | awk -F ' ' '{print $4}')
  556. NOW=$(date +"%Y-%m-%d %H:%M:%S")
  557. REMOTE_SERVER=$REMOTE_SERVER:$REMOTE_DIRECTORY
  558. echo "$NOW Starting backup to $REMOTE_SERVER" >> /var/log/remotebackups.log
  559. # Social key management
  560. for d in /home/*/ ; do
  561. USERNAME=$(echo "$d" | awk -F '/' '{print $3}')
  562. if [[ $USERNAME != "git" && $USERNAME != "mirrors" ]]; then
  563. disperse_key_shares $USERNAME $REMOTE_DOMAIN $REMOTE_SSH_PORT "$REMOTE_PASSWORD" $REMOTE_SERVER
  564. fi
  565. done
  566. if [[ $TEST_MODE == "yes" ]]; then
  567. echo "rsync -ratlzv --rsh=\"/usr/bin/sshpass -p '$REMOTE_PASSWORD' ssh -p $REMOTE_SSH_PORT -o StrictHostKeyChecking=no\" $SERVER_DIRECTORY/backup $REMOTE_SERVER"
  568. fi
  569. rsync -ratlzv --rsh="/usr/bin/sshpass -p \"$REMOTE_PASSWORD\" ssh -p $REMOTE_SSH_PORT -o StrictHostKeyChecking=no" $SERVER_DIRECTORY/backup $REMOTE_SERVER
  570. if [ ! "$?" = "0" ]; then
  571. echo "$NOW Backup to $REMOTE_SERVER failed" >> /var/log/remotebackups.log
  572. # Send a warning email
  573. echo "Backup to $REMOTE_SERVER failed" | mail -s "${PROJECT_NAME} backup to friends" $ADMIN_EMAIL_ADDRESS
  574. else
  575. echo "$NOW Backed up to $REMOTE_SERVER" >> /var/log/remotebackups.log
  576. fi
  577. fi
  578. done < /home/${ADMIN_USERNAME}/backup.list
  579. exit 0