freedombone-backup-remote 24KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662
  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 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. 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. # nginx password hashes
  161. if [ -f /etc/nginx/.htpasswd ]; then
  162. cp -f /etc/nginx/.htpasswd /root/tempbackupconfig/htpasswd
  163. fi
  164. backup_directory_to_friend /root/tempbackupconfig config
  165. }
  166. function backup_users {
  167. for d in /home/*/ ; do
  168. USERNAME=$(echo "$d" | awk -F '/' '{print $3}')
  169. if [[ $USERNAME != "git" && $USERNAME != "mirrors" ]]; then
  170. # personal settings
  171. if [ -d /home/$USERNAME/personal ]; then
  172. echo $"Backing up personal settings for $USERNAME"
  173. backup_directory_to_friend /home/$USERNAME/personal personal/$USERNAME
  174. fi
  175. # gpg keys
  176. if [ -d /home/$USERNAME/.gnupg ]; then
  177. echo $"Backing up gpg keys for $USERNAME"
  178. backup_directory_to_friend /home/$USERNAME/.gnupg gnupg/$USERNAME
  179. fi
  180. # ssh keys
  181. if [ -d /home/$USERNAME/.ssh ]; then
  182. echo $"Backing up ssh keys for $USERNAME"
  183. backup_directory_to_friend /home/$USERNAME/.ssh ssh/$USERNAME
  184. fi
  185. # config files
  186. if [ -d /home/$USERNAME/.config ]; then
  187. echo $"Backing up config files for $USERNAME"
  188. backup_directory_to_friend /home/$USERNAME/.config config/$USERNAME
  189. fi
  190. # fin files
  191. if [ -d /home/$USERNAME/.fin ]; then
  192. echo $"Backing up fin files for $USERNAME"
  193. backup_directory_to_friend /home/$USERNAME/.fin fin/$USERNAME
  194. fi
  195. # local files
  196. if [ -d /home/$USERNAME/.local ]; then
  197. echo $"Backing up local files for $USERNAME"
  198. backup_directory_to_friend /home/$USERNAME/.local local/$USERNAME
  199. fi
  200. # mutt settings
  201. if [ -f /home/$USERNAME/.muttrc ]; then
  202. echo $"Backing up Mutt settings for $USERNAME"
  203. if [ ! -d /home/$USERNAME/tempbackup ]; then
  204. mkdir -p /home/$USERNAME/tempbackup
  205. fi
  206. cp /home/$USERNAME/.muttrc /home/$USERNAME/tempbackup
  207. if [ -f /etc/Muttrc ]; then
  208. cp /etc/Muttrc /home/$USERNAME/tempbackup
  209. fi
  210. backup_directory_to_friend /home/$USERNAME/tempbackup mutt/$USERNAME
  211. fi
  212. # procmail settings
  213. if [ -f /home/$USERNAME/.procmailrc ]; then
  214. echo $"Backing up procmail settings for $USERNAME"
  215. if [ ! -d /home/$USERNAME/tempbackup ]; then
  216. mkdir -p /home/$USERNAME/tempbackup
  217. fi
  218. cp /home/$USERNAME/.procmailrc /home/$USERNAME/tempbackup
  219. backup_directory_to_friend /home/$USERNAME/tempbackup procmail/$USERNAME
  220. fi
  221. # spamassassin settings
  222. if [ -d /home/$USERNAME/.spamassassin ]; then
  223. echo $"Backing up spamassassin settings for $USERNAME"
  224. backup_directory_to_friend /home/$USERNAME/.spamassassin spamassassin/$USERNAME
  225. fi
  226. # email
  227. if [ -d /home/$USERNAME/Maildir ]; then
  228. echo $"Creating an email archive"
  229. if [ ! -d /root/backupemail/$USERNAME ]; then
  230. mkdir -p /root/backupemail/$USERNAME
  231. fi
  232. tar -czvf /root/backupemail/$USERNAME/maildir.tar.gz /home/$USERNAME/Maildir
  233. echo $"Backing up emails for $USERNAME"
  234. backup_directory_to_friend /root/backupemail/$USERNAME mail/$USERNAME
  235. fi
  236. fi
  237. done
  238. }
  239. function backup_letsencrypt {
  240. if [ -d /etc/letsencrypt ]; then
  241. echo $"Backing up Lets Encrypt settings"
  242. backup_directory_to_friend /etc/letsencrypt letsencrypt
  243. fi
  244. }
  245. function backup_tor {
  246. if [ -d /etc/letsencrypt ]; then
  247. echo $"Backing up Tor settings"
  248. backup_directory_to_friend /var/lib/tor tor
  249. fi
  250. }
  251. function backup_rss_reader {
  252. if grep -q "RSS reader domain" $COMPLETION_FILE; then
  253. RSS_READER_DOMAIN_NAME=$(cat $COMPLETION_FILE | grep "RSS reader domain" | awk -F ':' '{print $2}')
  254. if [ -d /etc/share/tt-rss ]; then
  255. suspend_site ${RSS_READER_DOMAIN_NAME}
  256. backup_database_to_friend ttrss
  257. backup_directory_to_friend /root/tempttrssdata ttrssdata
  258. echo $"Backing up RSS reader installation"
  259. backup_directory_to_friend /etc/share/tt-rss ttrss
  260. restart_site
  261. else
  262. echo $"RSS reader domain specified but not found in /etc/share/ttrss}"
  263. fi
  264. fi
  265. }
  266. function backup_gnusocial {
  267. if grep -q "GNU Social domain" $COMPLETION_FILE; then
  268. MICROBLOG_DOMAIN_NAME=$(cat $COMPLETION_FILE | grep "GNU Social domain" | awk -F ':' '{print $2}')
  269. if [ -d /var/www/${MICROBLOG_DOMAIN_NAME}/htdocs ]; then
  270. suspend_site ${MICROBLOG_DOMAIN_NAME}
  271. backup_database_to_friend gnusocial
  272. backup_directory_to_friend /root/tempgnusocialdata gnusocialdata
  273. echo $"Backing up GNU social installation"
  274. backup_directory_to_friend /var/www/${MICROBLOG_DOMAIN_NAME}/htdocs gnusocial
  275. restart_site
  276. else
  277. echo $"GNU Social domain specified but not found in /var/www/${MICROBLOG_DOMAIN_NAME}"
  278. fi
  279. fi
  280. }
  281. function backup_hubzilla {
  282. if grep -q "Hubzilla domain" $COMPLETION_FILE; then
  283. HUBZILLA_DOMAIN_NAME=$(cat $COMPLETION_FILE | grep "Hubzilla domain" | awk -F ':' '{print $2}')
  284. if [ -d /var/www/${HUBZILLA_DOMAIN_NAME} ]; then
  285. suspend_site ${HUBZILLA_DOMAIN_NAME}
  286. backup_database_to_friend hubzilla
  287. backup_directory_to_friend /root/temphubzilladata hubzilladata
  288. echo "Backing up Hubzilla installation"
  289. backup_directory_to_friend /var/www/${HUBZILLA_DOMAIN_NAME}/htdocs hubzilla
  290. restart_site
  291. else
  292. echo $"Hubzilla domain specified but not found in /var/www/${HUBZILLA_DOMAIN_NAME}"
  293. exit 2578
  294. fi
  295. fi
  296. }
  297. function backup_owncloud {
  298. if [ -d /etc/owncloud ]; then
  299. OWNCLOUD_DOMAIN_NAME=$(cat $COMPLETION_FILE | grep "Owncloud domain" | awk -F ':' '{print $2}')
  300. suspend_site ${OWNCLOUD_DOMAIN_NAME}
  301. backup_database_to_friend owncloudrepo
  302. backup_directory_to_friend /root/tempowncloudrepodata owncloudrepodata
  303. echo $"Backing up Owncloud data"
  304. if [ -d /var/www/${OWNCLOUD_DOMAIN_NAME}/htdocs/data ]; then
  305. backup_directory_to_friend /var/www/${OWNCLOUD_DOMAIN_NAME}/htdocs/data owncloudrepofiles
  306. fi
  307. backup_directory_to_friend /var/www/${OWNCLOUD_DOMAIN_NAME}/htdocs/config owncloudrepoconfig
  308. restart_site
  309. fi
  310. }
  311. function backup_gogs {
  312. if [ -d /home/git/go/src/github.com/gogits ]; then
  313. GIT_DOMAIN_NAME=$(cat $COMPLETION_FILE | grep "Gogs domain" | awk -F ':' '{print $2}')
  314. suspend_site ${GIT_DOMAIN_NAME}
  315. backup_database_to_friend gogs
  316. backup_directory_to_friend /root/tempgogsdata gogsdata
  317. echo $"Obtaining Gogs settings backup"
  318. backup_directory_to_friend /home/git/go/src/github.com/gogits/gogs/custom gogs
  319. echo $"Obtaining Gogs repos backup"
  320. mv /home/git/gogs-repositories/*.git /home/git/gogs-repositories/bob
  321. backup_directory_to_friend /home/git/gogs-repositories gogsrepos
  322. echo $"Obtaining Gogs authorized_keys backup"
  323. backup_directory_to_friend /home/git/.ssh gogsssh
  324. restart_site
  325. fi
  326. }
  327. function backup_wiki {
  328. if [ -d /etc/dokuwiki ]; then
  329. echo $"Backing up wiki"
  330. backup_directory_to_friend /var/lib/dokuwiki wiki
  331. backup_directory_to_friend /etc/dokuwiki wiki2
  332. fi
  333. }
  334. function backup_blog {
  335. if grep -q "Blog domain" $COMPLETION_FILE; then
  336. FULLBLOG_DOMAIN_NAME=$(cat $COMPLETION_FILE | grep "Blog domain" | awk -F ':' '{print $2}')
  337. if [ -d /var/www/${FULLBLOG_DOMAIN_NAME} ]; then
  338. echo $"Backing up blog"
  339. backup_directory_to_friend /var/www/${FULLBLOG_DOMAIN_NAME}/htdocs blog
  340. else
  341. echo $"Blog domain specified but not found in /var/www/${FULLBLOG_DOMAIN_NAME}"
  342. exit 2578
  343. fi
  344. fi
  345. }
  346. function backup_certs {
  347. if [ -d /etc/ssl ]; then
  348. echo $"Backing up certificates"
  349. backup_directory_to_friend /etc/ssl ssl
  350. fi
  351. }
  352. function backup_mailing_list {
  353. if [ -d /var/spool/mlmmj ]; then
  354. echo $"Backing up the public mailing list"
  355. backup_directory_to_friend /var/spool/mlmmj mailinglist
  356. fi
  357. }
  358. function backup_xmpp {
  359. if [ -d /var/lib/prosody ]; then
  360. echo $"Backing up the XMPP settings"
  361. backup_directory_to_friend /var/lib/prosody xmpp
  362. fi
  363. }
  364. function backup_web_server {
  365. if [ -d /etc/nginx ]; then
  366. echo $"Backing up web settings"
  367. backup_directory_to_friend /etc/nginx/sites-available web
  368. fi
  369. }
  370. function backup_admin_readme {
  371. if [ -f /home/$ADMIN_USERNAME/README ]; then
  372. echo $"Backing up README"
  373. if [ ! -d /home/$ADMIN_USERNAME/tempbackup ]; then
  374. mkdir -p /home/$ADMIN_USERNAME/tempbackup
  375. fi
  376. cp -f /home/$ADMIN_USERNAME/README /home/$ADMIN_USERNAME/tempbackup
  377. backup_directory_to_friend /home/$ADMIN_USERNAME/tempbackup readme
  378. fi
  379. }
  380. function backup_ipfs {
  381. if [ -d /home/$ADMIN_USERNAME/.ipfs ]; then
  382. echo $"Backing up IPFS"
  383. backup_directory_to_friend /home/$ADMIN_USERNAME/.ipfs ipfs
  384. fi
  385. }
  386. function backup_dlna {
  387. if [ -d /var/cache/minidlna ]; then
  388. echo $"Backing up DLNA cache"
  389. backup_directory_to_friend /var/cache/minidlna dlna
  390. fi
  391. }
  392. function backup_voip {
  393. if [ -f /etc/mumble-server.ini ]; then
  394. echo $"Backing up VoIP settings"
  395. if [ ! -d /root/tempvoipbackup ]; then
  396. mkdir -p /root/tempvoipbackup
  397. fi
  398. cp -f /etc/mumble-server.ini /root/tempvoipbackup
  399. cp -f /var/lib/mumble-server/mumble-server.sqlite /root/tempvoipbackup
  400. cp -f /etc/sipwitch.conf /root/tempvoipbackup
  401. backup_directory_to_friend /root/tempvoipbackup voip
  402. fi
  403. }
  404. function backup_tox {
  405. if [ -d /var/lib/tox-bootstrapd ]; then
  406. echo "Backing up Tox node settings"
  407. if [ -d /var/lib/tox-bootstrapd/Maildir ]; then
  408. rm -rf /var/lib/tox-bootstrapd/Maildir
  409. fi
  410. cp /etc/tox-bootstrapd.conf /var/lib/tox-bootstrapd
  411. backup_directory_to_friend /var/lib/tox-bootstrapd tox
  412. fi
  413. }
  414. function backup_mariadb {
  415. if [ ${#DATABASE_PASSWORD} -gt 1 ]; then
  416. if [ ! -d /root/tempmariadb ]; then
  417. mkdir /root/tempmariadb
  418. fi
  419. mysqldump --password=$DATABASE_PASSWORD mysql user > /root/tempmariadb/mysql.sql
  420. if [ ! -s /root/tempmariadb/mysql.sql ]; then
  421. echo $"Unable to backup MariaDB settings"
  422. rm -rf /root/tempmariadb
  423. # Send a warning email
  424. echo $"Unable to export database settings" | mail -s "${PROJECT_NAME} backup to friends" $ADMIN_EMAIL_ADDRESS
  425. exit 653
  426. fi
  427. echo "$DATABASE_PASSWORD" > /root/tempmariadb/db
  428. chmod 400 /root/tempmariadb/db
  429. backup_directory_to_friend /root/tempmariadb mariadb
  430. fi
  431. }
  432. # Returns the filename of a key share
  433. function get_key_share {
  434. no_of_shares=$1
  435. USERNAME="$2"
  436. REMOTE_DOMAIN="$3"
  437. # Get a share index based on the supplied domain name
  438. # This ensures that the same share is always given to the same domain
  439. sharenumstr=$(md5sum <<< "$REMOTE_DOMAIN")
  440. share_index=$(echo $((0x${sharenumstr%% *} % ${no_of_shares})) | tr -d -)
  441. # get the filename
  442. share_files=(/home/$USERNAME/.gnupg_fragments/keyshare.asc.*)
  443. share_filename=${share_files[share_index]}
  444. echo "$share_filename"
  445. }
  446. function disperse_key_shares {
  447. USERNAME=$1
  448. REMOTE_DOMAIN=$2
  449. REMOTE_SSH_PORT=$3
  450. REMOTE_PASSWORD=$4
  451. REMOTE_SERVER=$5
  452. if [ -d /home/$USERNAME/.gnupg_fragments ]; then
  453. if [ $REMOTE_DOMAIN ]; then
  454. cd /home/$USERNAME/.gnupg_fragments
  455. no_of_shares=$(ls -afq keyshare.asc.* | wc -l)
  456. if (( no_of_shares > 1 )); then
  457. share_filename=$(get_key_share $no_of_shares "$USERNAME" "$REMOTE_DOMAIN")
  458. # create a temp directory containing the share
  459. temp_key_share_dir=/home/$USERNAME/tempkey
  460. temp_key_share_fragments=$temp_key_share_dir/.gnupg_fragments_${USERNAME}
  461. mkdir -p $temp_key_share_fragments
  462. cp $share_filename $temp_key_share_fragments/
  463. # copy the fragments directory to the remote server
  464. /usr/bin/sshpass -p "$REMOTE_PASSWORD" \
  465. scp -r -P $REMOTE_SSH_PORT $temp_key_share_fragments $REMOTE_SERVER
  466. if [ ! "$?" = "0" ]; then
  467. # Send a warning email
  468. echo "Key share to $REMOTE_SERVER failed" | \
  469. mail -s "${PROJECT_NAME} social key management" $MY_EMAIL_ADDRESS
  470. else
  471. # Send a confirmation email
  472. echo "Key ${share_filename} shared to $REMOTE_SERVER" | \
  473. mail -s "${PROJECT_NAME} social key management" $MY_EMAIL_ADDRESS
  474. fi
  475. # remove the temp file/directory
  476. shred -zu $temp_key_share_fragments/*
  477. rm -rf $temp_key_share_dir
  478. fi
  479. fi
  480. fi
  481. }
  482. function valid_backup_destination {
  483. destination_dir="$1"
  484. is_valid="yes"
  485. if [[ "$destination_dir" == "hubzilla" || \
  486. "$destination_dir" == "hubzilladata" || \
  487. "$destination_dir" == "gogs" || \
  488. "$destination_dir" == "gogsrepos" || \
  489. "$destination_dir" == "gogsssh" || \
  490. "$destination_dir" == "gnusocial" || \
  491. "$destination_dir" == "gnusocialdata" || \
  492. "$destination_dir" == "mariadb" || \
  493. "$destination_dir" == "config" || \
  494. "$destination_dir" == "letsencrypt" || \
  495. "$destination_dir" == "wiki" || \
  496. "$destination_dir" == "wiki2" || \
  497. "$destination_dir" == "xmpp" || \
  498. "$destination_dir" == "ipfs" || \
  499. "$destination_dir" == "dlna" || \
  500. "$destination_dir" == "tox" || \
  501. "$destination_dir" == "ssl" || \
  502. "$destination_dir" == "blog" || \
  503. "$destination_dir" == "owncloudrepofiles" || \
  504. "$destination_dir" == "owncloudrepoconfig" || \
  505. "$destination_dir" == "owncloudrepodata" || \
  506. "$destination_dir" == "mailinglist" ]]; then
  507. is_valid="no"
  508. fi
  509. echo $is_valid
  510. }
  511. function backup_extra_directories {
  512. if [ ! -f $BACKUP_EXTRA_DIRECTORIES ]; then
  513. return
  514. fi
  515. echo $"Backing up some additional directories"
  516. while read backup_line
  517. do
  518. backup_dir=$(echo "$backup_line" | awk -F ',' '{print $1}' | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//')
  519. if [ -d "$backup_dir" ]; then
  520. destination_dir=$(echo "$backup_line" | awk -F ',' '{print $2}' | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//')
  521. if [[ $(valid_backup_destination "$destination_dir") == "yes" ]]; then
  522. backup_directory_to_friend "$backup_dir" "$destination_dir"
  523. else
  524. echo $"WARNING: The backup directory $destination_dir is already used."
  525. echo $"Choose a different destination name for backing up $backup_dir"
  526. fi
  527. else
  528. echo $"WARNING: Directory $backup_dir does not exist"
  529. fi
  530. done <$BACKUP_EXTRA_DIRECTORIES
  531. }
  532. TEST_MODE="no"
  533. if [[ "$1" == "test" ]]; then
  534. TEST_MODE="yes"
  535. fi
  536. backup_configuration
  537. if [[ $TEST_MODE == "no" ]]; then
  538. backup_users
  539. backup_letsencrypt
  540. backup_tor
  541. backup_gnusocial
  542. backup_rss_reader
  543. backup_hubzilla
  544. backup_owncloud
  545. backup_gogs
  546. backup_wiki
  547. backup_blog
  548. backup_certs
  549. backup_mailing_list
  550. backup_xmpp
  551. backup_web_server
  552. backup_admin_readme
  553. backup_ipfs
  554. backup_dlna
  555. backup_voip
  556. backup_tox
  557. backup_mariadb
  558. backup_extra_directories
  559. fi
  560. # For each remote server
  561. while read remote_server
  562. do
  563. # Get the server and its password
  564. # Format is:
  565. # username@domain <port number> /home/username <ssh password>
  566. REMOTE_SERVER=$(echo "${remote_server}" | awk -F ' ' '{print $1}')
  567. if [ $REMOTE_SERVER ]; then
  568. REMOTE_DOMAIN=$(echo "${remote_server}" | awk -F ' ' '{print $1}' | awk -F '@' '{print $2}')
  569. REMOTE_SSH_PORT=$(echo "${remote_server}" | awk -F ' ' '{print $2}')
  570. REMOTE_DIRECTORY=$(echo "${remote_server}" | awk -F ' ' '{print $3}')
  571. REMOTE_PASSWORD=$(echo "${remote_server}" | awk -F ' ' '{print $4}')
  572. NOW=$(date +"%Y-%m-%d %H:%M:%S")
  573. REMOTE_SERVER=$REMOTE_SERVER:$REMOTE_DIRECTORY
  574. echo "$NOW Starting backup to $REMOTE_SERVER" >> /var/log/remotebackups.log
  575. # Social key management
  576. for d in /home/*/ ; do
  577. USERNAME=$(echo "$d" | awk -F '/' '{print $3}')
  578. if [[ $USERNAME != "git" && $USERNAME != "mirrors" ]]; then
  579. disperse_key_shares $USERNAME $REMOTE_DOMAIN $REMOTE_SSH_PORT "$REMOTE_PASSWORD" $REMOTE_SERVER
  580. fi
  581. done
  582. if [[ $TEST_MODE == "yes" ]]; then
  583. echo "rsync -ratlzv --rsh=\"/usr/bin/sshpass -p '$REMOTE_PASSWORD' ssh -p $REMOTE_SSH_PORT -o StrictHostKeyChecking=no\" $SERVER_DIRECTORY/backup $REMOTE_SERVER"
  584. fi
  585. rsync -ratlzv --rsh="/usr/bin/sshpass -p \"$REMOTE_PASSWORD\" ssh -p $REMOTE_SSH_PORT -o StrictHostKeyChecking=no" $SERVER_DIRECTORY/backup $REMOTE_SERVER
  586. if [ ! "$?" = "0" ]; then
  587. echo "$NOW Backup to $REMOTE_SERVER failed" >> /var/log/remotebackups.log
  588. # Send a warning email
  589. echo "Backup to $REMOTE_SERVER failed" | mail -s "${PROJECT_NAME} backup to friends" $ADMIN_EMAIL_ADDRESS
  590. else
  591. echo "$NOW Backed up to $REMOTE_SERVER" >> /var/log/remotebackups.log
  592. fi
  593. fi
  594. done < /home/${ADMIN_USERNAME}/backup.list
  595. exit 0