freedombone-backup-remote 17KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493
  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 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. export TEXTDOMAIN=${PROJECT_NAME}-backup-remote
  33. export TEXTDOMAINDIR="/usr/share/locale"
  34. # Temporary location for data to be backed up to other servers
  35. SERVER_DIRECTORY=/root/remotebackup
  36. ADMIN_USERNAME=$(cat $COMPLETION_FILE | grep "Admin user" | awk -F ':' '{print $2}')
  37. ADMIN_NAME=$(getent passwd $ADMIN_USERNAME | cut -d: -f5 | cut -d, -f1)
  38. ADMIN_EMAIL_ADDRESS=${ADMIN_USERNAME}@${HOSTNAME}
  39. if [ ! -f /etc/ssl/private/backup.key ]; then
  40. echo $"Creating backup key"
  41. ${PROJECT_NAME}-addcert -h backup --dhkey 2048
  42. fi
  43. if [ ! -f /home/${ADMIN_USERNAME}/backup.list ]; then
  44. exit 1
  45. fi
  46. # MariaDB password
  47. DATABASE_PASSWORD=''
  48. if [ -f /root/dbpass ]; then
  49. DATABASE_PASSWORD=$(cat /root/dbpass)
  50. fi
  51. # local directory where the backup will be made
  52. if [ ! -d $SERVER_DIRECTORY ]; then
  53. mkdir $SERVER_DIRECTORY
  54. fi
  55. if [ ! -d $SERVER_DIRECTORY/backup ]; then
  56. mkdir -p $SERVER_DIRECTORY/backup
  57. fi
  58. function backup_directory_to_friend {
  59. BACKUP_KEY_EXISTS=$(gpg --list-keys "$ADMIN_NAME (backup key)")
  60. if [ ! "$?" = "0" ]; then
  61. echo $"Backup key could not be found"
  62. exit 43382
  63. fi
  64. ADMIN_BACKUP_KEY_ID=$(gpg --list-keys "$ADMIN_NAME (backup key)" | grep 'pub ' | awk -F ' ' '{print $2}' | awk -F '/' '{print $2}')
  65. if [ ! -d $SERVER_DIRECTORY/backup/${2} ]; then
  66. mkdir -p $SERVER_DIRECTORY/backup/${2}
  67. fi
  68. obnam force-lock -r $SERVER_DIRECTORY/backup/${2} --encrypt-with $ADMIN_BACKUP_KEY_ID ${1}
  69. obnam backup -r $SERVER_DIRECTORY/backup/${2} --encrypt-with $ADMIN_BACKUP_KEY_ID ${1}
  70. obnam forget --keep=30d -r $SERVER_DIRECTORY/backup/${2} --encrypt-with $ADMIN_BACKUP_KEY_ID
  71. if [ ! "$?" = "0" ]; then
  72. if [[ ${1} == "/root/temp"* || ${1} == *"tempbackup" ]]; then
  73. shred -zu /root/temp${2}/*
  74. rm -rf /root/temp${2}
  75. fi
  76. # Send a warning email
  77. echo "Unable to backup ${2}" | mail -s "${PROJECT_NAME} backup to friends" $ADMIN_EMAIL_ADDRESS
  78. exit 853
  79. fi
  80. if [[ ${1} == "/root/temp"* || ${1} == *"tempbackup" ]]; then
  81. shred -zu /root/temp${2}/*
  82. rm -rf /root/temp${2}
  83. fi
  84. }
  85. function backup_database_to_friend {
  86. if [ ${#DATABASE_PASSWORD} -lt 2 ]; then
  87. echo $"No MariaDB password was given"
  88. exit 5783
  89. fi
  90. if [ ! -d $SERVER_DIRECTORY/backup/${1} ]; then
  91. mkdir -p $SERVER_DIRECTORY/backup/${1}
  92. fi
  93. if [ ! -d $SERVER_DIRECTORY/backup/${1}data ]; then
  94. mkdir -p $SERVER_DIRECTORY/backup/${1}data
  95. fi
  96. if [ ! -d /root/temp${1}data ]; then
  97. mkdir -p /root/temp${1}data
  98. fi
  99. echo "Obtaining ${1} database backup"
  100. mysqldump --password=$DATABASE_PASSWORD ${1} > /root/temp${1}data/${1}.sql
  101. if [ ! -s /root/temp${1}data/${1}.sql ]; then
  102. echo $"${1} database could not be saved"
  103. shred -zu /root/temp${1}data/*
  104. rm -rf /root/temp${1}data
  105. # Send a warning email
  106. echo $"Unable to export ${1} database" | mail -s $"${PROJECT_NAME} backup to friends" $ADMIN_EMAIL_ADDRESS
  107. exit 5738
  108. fi
  109. }
  110. function backup_configuration {
  111. echo $"Backing up ${PROJECT_NAME} configuration files"
  112. if [ ! -d /root/tempbackupconfig ]; then
  113. mkdir -p /root/tempbackupconfig
  114. fi
  115. cp -f $CONFIG_FILE /root/tempbackupconfig
  116. cp -f $COMPLETION_FILE /root/tempbackupconfig
  117. backup_directory_to_friend /root/tempbackupconfig config
  118. }
  119. function backup_users {
  120. for d in /home/*/ ; do
  121. USERNAME=$(echo "$d" | awk -F '/' '{print $3}')
  122. if [[ $USERNAME != "git" ]]; then
  123. # personal settings
  124. if [ -d /home/$USERNAME/personal ]; then
  125. echo $"Backing up personal settings for $USERNAME"
  126. backup_directory_to_friend /home/$USERNAME/personal personal/$USERNAME
  127. fi
  128. # gpg keys
  129. if [ -d /home/$USERNAME/.gnupg ]; then
  130. echo $"Backing up gpg keys for $USERNAME"
  131. backup_directory_to_friend /home/$USERNAME/.gnupg gnupg/$USERNAME
  132. fi
  133. # ssh keys
  134. if [ -d /home/$USERNAME/.ssh ]; then
  135. echo $"Backing up ssh keys for $USERNAME"
  136. backup_directory_to_friend /home/$USERNAME/.ssh ssh/$USERNAME
  137. fi
  138. # config files
  139. if [ -d /home/$USERNAME/.config ]; then
  140. echo $"Backing up config files for $USERNAME"
  141. backup_directory_to_friend /home/$USERNAME/.config config/$USERNAME
  142. fi
  143. # mutt settings
  144. if [ -f /home/$USERNAME/.muttrc ]; then
  145. echo $"Backing up Mutt settings for $USERNAME"
  146. if [ ! -d /home/$USERNAME/tempbackup ]; then
  147. mkdir -p /home/$USERNAME/tempbackup
  148. fi
  149. cp /home/$USERNAME/.muttrc /home/$USERNAME/tempbackup
  150. if [ -f /etc/Muttrc ]; then
  151. cp /etc/Muttrc /home/$USERNAME/tempbackup
  152. fi
  153. backup_directory_to_friend /home/$USERNAME/tempbackup mutt/$USERNAME
  154. fi
  155. # procmail settings
  156. if [ -f /home/$USERNAME/.procmailrc ]; then
  157. echo $"Backing up procmail settings for $USERNAME"
  158. if [ ! -d /home/$USERNAME/tempbackup ]; then
  159. mkdir -p /home/$USERNAME/tempbackup
  160. fi
  161. cp /home/$USERNAME/.procmailrc /home/$USERNAME/tempbackup
  162. backup_directory_to_friend /home/$USERNAME/tempbackup procmail/$USERNAME
  163. fi
  164. # spamassassin settings
  165. if [ -d /home/$USERNAME/.spamassassin ]; then
  166. echo $"Backing up spamassassin settings for $USERNAME"
  167. backup_directory_to_friend /home/$USERNAME/.spamassassin spamassassin/$USERNAME
  168. fi
  169. # email
  170. if [ -d /home/$USERNAME/Maildir ]; then
  171. echo $"Creating an email archive"
  172. if [ ! -d /root/backupemail/$USERNAME ]; then
  173. mkdir -p /root/backupemail/$USERNAME
  174. fi
  175. tar -czvf /root/backupemail/$USERNAME/maildir.tar.gz /home/$USERNAME/Maildir
  176. echo $"Backing up emails for $USERNAME"
  177. backup_directory_to_friend /root/backupemail/$USERNAME mail/$USERNAME
  178. fi
  179. fi
  180. done
  181. }
  182. function backup_letsencrypt {
  183. if [ -d /etc/letsencrypt ]; then
  184. echo $"Backing up Lets Encrypt settings"
  185. backup_directory_to_friend /etc/letsencrypt letsencrypt
  186. fi
  187. }
  188. function backup_gnusocial {
  189. if grep -q "GNU Social domain" $COMPLETION_FILE; then
  190. MICROBLOG_DOMAIN_NAME=$(cat $COMPLETION_FILE | grep "GNU Social domain" | awk -F ':' '{print $2}')
  191. if [ -d /var/www/${MICROBLOG_DOMAIN_NAME} ]; then
  192. backup_database_to_friend gnusocial
  193. backup_directory_to_friend /root/tempgnusocialdata gnusocialdata
  194. echo $"Backing up GNU social installation"
  195. backup_directory_to_friend /var/www/${MICROBLOG_DOMAIN_NAME}/htdocs gnusocial
  196. else
  197. echo $"GNU Social domain specified but not found in /var/www/${MICROBLOG_DOMAIN_NAME}"
  198. exit 6327
  199. fi
  200. fi
  201. }
  202. function backup_hubzilla {
  203. if grep -q "Hubzilla domain" $COMPLETION_FILE; then
  204. HUBZILLA_DOMAIN_NAME=$(cat $COMPLETION_FILE | grep "Hubzilla domain" | awk -F ':' '{print $2}')
  205. if [ -d /var/www/${HUBZILLA_DOMAIN_NAME} ]; then
  206. backup_database_to_friend hubzilla
  207. backup_directory_to_friend /root/temphubzilladata hubzilladata
  208. echo "Backing up Hubzilla installation"
  209. backup_directory_to_friend /var/www/${HUBZILLA_DOMAIN_NAME}/htdocs hubzilla
  210. else
  211. echo $"Hubzilla domain specified but not found in /var/www/${HUBZILLA_DOMAIN_NAME}"
  212. exit 2578
  213. fi
  214. fi
  215. }
  216. function backup_owncloud {
  217. if [ -d /etc/owncloud ]; then
  218. backup_database_to_friend owncloud
  219. backup_directory_to_friend /root/tempownclouddata ownclouddata
  220. echo $"Backing up Owncloud data"
  221. backup_directory_to_friend /var/lib/owncloud owncloud
  222. backup_directory_to_friend /etc/owncloud owncloud2
  223. fi
  224. }
  225. function backup_gogs {
  226. if [ -d /home/git/go/src/github.com/gogits ]; then
  227. backup_database_to_friend gogs
  228. backup_directory_to_friend /root/tempgogsdata gogsdata
  229. echo $"Obtaining Gogs settings backup"
  230. backup_directory_to_friend /home/git/go/src/github.com/gogits/gogs/custom gogs
  231. echo $"Obtaining Gogs repos backup"
  232. mv /home/git/gogs-repositories/*.git /home/git/gogs-repositories/bob
  233. backup_directory_to_friend /home/git/gogs-repositories gogsrepos
  234. echo $"Obtaining Gogs authorized_keys backup"
  235. backup_directory_to_friend /home/git/.ssh gogsssh
  236. fi
  237. }
  238. function backup_wiki {
  239. if [ -d /etc/dokuwiki ]; then
  240. echo $"Backing up wiki"
  241. backup_directory_to_friend /var/lib/dokuwiki wiki
  242. backup_directory_to_friend /etc/dokuwiki wiki2
  243. fi
  244. }
  245. function backup_blog {
  246. if grep -q "Blog domain" $COMPLETION_FILE; then
  247. FULLBLOG_DOMAIN_NAME=$(cat $COMPLETION_FILE | grep "Blog domain" | awk -F ':' '{print $2}')
  248. if [ -d /var/www/${FULLBLOG_DOMAIN_NAME} ]; then
  249. echo $"Backing up blog"
  250. backup_directory_to_friend /var/www/${FULLBLOG_DOMAIN_NAME}/htdocs blog
  251. else
  252. echo $"Blog domain specified but not found in /var/www/${FULLBLOG_DOMAIN_NAME}"
  253. exit 2578
  254. fi
  255. fi
  256. }
  257. function backup_certs {
  258. if [ -d /etc/ssl ]; then
  259. echo $"Backing up certificates"
  260. backup_directory_to_friend /etc/ssl ssl
  261. fi
  262. }
  263. function backup_mailing_list {
  264. if [ -d /var/spool/mlmmj ]; then
  265. echo $"Backing up the public mailing list"
  266. backup_directory_to_friend /var/spool/mlmmj mailinglist
  267. fi
  268. }
  269. function backup_xmpp {
  270. if [ -d /var/lib/prosody ]; then
  271. echo $"Backing up the XMPP settings"
  272. backup_directory_to_friend /var/lib/prosody xmpp
  273. fi
  274. }
  275. function backup_web_server {
  276. if [ -d /etc/nginx ]; then
  277. echo $"Backing up web settings"
  278. backup_directory_to_friend /etc/nginx/sites-available web
  279. fi
  280. }
  281. function backup_admin_readme {
  282. if [ -f /home/$ADMIN_USERNAME/README ]; then
  283. echo $"Backing up README"
  284. if [ ! -d /home/$ADMIN_USERNAME/tempbackup ]; then
  285. mkdir -p /home/$ADMIN_USERNAME/tempbackup
  286. fi
  287. cp -f /home/$ADMIN_USERNAME/README /home/$ADMIN_USERNAME/tempbackup
  288. backup_directory_to_friend /home/$ADMIN_USERNAME/tempbackup readme
  289. fi
  290. }
  291. function backup_ipfs {
  292. if [ -d /home/$ADMIN_USERNAME/.ipfs ]; then
  293. echo $"Backing up IPFS"
  294. backup_directory_to_friend /home/$ADMIN_USERNAME/.ipfs ipfs
  295. fi
  296. }
  297. function backup_dlna {
  298. if [ -d /var/cache/minidlna ]; then
  299. echo $"Backing up DLNA cache"
  300. backup_directory_to_friend /var/cache/minidlna dlna
  301. fi
  302. }
  303. function backup_voip {
  304. if [ -f /etc/mumble-server.ini ]; then
  305. echo $"Backing up VoIP settings"
  306. if [ ! -d /root/tempvoipbackup ]; then
  307. mkdir -p /root/tempvoipbackup
  308. fi
  309. cp -f /etc/mumble-server.ini /root/tempvoipbackup
  310. cp -f /var/lib/mumble-server/mumble-server.sqlite /root/tempvoipbackup
  311. cp -f /etc/sipwitch.conf /root/tempvoipbackup
  312. backup_directory_to_friend /root/tempvoipbackup voip
  313. fi
  314. }
  315. function backup_tox {
  316. if [ -d /var/lib/tox-bootstrapd ]; then
  317. echo "Backing up Tox node settings"
  318. if [ -d /var/lib/tox-bootstrapd/Maildir ]; then
  319. rm -rf /var/lib/tox-bootstrapd/Maildir
  320. fi
  321. cp /etc/tox-bootstrapd.conf /var/lib/tox-bootstrapd
  322. backup_directory_to_friend /var/lib/tox-bootstrapd tox
  323. fi
  324. }
  325. function backup_mariadb {
  326. if [ ${#DATABASE_PASSWORD} -gt 1 ]; then
  327. if [ ! -d /root/tempmariadb ]; then
  328. mkdir /root/tempmariadb
  329. fi
  330. mysqldump --password=$DATABASE_PASSWORD mysql user > /root/tempmariadb/mysql.sql
  331. if [ ! -s /root/tempmariadb/mysql.sql ]; then
  332. echo $"Unable to backup MariaDB settings"
  333. rm -rf /root/tempmariadb
  334. # Send a warning email
  335. echo $"Unable to export database settings" | mail -s "${PROJECT_NAME} backup to friends" $ADMIN_EMAIL_ADDRESS
  336. exit 653
  337. fi
  338. echo "$DATABASE_PASSWORD" > /root/tempmariadb/db
  339. chmod 400 /root/tempmariadb/db
  340. backup_directory_to_friend /root/tempmariadb mariadb
  341. fi
  342. }
  343. # Returns the filename of a key share
  344. function get_key_share {
  345. no_of_shares=$1
  346. USERNAME="$2"
  347. REMOTE_DOMAIN="$3"
  348. # Get a share index based on the supplied domain name
  349. # This ensures that the same share is always given to the same domain
  350. sharenumstr=$(md5sum <<< "$REMOTE_DOMAIN")
  351. share_index=$(echo $((0x${sharenumstr%% *} % ${no_of_shares})) | tr -d -)
  352. # get the filename
  353. share_files=(/home/$USERNAME/.gnupg_fragments/keyshare.asc.*)
  354. share_filename=${share_files[share_index]}
  355. echo "$share_filename"
  356. }
  357. function disperse_key_shares {
  358. USERNAME=$1
  359. REMOTE_DOMAIN=$2
  360. REMOTE_SSH_PORT=$3
  361. REMOTE_PASSWORD=$4
  362. REMOTE_SERVER=$5
  363. if [ -d /home/$USERNAME/.gnupg_fragments ]; then
  364. if [ $REMOTE_DOMAIN ]; then
  365. cd /home/$USERNAME/.gnupg_fragments
  366. no_of_shares=$(ls -afq keyshare.asc.* | wc -l)
  367. if (( no_of_shares > 1 )); then
  368. share_filename=$(get_key_share $no_of_shares "$USERNAME" "$REMOTE_DOMAIN")
  369. # create a temp directory containing the share
  370. temp_key_share_dir=/home/$USERNAME/tempkey
  371. temp_key_share_fragments=$temp_key_share_dir/.gnupg_fragments_${USERNAME}
  372. mkdir -p $temp_key_share_fragments
  373. cp $share_filename $temp_key_share_fragments/
  374. # copy the fragments directory to the remote server
  375. /usr/bin/sshpass -p "$REMOTE_PASSWORD" scp -r -P $REMOTE_SSH_PORT $temp_key_share_fragments $REMOTE_SERVER
  376. if [ ! "$?" = "0" ]; then
  377. # Send a warning email
  378. echo "Key share to $REMOTE_SERVER failed" | mail -s "${PROJECT_NAME} social key management" $MY_EMAIL_ADDRESS
  379. fi
  380. # remove the temp file/directory
  381. shred -zu $temp_key_share_fragments/*
  382. rm -rf $temp_key_share_dir
  383. # Send a confirmation email
  384. echo "Key shared to $REMOTE_SERVER" | mail -s "${PROJECT_NAME} social key management" $MY_EMAIL_ADDRESS
  385. fi
  386. fi
  387. fi
  388. }
  389. backup_configuration
  390. backup_users
  391. backup_letsencrypt
  392. backup_gnusocial
  393. backup_hubzilla
  394. backup_owncloud
  395. backup_gogs
  396. backup_wiki
  397. backup_blog
  398. backup_certs
  399. backup_mailing_list
  400. backup_xmpp
  401. backup_web_server
  402. backup_admin_readme
  403. backup_ipfs
  404. backup_dlna
  405. backup_voip
  406. backup_tox
  407. backup_mariadb
  408. # For each remote server
  409. while read remote_server
  410. do
  411. # Get the server and its password
  412. # Format is:
  413. # username@domain:/home/username <port number> <ssh password>
  414. REMOTE_SERVER=$(echo "${remote_server}" | awk -F ' ' '{print $1}')
  415. if [ $REMOTE_SERVER ]; then
  416. REMOTE_DOMAIN=$(echo "${remote_server}" | awk -F ':' '{print $1}' | awk -F '@' '{print $2}')
  417. REMOTE_SSH_PORT=$(echo "${remote_server}" | awk -F ' ' '{print $2}')
  418. REMOTE_PASSWORD=$(echo "${remote_server}" | awk -F ' ' '{print $3}')
  419. NOW=$(date +"%Y-%m-%d %H:%M:%S")
  420. echo "$NOW Starting backup to $REMOTE_SERVER" >> /var/log/remotebackups.log
  421. # Social key management
  422. for d in /home/*/ ; do
  423. USERNAME=$(echo "$d" | awk -F '/' '{print $3}')
  424. if [[ $USERNAME != "git" ]]; then
  425. disperse_key_shares $USERNAME $REMOTE_DOMAIN $REMOTE_SSH_PORT "$REMOTE_PASSWORD" $REMOTE_SERVER
  426. fi
  427. done
  428. rsync -ratlzv --rsh="/usr/bin/sshpass -p \"$REMOTE_PASSWORD\" ssh -p $REMOTE_SSH_PORT -o StrictHostKeyChecking=no" $SERVER_DIRECTORY/backup $REMOTE_SERVER
  429. if [ ! "$?" = "0" ]; then
  430. echo "$NOW Backup to $REMOTE_SERVER failed" >> /var/log/remotebackups.log
  431. # Send a warning email
  432. echo "Backup to $REMOTE_SERVER failed" | mail -s "${PROJECT_NAME} backup to friends" $ADMIN_EMAIL_ADDRESS
  433. else
  434. echo "$NOW Backed up to $REMOTE_SERVER" >> /var/log/remotebackups.log
  435. fi
  436. fi
  437. done < /home/${ADMIN_USERNAME}/backup.list
  438. exit 0