freedombone-backup-remote 22KB

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