freedombone-backup-remote 16KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459
  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@freedombone.net>
  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. CONFIGURATION_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. PROJECT_INSTALL_DIR=/usr/local/bin
  37. if [ -f /usr/bin/${PROJECT_NAME} ]; then
  38. PROJECT_INSTALL_DIR=/usr/bin
  39. fi
  40. source $PROJECT_INSTALL_DIR/${PROJECT_NAME}-vars
  41. # utilities needed for backup commands
  42. UTILS_FILES=/usr/share/${PROJECT_NAME}/utils/${PROJECT_NAME}-utils-*
  43. for f in $UTILS_FILES
  44. do
  45. source $f
  46. done
  47. # Temporary location for data to be backed up to other servers
  48. SERVER_DIRECTORY=/root/remotebackup
  49. # get the version of Go being used
  50. GO_VERSION=$(cat /usr/share/${PROJECT_NAME}/utils/${PROJECT_NAME}-utils-go | grep 'GO_VERSION=' | head -n 1 | awk -F '=' '{print $2}')
  51. ADMIN_USERNAME=$(get_completion_param "Admin user")
  52. ADMIN_NAME=$(getent passwd $ADMIN_USERNAME | cut -d: -f5 | cut -d, -f1)
  53. ADMIN_EMAIL_ADDRESS=${ADMIN_USERNAME}@${HOSTNAME}
  54. if [ ! -f /etc/ssl/private/backup.key ]; then
  55. echo $"Creating backup key"
  56. ${PROJECT_NAME}-addcert -h backup --dhkey 2048
  57. fi
  58. if [ ! -f /home/${ADMIN_USERNAME}/backup.list ]; then
  59. exit 1
  60. fi
  61. # MariaDB password
  62. DATABASE_PASSWORD=$(${PROJECT_NAME}-pass -u root -a mariadb)
  63. # local directory where the backup will be made
  64. if [ ! -d $SERVER_DIRECTORY ]; then
  65. mkdir $SERVER_DIRECTORY
  66. fi
  67. if [ ! -d $SERVER_DIRECTORY/backup ]; then
  68. mkdir -p $SERVER_DIRECTORY/backup
  69. fi
  70. # The name of a currently suspended site
  71. # Sites are suspended so that verification should work
  72. SUSPENDED_SITE=
  73. function suspend_site {
  74. # suspends a given website
  75. if [[ $ENABLE_VERIFICATION != "yes" ]]; then
  76. return
  77. fi
  78. SUSPENDED_SITE="$1"
  79. nginx_dissite $SUSPENDED_SITE
  80. systemctl reload nginx
  81. }
  82. function restart_site {
  83. # restarts a given website
  84. if [ ! $SUSPENDED_SITE ]; then
  85. return
  86. fi
  87. nginx_ensite $SUSPENDED_SITE
  88. systemctl reload nginx
  89. SUSPENDED_SITE=
  90. }
  91. function backup_blocklist {
  92. if [ ! -f /root/${PROJECT_NAME}-firewall-domains.cfg ]; then
  93. return
  94. fi
  95. echo $"Backing up ${PROJECT_NAME} blocklist"
  96. temp_backup_dir=/root/tempbackupblocklist
  97. if [ ! -d $temp_backup_dir ]; then
  98. mkdir -p $temp_backup_dir
  99. fi
  100. if [ -f $NODEJS_INSTALLED_APPS_FILE ]; then
  101. cp -f /root/${PROJECT_NAME}-firewall-domains.cfg $temp_backup_dir
  102. fi
  103. backup_directory_to_friend $temp_backup_dir blocklist
  104. rm -rf $temp_backup_dir
  105. }
  106. function backup_configfiles {
  107. echo $"Backing up ${PROJECT_NAME} configuration files"
  108. temp_backup_dir=/root/tempbackupconfig
  109. if [ ! -d $temp_backup_dir ]; then
  110. mkdir -p $temp_backup_dir
  111. fi
  112. if [ -f $NODEJS_INSTALLED_APPS_FILE ]; then
  113. cp -f $NODEJS_INSTALLED_APPS_FILE $temp_backup_dir
  114. fi
  115. if [ -f /root/.nostore ]; then
  116. cp -f /root/.nostore $temp_backup_dir
  117. else
  118. if [ -f $temp_backup_dir/.nostore ]; then
  119. rm $temp_backup_dir/.nostore
  120. fi
  121. fi
  122. cp -f $CONFIGURATION_FILE $temp_backup_dir
  123. cp -f $COMPLETION_FILE $temp_backup_dir
  124. if [ -f $BACKUP_EXTRA_DIRECTORIES ]; then
  125. cp -f $BACKUP_EXTRA_DIRECTORIES $temp_backup_dir
  126. fi
  127. # nginx password hashes
  128. if [ -f /etc/nginx/.htpasswd ]; then
  129. cp -f /etc/nginx/.htpasswd $temp_backup_dir/htpasswd
  130. fi
  131. backup_directory_to_friend $temp_backup_dir configfiles
  132. }
  133. function backup_users {
  134. for d in /home/*/ ; do
  135. USERNAME=$(echo "$d" | awk -F '/' '{print $3}')
  136. if [[ $(is_valid_user "$USERNAME") == "1" ]]; then
  137. # personal settings
  138. if [ -d /home/$USERNAME/personal ]; then
  139. echo $"Backing up personal settings for $USERNAME"
  140. backup_directory_to_friend /home/$USERNAME/personal personal/$USERNAME
  141. fi
  142. # gpg keys
  143. if [ -d /home/$USERNAME/.gnupg ]; then
  144. echo $"Backing up gpg keys for $USERNAME"
  145. backup_directory_to_friend /home/$USERNAME/.gnupg gnupg/$USERNAME
  146. fi
  147. # ssh keys
  148. if [ -d /home/$USERNAME/.ssh ]; then
  149. echo $"Backing up ssh keys for $USERNAME"
  150. backup_directory_to_friend /home/$USERNAME/.ssh ssh/$USERNAME
  151. fi
  152. # syncthing files
  153. if [ -d /home/$USERNAME/Sync ]; then
  154. echo $"Backing up syncthing files for $USERNAME"
  155. backup_directory_to_friend /home/$USERNAME/Sync syncthing/$USERNAME
  156. # ensure that device IDs will be backed up as part of user config settings
  157. if [ ! -d /home/$USERNAME/.config/syncthing ]; then
  158. mkdir -p /home/$USERNAME/.config/syncthing
  159. chown -R $USERNAME:$USERNAME /home/$USERNAME/.config
  160. fi
  161. if [ -f /home/$USERNAME/.syncthing-server-id ]; then
  162. cp /home/$USERNAME/.syncthing-server-id /home/$USERNAME/.config/syncthing
  163. chown -R $USERNAME:$USERNAME /home/$USERNAME/.config
  164. fi
  165. if [ -f /home/$USERNAME/.syncthingids ]; then
  166. cp /home/$USERNAME/.syncthingids /home/$USERNAME/.config/syncthing
  167. chown -R $USERNAME:$USERNAME /home/$USERNAME/.config
  168. fi
  169. fi
  170. # config files
  171. if [ -d /home/$USERNAME/.config ]; then
  172. echo $"Backing up config files for $USERNAME"
  173. backup_directory_to_friend /home/$USERNAME/.config config/$USERNAME
  174. fi
  175. # monkeysphere files
  176. if [ -d /home/$USERNAME/.monkeysphere ]; then
  177. echo $"Backing up monkeysphere files for $USERNAME"
  178. backup_directory_to_friend /home/$USERNAME/.monkeysphere monkeysphere/$USERNAME
  179. fi
  180. # fin files
  181. if [ -d /home/$USERNAME/.fin ]; then
  182. echo $"Backing up fin files for $USERNAME"
  183. backup_directory_to_friend /home/$USERNAME/.fin fin/$USERNAME
  184. fi
  185. # local files
  186. if [ -d /home/$USERNAME/.local ]; then
  187. echo $"Backing up local files for $USERNAME"
  188. backup_directory_to_friend /home/$USERNAME/.local local/$USERNAME
  189. fi
  190. # mutt settings
  191. if [ -f /home/$USERNAME/.muttrc ]; then
  192. echo $"Backing up Mutt settings for $USERNAME"
  193. if [ ! -d /home/$USERNAME/tempbackup ]; then
  194. mkdir -p /home/$USERNAME/tempbackup
  195. fi
  196. cp /home/$USERNAME/.muttrc /home/$USERNAME/tempbackup
  197. if [ -f /etc/Muttrc ]; then
  198. cp /etc/Muttrc /home/$USERNAME/tempbackup
  199. fi
  200. backup_directory_to_friend /home/$USERNAME/tempbackup mutt/$USERNAME
  201. fi
  202. # procmail settings
  203. if [ -f /home/$USERNAME/.procmailrc ]; then
  204. echo $"Backing up procmail settings for $USERNAME"
  205. if [ ! -d /home/$USERNAME/tempbackup ]; then
  206. mkdir -p /home/$USERNAME/tempbackup
  207. fi
  208. cp /home/$USERNAME/.procmailrc /home/$USERNAME/tempbackup
  209. backup_directory_to_friend /home/$USERNAME/tempbackup procmail/$USERNAME
  210. fi
  211. # spamassassin settings
  212. if [ -d /home/$USERNAME/.spamassassin ]; then
  213. echo $"Backing up spamassassin settings for $USERNAME"
  214. backup_directory_to_friend /home/$USERNAME/.spamassassin spamassassin/$USERNAME
  215. fi
  216. # email
  217. if [ -d /home/$USERNAME/Maildir ]; then
  218. echo $"Stopping mail server"
  219. systemctl stop exim4
  220. echo $"Creating an email archive"
  221. if [ ! -d /root/backupemail/$USERNAME ]; then
  222. mkdir -p /root/backupemail/$USERNAME
  223. fi
  224. tar -czvf /root/backupemail/$USERNAME/maildir.tar.gz /home/$USERNAME/Maildir
  225. echo $"Restarting mail server"
  226. systemctl start exim4
  227. echo $"Backing up emails for $USERNAME"
  228. backup_directory_to_friend /root/backupemail/$USERNAME mail/$USERNAME
  229. fi
  230. fi
  231. done
  232. }
  233. function backup_letsencrypt {
  234. if [ -d /etc/letsencrypt ]; then
  235. echo $"Backing up Lets Encrypt settings"
  236. backup_directory_to_friend /etc/letsencrypt letsencrypt
  237. fi
  238. }
  239. function backup_passwordstore {
  240. if [ -d /root/.passwords ]; then
  241. echo $"Backing up password store"
  242. backup_directory_to_friend /root/.passwords passwordstore
  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_certs {
  252. if [ -d /etc/ssl ]; then
  253. echo $"Backing up certificates"
  254. backup_directory_to_friend /etc/ssl ssl
  255. fi
  256. }
  257. function backup_mailing_list {
  258. if [ -d /var/spool/mlmmj ]; then
  259. echo $"Backing up the public mailing list"
  260. backup_directory_to_friend /var/spool/mlmmj mailinglist
  261. fi
  262. }
  263. function backup_web_server {
  264. if [ -d /etc/nginx ]; then
  265. echo $"Backing up web settings"
  266. backup_directory_to_friend /etc/nginx/sites-available web
  267. fi
  268. }
  269. function backup_admin_readme {
  270. if [ -f /home/$ADMIN_USERNAME/README ]; then
  271. echo $"Backing up README"
  272. if [ ! -d /home/$ADMIN_USERNAME/tempbackup ]; then
  273. mkdir -p /home/$ADMIN_USERNAME/tempbackup
  274. fi
  275. cp -f /home/$ADMIN_USERNAME/README /home/$ADMIN_USERNAME/tempbackup
  276. backup_directory_to_friend /home/$ADMIN_USERNAME/tempbackup readme
  277. fi
  278. }
  279. function backup_mariadb {
  280. if [ ${#DATABASE_PASSWORD} -gt 1 ]; then
  281. temp_backup_dir=/root/tempmariadb
  282. if [ ! -d $temp_backup_dir ]; then
  283. mkdir $temp_backup_dir
  284. fi
  285. keep_database_running
  286. mysqldump --password=$DATABASE_PASSWORD mysql user > $temp_backup_dir/mysql.sql
  287. if [ ! -s $temp_backup_dir/mysql.sql ]; then
  288. echo $"Unable to backup MariaDB settings"
  289. rm -rf $temp_backup_dir
  290. # Send a warning email
  291. echo $"Unable to export database settings" | mail -s "${PROJECT_NAME} backup to friends" $ADMIN_EMAIL_ADDRESS
  292. exit 653
  293. fi
  294. echo "$DATABASE_PASSWORD" > $temp_backup_dir/db
  295. chmod 400 $temp_backup_dir/db
  296. backup_directory_to_friend $temp_backup_dir mariadb
  297. fi
  298. }
  299. # Returns the filename of a key share
  300. function get_key_share {
  301. no_of_shares=$1
  302. USERNAME="$2"
  303. REMOTE_DOMAIN="$3"
  304. # Get a share index based on the supplied domain name
  305. # This ensures that the same share is always given to the same domain
  306. sharenumstr=$(md5sum <<< "$REMOTE_DOMAIN")
  307. share_index=$(echo $((0x${sharenumstr%% *} % ${no_of_shares})) | tr -d -)
  308. # get the filename
  309. share_files=(/home/$USERNAME/.gnupg_fragments/keyshare.asc.*)
  310. share_filename=${share_files[share_index]}
  311. echo "$share_filename"
  312. }
  313. function disperse_key_shares {
  314. USERNAME=$1
  315. REMOTE_DOMAIN=$2
  316. REMOTE_SSH_PORT=$3
  317. REMOTE_PASSWORD=$4
  318. REMOTE_SERVER=$5
  319. if [ -d /home/$USERNAME/.gnupg_fragments ]; then
  320. if [ $REMOTE_DOMAIN ]; then
  321. cd /home/$USERNAME/.gnupg_fragments
  322. no_of_shares=$(ls -afq keyshare.asc.* | wc -l)
  323. if (( no_of_shares > 1 )); then
  324. share_filename=$(get_key_share $no_of_shares "$USERNAME" "$REMOTE_DOMAIN")
  325. # create a temp directory containing the share
  326. temp_key_share_dir=/home/$USERNAME/tempkey
  327. temp_key_share_fragments=$temp_key_share_dir/.gnupg_fragments_${USERNAME}
  328. mkdir -p $temp_key_share_fragments
  329. cp $share_filename $temp_key_share_fragments/
  330. # copy the fragments directory to the remote server
  331. /usr/bin/sshpass -p "$REMOTE_PASSWORD" \
  332. scp -r -P $REMOTE_SSH_PORT $temp_key_share_fragments $REMOTE_SERVER
  333. if [ ! "$?" = "0" ]; then
  334. # Send a warning email
  335. echo "Key share to $REMOTE_SERVER failed" | \
  336. mail -s "${PROJECT_NAME} social key management" $MY_EMAIL_ADDRESS
  337. else
  338. # Send a confirmation email
  339. echo "Key ${share_filename} shared to $REMOTE_SERVER" | \
  340. mail -s "${PROJECT_NAME} social key management" $MY_EMAIL_ADDRESS
  341. fi
  342. # remove the temp file/directory
  343. shred -zu $temp_key_share_fragments/*
  344. rm -rf $temp_key_share_dir
  345. fi
  346. fi
  347. fi
  348. }
  349. TEST_MODE="no"
  350. if [[ "$1" == "test" ]]; then
  351. TEST_MODE="yes"
  352. fi
  353. backup_configfiles
  354. if [[ $TEST_MODE == "no" ]]; then
  355. backup_blocklist
  356. backup_users
  357. backup_letsencrypt
  358. backup_passwordstore
  359. backup_tor
  360. backup_web_server
  361. backup_admin_readme
  362. backup_mariadb
  363. backup_certs
  364. backup_mailing_list
  365. backup_apps remote
  366. backup_extra_directories remote
  367. fi
  368. # For each remote server
  369. while read remote_server
  370. do
  371. # Get the server and its password
  372. # Format is:
  373. # username@domain <port number> /home/username <ssh password>
  374. REMOTE_SERVER=$(echo "${remote_server}" | awk -F ' ' '{print $1}')
  375. if [ $REMOTE_SERVER ]; then
  376. REMOTE_DOMAIN=$(echo "${remote_server}" | awk -F ' ' '{print $1}' | awk -F '@' '{print $2}')
  377. REMOTE_SSH_PORT=$(echo "${remote_server}" | awk -F ' ' '{print $2}')
  378. REMOTE_DIRECTORY=$(echo "${remote_server}" | awk -F ' ' '{print $3}')
  379. REMOTE_PASSWORD=$(echo "${remote_server}" | awk -F ' ' '{print $4}')
  380. NOW=$(date +"%Y-%m-%d %H:%M:%S")
  381. REMOTE_SERVER=$REMOTE_SERVER:$REMOTE_DIRECTORY
  382. echo "$NOW Starting backup to $REMOTE_SERVER" >> /var/log/remotebackups.log
  383. # Social key management
  384. for d in /home/*/ ; do
  385. USERNAME=$(echo "$d" | awk -F '/' '{print $3}')
  386. if [[ $(is_valid_user "$USERNAME") == "1" ]]; then
  387. disperse_key_shares $USERNAME $REMOTE_DOMAIN $REMOTE_SSH_PORT "$REMOTE_PASSWORD" $REMOTE_SERVER
  388. fi
  389. done
  390. if [[ $TEST_MODE == "yes" ]]; then
  391. echo "rsync -ratlzv --rsh=\"/usr/bin/sshpass -p '$REMOTE_PASSWORD' ssh -p $REMOTE_SSH_PORT -o StrictHostKeyChecking=no\" $SERVER_DIRECTORY/backup $REMOTE_SERVER"
  392. fi
  393. rsync -ratlzv --rsh="/usr/bin/sshpass -p \"$REMOTE_PASSWORD\" ssh -p $REMOTE_SSH_PORT -o StrictHostKeyChecking=no" $SERVER_DIRECTORY/backup $REMOTE_SERVER
  394. if [ ! "$?" = "0" ]; then
  395. echo "$NOW Backup to $REMOTE_SERVER failed" >> /var/log/remotebackups.log
  396. # Send a warning email
  397. echo "Backup to $REMOTE_SERVER failed" | mail -s "${PROJECT_NAME} backup to friends" $ADMIN_EMAIL_ADDRESS
  398. else
  399. echo "$NOW Backed up to $REMOTE_SERVER" >> /var/log/remotebackups.log
  400. fi
  401. fi
  402. done < /home/${ADMIN_USERNAME}/backup.list
  403. exit 0