freedombone-backup-remote 17KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491
  1. #!/bin/bash
  2. # _____ _ _
  3. # | __|___ ___ ___ _| |___ _____| |_ ___ ___ ___
  4. # | __| _| -_| -_| . | . | | . | . | | -_|
  5. # |__| |_| |___|___|___|___|_|_|_|___|___|_|_|___|
  6. #
  7. # Freedom in the Cloud
  8. #
  9. # Backup to remote servers - the web of backups
  10. # License
  11. # =======
  12. #
  13. # Copyright (C) 2015-2018 Bob Mottram <bob@freedombone.net>
  14. #
  15. # This program is free software: you can redistribute it and/or modify
  16. # it under the terms of the GNU Affero General Public License as published by
  17. # the Free Software Foundation, either version 3 of the License, or
  18. # (at your option) any later version.
  19. #
  20. # This program is distributed in the hope that it will be useful,
  21. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  22. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  23. # GNU Affero General Public License for more details.
  24. #
  25. # You should have received a copy of the GNU Affero General Public License
  26. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  27. PROJECT_NAME='freedombone'
  28. COMPLETION_FILE=$HOME/${PROJECT_NAME}-completed.txt
  29. CONFIGURATION_FILE=$HOME/${PROJECT_NAME}.cfg
  30. MONGODB_APPS_FILE=$HOME/.mongodbapps
  31. BACKUP_EXTRA_DIRECTORIES=/root/backup-extra-dirs.csv
  32. ENABLE_VERIFICATION="no"
  33. export TEXTDOMAIN=${PROJECT_NAME}-backup-remote
  34. export TEXTDOMAINDIR="/usr/share/locale"
  35. PROJECT_INSTALL_DIR=/usr/local/bin
  36. if [ -f /usr/bin/${PROJECT_NAME} ]; then
  37. PROJECT_INSTALL_DIR=/usr/bin
  38. fi
  39. source "$PROJECT_INSTALL_DIR/${PROJECT_NAME}-vars"
  40. # utilities needed for backup commands
  41. UTILS_FILES="/usr/share/${PROJECT_NAME}/utils/${PROJECT_NAME}-utils-*"
  42. for f in $UTILS_FILES
  43. do
  44. source "$f"
  45. done
  46. # Temporary location for data to be backed up to other servers
  47. SERVER_DIRECTORY=/root/remotebackup
  48. # get the version of Go being used
  49. GO_VERSION=$(grep 'GO_VERSION=' "/usr/share/${PROJECT_NAME}/utils/${PROJECT_NAME}-utils-go" | head -n 1 | awk -F '=' '{print $2}')
  50. ADMIN_USERNAME=$(get_completion_param "Admin user")
  51. ADMIN_NAME=$(getent passwd "$ADMIN_USERNAME" | cut -d: -f5 | cut -d, -f1)
  52. ADMIN_EMAIL_ADDRESS=${ADMIN_USERNAME}@${HOSTNAME}
  53. if [ ! -f /etc/ssl/private/backup.key ]; then
  54. echo $"Creating backup key"
  55. ${PROJECT_NAME}-addcert -h backup --dhkey 2048
  56. fi
  57. if [ ! -f "/home/${ADMIN_USERNAME}/backup.list" ]; then
  58. exit 1
  59. fi
  60. # MariaDB password
  61. DATABASE_PASSWORD=$(${PROJECT_NAME}-pass -u root -a mariadb)
  62. # local directory where the backup will be made
  63. if [ ! -d $SERVER_DIRECTORY ]; then
  64. mkdir $SERVER_DIRECTORY
  65. fi
  66. if [ ! -d $SERVER_DIRECTORY/backup ]; then
  67. mkdir -p $SERVER_DIRECTORY/backup
  68. fi
  69. # The name of a currently suspended site
  70. # Sites are suspended so that verification should work
  71. SUSPENDED_SITE=
  72. function suspend_site {
  73. # suspends a given website
  74. if [[ $ENABLE_VERIFICATION != "yes" ]]; then
  75. return
  76. fi
  77. SUSPENDED_SITE="$1"
  78. nginx_dissite "$SUSPENDED_SITE"
  79. systemctl reload nginx
  80. }
  81. function restart_site {
  82. # restarts a given website
  83. if [ ! "$SUSPENDED_SITE" ]; then
  84. return
  85. fi
  86. nginx_ensite "$SUSPENDED_SITE"
  87. systemctl reload nginx
  88. SUSPENDED_SITE=
  89. }
  90. function backup_blocklist {
  91. if [ ! -f /root/${PROJECT_NAME}-firewall-domains.cfg ]; then
  92. return
  93. fi
  94. echo $"Backing up ${PROJECT_NAME} blocklist"
  95. temp_backup_dir=/root/tempbackupblocklist
  96. if [ ! -d $temp_backup_dir ]; then
  97. mkdir -p $temp_backup_dir
  98. fi
  99. if [ -f "$NODEJS_INSTALLED_APPS_FILE" ]; then
  100. cp -f /root/${PROJECT_NAME}-firewall-domains.cfg $temp_backup_dir
  101. fi
  102. backup_directory_to_friend $temp_backup_dir blocklist
  103. rm -rf $temp_backup_dir
  104. }
  105. function backup_configfiles {
  106. echo $"Backing up ${PROJECT_NAME} configuration files"
  107. temp_backup_dir=/root/tempbackupconfig
  108. if [ ! -d $temp_backup_dir ]; then
  109. mkdir -p $temp_backup_dir
  110. fi
  111. if [ -f "$NODEJS_INSTALLED_APPS_FILE" ]; then
  112. cp -f "$NODEJS_INSTALLED_APPS_FILE" $temp_backup_dir
  113. fi
  114. if [ -f /root/.nostore ]; then
  115. cp -f /root/.nostore $temp_backup_dir
  116. else
  117. if [ -f $temp_backup_dir/.nostore ]; then
  118. rm $temp_backup_dir/.nostore
  119. fi
  120. fi
  121. cp -f "$CONFIGURATION_FILE" $temp_backup_dir
  122. cp -f "$COMPLETION_FILE" $temp_backup_dir
  123. if [ -f $BACKUP_EXTRA_DIRECTORIES ]; then
  124. cp -f $BACKUP_EXTRA_DIRECTORIES $temp_backup_dir
  125. fi
  126. if [ -f "$MONGODB_APPS_FILE" ]; then
  127. cp -f "$MONGODB_APPS_FILE" $temp_backup_dir
  128. fi
  129. # nginx password hashes
  130. if [ -f /etc/nginx/.htpasswd ]; then
  131. cp -f /etc/nginx/.htpasswd $temp_backup_dir/htpasswd
  132. fi
  133. backup_directory_to_friend $temp_backup_dir configfiles
  134. }
  135. function backup_users {
  136. for d in /home/*/ ; do
  137. USERNAME=$(echo "$d" | awk -F '/' '{print $3}')
  138. if [[ $(is_valid_user "$USERNAME") == "1" ]]; then
  139. # personal settings
  140. if [ -d "/home/$USERNAME/personal" ]; then
  141. echo $"Backing up personal settings for $USERNAME"
  142. backup_directory_to_friend "/home/$USERNAME/personal" "personal/$USERNAME"
  143. fi
  144. # gpg keys
  145. if [ -d "/home/$USERNAME/.gnupg" ]; then
  146. echo $"Backing up gpg keys for $USERNAME"
  147. backup_directory_to_friend "/home/$USERNAME/.gnupg" "gnupg/$USERNAME"
  148. fi
  149. # ssh keys
  150. if [ -d "/home/$USERNAME/.ssh" ]; then
  151. echo $"Backing up ssh keys for $USERNAME"
  152. backup_directory_to_friend "/home/$USERNAME/.ssh" "ssh/$USERNAME"
  153. fi
  154. # syncthing files
  155. if [ -d "/home/$USERNAME/Sync" ]; then
  156. echo $"Backing up syncthing files for $USERNAME"
  157. backup_directory_to_friend "/home/$USERNAME/Sync" "syncthing/$USERNAME"
  158. # ensure that device IDs will be backed up as part of user config settings
  159. if [ ! -d "/home/$USERNAME/.config/syncthing" ]; then
  160. mkdir -p "/home/$USERNAME/.config/syncthing"
  161. chown -R "$USERNAME":"$USERNAME" "/home/$USERNAME/.config"
  162. fi
  163. if [ -f "/home/$USERNAME/.syncthing-server-id" ]; then
  164. cp "/home/$USERNAME/.syncthing-server-id" "/home/$USERNAME/.config/syncthing"
  165. chown -R "$USERNAME":"$USERNAME" "/home/$USERNAME/.config"
  166. fi
  167. if [ -f "/home/$USERNAME/.syncthingids" ]; then
  168. cp "/home/$USERNAME/.syncthingids" "/home/$USERNAME/.config/syncthing"
  169. chown -R "$USERNAME":"$USERNAME" "/home/$USERNAME/.config"
  170. fi
  171. fi
  172. # config files
  173. if [ -d "/home/$USERNAME/.config" ]; then
  174. echo $"Backing up config files for $USERNAME"
  175. backup_directory_to_friend "/home/$USERNAME/.config" "config/$USERNAME"
  176. fi
  177. # monkeysphere files
  178. if [ -d "/home/$USERNAME/.monkeysphere" ]; then
  179. echo $"Backing up monkeysphere files for $USERNAME"
  180. backup_directory_to_friend "/home/$USERNAME/.monkeysphere" "monkeysphere/$USERNAME"
  181. fi
  182. # fin files
  183. if [ -d "/home/$USERNAME/.fin" ]; then
  184. echo $"Backing up fin files for $USERNAME"
  185. backup_directory_to_friend "/home/$USERNAME/.fin" "fin/$USERNAME"
  186. fi
  187. # local files
  188. if [ -d "/home/$USERNAME/.local" ]; then
  189. echo $"Backing up local files for $USERNAME"
  190. backup_directory_to_friend "/home/$USERNAME/.local" "local/$USERNAME"
  191. fi
  192. # mutt settings
  193. if [ -f "/home/$USERNAME/.muttrc" ]; then
  194. echo $"Backing up Mutt settings for $USERNAME"
  195. if [ ! -d "/home/$USERNAME/tempbackup" ]; then
  196. mkdir -p "/home/$USERNAME/tempbackup"
  197. fi
  198. cp "/home/$USERNAME/.muttrc" "/home/$USERNAME/tempbackup"
  199. if [ -f /etc/Muttrc ]; then
  200. cp /etc/Muttrc "/home/$USERNAME/tempbackup"
  201. fi
  202. backup_directory_to_friend "/home/$USERNAME/tempbackup" "mutt/$USERNAME"
  203. fi
  204. if [ -d "/home/$USERNAME/.mutt" ]; then
  205. echo $"Backing up Mutt configurations for $USERNAME"
  206. backup_directory_to_friend "/home/$USERNAME/.mutt" "mutt/${USERNAME}configs"
  207. fi
  208. # procmail settings
  209. if [ -f "/home/$USERNAME/.procmailrc" ]; then
  210. echo $"Backing up procmail settings for $USERNAME"
  211. if [ ! -d "/home/$USERNAME/tempbackup" ]; then
  212. mkdir -p "/home/$USERNAME/tempbackup"
  213. fi
  214. cp "/home/$USERNAME/.procmailrc" "/home/$USERNAME/tempbackup"
  215. backup_directory_to_friend "/home/$USERNAME/tempbackup" "procmail/$USERNAME"
  216. fi
  217. # spamassassin settings
  218. if [ -d "/home/$USERNAME/.spamassassin" ]; then
  219. echo $"Backing up spamassassin settings for $USERNAME"
  220. backup_directory_to_friend "/home/$USERNAME/.spamassassin" "spamassassin/$USERNAME"
  221. fi
  222. # email
  223. if [ -d "/home/$USERNAME/Maildir" ]; then
  224. echo $"Stopping mail server"
  225. systemctl stop exim4
  226. echo $"Creating an email archive"
  227. if [ ! -d "/root/backupemail/$USERNAME" ]; then
  228. mkdir -p "/root/backupemail/$USERNAME"
  229. fi
  230. tar -czvf "/root/backupemail/$USERNAME/maildir.tar.gz" "/home/$USERNAME/Maildir"
  231. echo $"Restarting mail server"
  232. systemctl start exim4
  233. echo $"Backing up emails for $USERNAME"
  234. backup_directory_to_friend "/root/backupemail/$USERNAME" "mail/$USERNAME"
  235. fi
  236. gpg_agent_enable "$USERNAME"
  237. fi
  238. done
  239. }
  240. function backup_letsencrypt {
  241. if [ -d /etc/letsencrypt ]; then
  242. echo $"Backing up Lets Encrypt settings"
  243. backup_directory_to_friend /etc/letsencrypt letsencrypt
  244. fi
  245. }
  246. function backup_passwordstore {
  247. if [ -d /root/.passwords ]; then
  248. echo $"Backing up password store"
  249. backup_directory_to_friend /root/.passwords passwordstore
  250. fi
  251. }
  252. function backup_tor {
  253. if [ -d /etc/letsencrypt ]; then
  254. echo $"Backing up Tor settings"
  255. backup_directory_to_friend /var/lib/tor tor
  256. fi
  257. }
  258. function backup_certs {
  259. if [ -d /etc/ssl ]; then
  260. echo $"Backing up certificates"
  261. backup_directory_to_friend /etc/ssl ssl
  262. fi
  263. }
  264. function backup_mailing_list {
  265. if [ -d /var/spool/mlmmj ]; then
  266. echo $"Backing up the public mailing list"
  267. backup_directory_to_friend /var/spool/mlmmj mailinglist
  268. fi
  269. }
  270. function backup_web_server {
  271. if [ -d /etc/nginx ]; then
  272. echo $"Backing up web settings"
  273. backup_directory_to_friend /etc/nginx/sites-available web
  274. fi
  275. }
  276. function backup_admin_readme {
  277. if [ -f "/home/$ADMIN_USERNAME/README" ]; then
  278. echo $"Backing up README"
  279. if [ ! -d "/home/$ADMIN_USERNAME/tempbackup" ]; then
  280. mkdir -p "/home/$ADMIN_USERNAME/tempbackup"
  281. fi
  282. cp -f "/home/$ADMIN_USERNAME/README" "/home/$ADMIN_USERNAME/tempbackup"
  283. backup_directory_to_friend "/home/$ADMIN_USERNAME/tempbackup" readme
  284. fi
  285. }
  286. function backup_mariadb {
  287. if [ ${#DATABASE_PASSWORD} -gt 1 ]; then
  288. temp_backup_dir=/root/tempmariadb
  289. if [ ! -d $temp_backup_dir ]; then
  290. mkdir $temp_backup_dir
  291. fi
  292. keep_database_running
  293. mysqldump --password="$DATABASE_PASSWORD" mysql user > "$temp_backup_dir/mysql.sql"
  294. if [ ! -s $temp_backup_dir/mysql.sql ]; then
  295. echo $"Unable to backup MariaDB settings"
  296. rm -rf $temp_backup_dir
  297. # Send a warning email
  298. echo $"Unable to export database settings" | mail -s "${PROJECT_NAME} backup to friends" "$ADMIN_EMAIL_ADDRESS"
  299. exit 653
  300. fi
  301. echo "$DATABASE_PASSWORD" > $temp_backup_dir/db
  302. chmod 400 $temp_backup_dir/db
  303. backup_directory_to_friend $temp_backup_dir mariadb
  304. fi
  305. }
  306. function backup_postgresql {
  307. if [ ! -d /etc/postgresql ]; then
  308. return
  309. fi
  310. temp_backup_dir=/root/temppostgresql
  311. if [ ! -d $temp_backup_dir ]; then
  312. mkdir $temp_backup_dir
  313. fi
  314. # shellcheck disable=SC2024
  315. sudo -u postgres pg_dumpall --roles-only > "$temp_backup_dir/postgresql.sql"
  316. if [ ! -s $temp_backup_dir/postgresql.sql ]; then
  317. echo $"Unable to backup postgresql settings"
  318. rm -rf $temp_backup_dir
  319. umount "$USB_MOUNT"
  320. rm -rf "$USB_MOUNT"
  321. exit 684365
  322. fi
  323. echo "$DATABASE_PASSWORD" > $temp_backup_dir/db
  324. chmod 400 $temp_backup_dir/db
  325. backup_directory_to_friend $temp_backup_dir postgresql
  326. }
  327. # Returns the filename of a key share
  328. function get_key_share {
  329. no_of_shares=$1
  330. USERNAME="$2"
  331. REMOTE_DOMAIN="$3"
  332. # Get a share index based on the supplied domain name
  333. # This ensures that the same share is always given to the same domain
  334. sharenumstr=$(md5sum <<< "$REMOTE_DOMAIN")
  335. share_index=$(echo $((0x${sharenumstr%% *} % no_of_shares)) | tr -d -)
  336. # get the filename
  337. share_files=("/home/$USERNAME/.gnupg_fragments/keyshare.asc.*")
  338. share_filename=${share_files[share_index]}
  339. echo "$share_filename"
  340. }
  341. function disperse_key_shares {
  342. USERNAME=$1
  343. REMOTE_DOMAIN=$2
  344. REMOTE_SSH_PORT=$3
  345. REMOTE_PASSWORD=$4
  346. REMOTE_SERVER=$5
  347. if [ -d "/home/$USERNAME/.gnupg_fragments" ]; then
  348. if [ "$REMOTE_DOMAIN" ]; then
  349. cd "/home/$USERNAME/.gnupg_fragments" || exit 34687246462
  350. # shellcheck disable=SC2012
  351. no_of_shares=$(ls -afq keyshare.asc.* | wc -l)
  352. if (( no_of_shares > 1 )); then
  353. share_filename=$(get_key_share "$no_of_shares" "$USERNAME" "$REMOTE_DOMAIN")
  354. # create a temp directory containing the share
  355. temp_key_share_dir=/home/$USERNAME/tempkey
  356. temp_key_share_fragments=$temp_key_share_dir/.gnupg_fragments_${USERNAME}
  357. mkdir -p "$temp_key_share_fragments"
  358. cp "$share_filename" "$temp_key_share_fragments/"
  359. # copy the fragments directory to the remote server
  360. /usr/bin/sshpass -p "$REMOTE_PASSWORD" \
  361. scp -r -P "$REMOTE_SSH_PORT" "$temp_key_share_fragments" "$REMOTE_SERVER"
  362. # shellcheck disable=SC2181
  363. if [ ! "$?" = "0" ]; then
  364. # Send a warning email
  365. echo "Key share to $REMOTE_SERVER failed" | \
  366. mail -s "${PROJECT_NAME} social key management" "$MY_EMAIL_ADDRESS"
  367. else
  368. # Send a confirmation email
  369. echo "Key ${share_filename} shared to $REMOTE_SERVER" | \
  370. mail -s "${PROJECT_NAME} social key management" "$MY_EMAIL_ADDRESS"
  371. fi
  372. # remove the temp file/directory
  373. shred -zu "$temp_key_share_fragments/"*
  374. rm -rf "$temp_key_share_dir"
  375. fi
  376. fi
  377. fi
  378. }
  379. TEST_MODE="no"
  380. if [[ "$1" == "test" ]]; then
  381. TEST_MODE="yes"
  382. fi
  383. gpg_agent_setup root
  384. backup_configfiles
  385. if [[ $TEST_MODE == "no" ]]; then
  386. backup_blocklist
  387. backup_users
  388. backup_letsencrypt
  389. backup_passwordstore
  390. backup_tor
  391. backup_web_server
  392. backup_admin_readme
  393. backup_mariadb
  394. backup_postgresql
  395. backup_certs
  396. backup_mailing_list
  397. backup_apps remote
  398. backup_extra_directories remote
  399. fi
  400. # For each remote server
  401. while read -r remote_server
  402. do
  403. # Get the server and its password
  404. # Format is:
  405. # username@domain <port number> /home/username <ssh password>
  406. REMOTE_SERVER=$(echo "${remote_server}" | awk -F ' ' '{print $1}')
  407. if [ "$REMOTE_SERVER" ]; then
  408. REMOTE_DOMAIN=$(echo "${remote_server}" | awk -F ' ' '{print $1}' | awk -F '@' '{print $2}')
  409. REMOTE_SSH_PORT=$(echo "${remote_server}" | awk -F ' ' '{print $2}')
  410. REMOTE_DIRECTORY=$(echo "${remote_server}" | awk -F ' ' '{print $3}')
  411. REMOTE_PASSWORD=$(echo "${remote_server}" | awk -F ' ' '{print $4}')
  412. NOW=$(date +"%Y-%m-%d %H:%M:%S")
  413. REMOTE_SERVER=$REMOTE_SERVER:$REMOTE_DIRECTORY
  414. echo "$NOW Starting backup to $REMOTE_SERVER" >> /var/log/remotebackups.log
  415. # Social key management
  416. for d in /home/*/ ; do
  417. USERNAME=$(echo "$d" | awk -F '/' '{print $3}')
  418. if [[ $(is_valid_user "$USERNAME") == "1" ]]; then
  419. disperse_key_shares "$USERNAME" "$REMOTE_DOMAIN" "$REMOTE_SSH_PORT" "$REMOTE_PASSWORD" "$REMOTE_SERVER"
  420. fi
  421. done
  422. if [[ $TEST_MODE == "yes" ]]; then
  423. echo "rsync -ratlzv --rsh=\"/usr/bin/sshpass -p '$REMOTE_PASSWORD' ssh -p $REMOTE_SSH_PORT -o StrictHostKeyChecking=no\" $SERVER_DIRECTORY/backup $REMOTE_SERVER"
  424. fi
  425. if ! rsync -ratlzv --rsh="/usr/bin/sshpass -p \"$REMOTE_PASSWORD\" ssh -p $REMOTE_SSH_PORT -o StrictHostKeyChecking=no" "$SERVER_DIRECTORY/backup" "$REMOTE_SERVER"; then
  426. echo "$NOW Backup to $REMOTE_SERVER failed" >> /var/log/remotebackups.log
  427. # Send a warning email
  428. echo "Backup to $REMOTE_SERVER failed" | mail -s "${PROJECT_NAME} backup to friends" "$ADMIN_EMAIL_ADDRESS"
  429. else
  430. echo "$NOW Backed up to $REMOTE_SERVER" >> /var/log/remotebackups.log
  431. fi
  432. fi
  433. done < "/home/${ADMIN_USERNAME}/backup.list"
  434. exit 0