freedombone-backup-remote 25KB

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