Last active 3 weeks ago

Revision 68e36d53c845bf5b0d5604b4932e1a98aa6d4afc

backup.sh Raw
1#!/usr/bin/env bash
2#
3# full backup of fflol (or another ao3 fork) to another LAN machine
4# via SSH using rsync and tarballs.
5#
6# Backs up: 1) a mysqldump of the database
7# 2) a tarball of the Elasticsearch data directory
8# 3) the entire app directory (code, config, uploaded files)
9# Then pushes it all to a remote host with rsync, throttled to prevent
10# using too much of the production machine of CPU, disk I/O or bandwidth.
11#
12# rsync only transfers deltas. Cron-friendly.
13#
14# REQUIREMENTS:
15# - passwordless SSH from THIS machine to the remote (ssh-copy-id once)
16# - rsync on both ends, docker running, ionice + nice (util-linux)
17#
18# ---------------------------------------------------------------------------
19
20set -euo pipefail
21
22############################ CONFIG (edit me) ##############################
23
24# Where the AO3 directory exists on the server (the dir bind-mounted to /otwa)
25APP_DIR="/home/YOU/ao3"
26
27# The MariaDB/MySQL docker container name (`docker ps` to check)
28DB_CONTAINER="db"
29
30# Database credentials, match the docker-compose.yml / config/database.yml
31DB_NAME="otwarchive_production"
32DB_USER="root"
33DB_PASS="change_me"
34
35# Elasticsearch data directory on the host (the volume mounted into the
36# es container). Set ES_DATA_DIR="" to skip the ES step.
37ES_DATA_DIR="${APP_DIR}/es"
38
39# Remote target. user@host must already accept this machine's SSH key.
40REMOTE="you@backup-host"
41REMOTE_BASE="/home/you/backups" # parent dir on the remote
42
43# Bandwidth cap for rsync, in KB/s, ~10 MB/s
44# Raise it (e.g. 51200 = 50 MB/s) if yr machines are fast. Zoom zoom.
45BW_LIMIT=10240
46
47# How many days of *dump artifacts* to keep locally.
48RETENTION_DAYS=30
49
50############################################################################
51
52TS="$(date +%Y%m%d_%H%M%S)"
53TODAY="$(date +%Y%m%d)"
54BACKUP_DIR="${APP_DIR}/backups"
55REMOTE_DIR="${REMOTE_BASE}/${TODAY}-full"
56
57mkdir -p "${BACKUP_DIR}"
58
59echo "=== BACKUP TIME! ${TS} ==="
60
61############################################################################
62# Step 1: database dump
63#
64# Dump through the docker container rather than copying the raw data dir.
65# Never rsync live InnoDB files (db/, mysql-data/) as they're root-owned
66# and being written mid-copy.
67############################################################################
68
69DB_DUMP="${BACKUP_DIR}/db_${TS}.sql.gz"
70echo "[1/3] mysqldump -> ${DB_DUMP}"
71# nice -n 19: gzip runs at lowest CPU priority.
72docker exec "${DB_CONTAINER}" sh -c \
73 "mysqldump -u ${DB_USER} -p'${DB_PASS}' --no-tablespaces ${DB_NAME}" \
74 | nice -n 19 gzip > "${DB_DUMP}"
75echo " done: $(du -h "${DB_DUMP}" | cut -f1)"
76
77############################################################################
78# Step 2: Elasticsearch data (tarball)
79############################################################################
80
81if [ -n "${ES_DATA_DIR}" ] && [ -d "${ES_DATA_DIR}" ]; then
82 ES_DUMP="${BACKUP_DIR}/es_${TS}.tar.gz"
83 echo "[2/3] ES tarball -> ${ES_DUMP}"
84 # ionice -c3: idle disk class, low-priority use of I/O
85 nice -n 19 ionice -c3 tar -czf "${ES_DUMP}" -C "${ES_DATA_DIR}" .
86 echo " done: $(du -h "${ES_DUMP}" | cut -f1)"
87else
88 echo "[2/3] skipping ES (ES_DATA_DIR unset or missing)"
89fi
90
91############################################################################
92# Step 3: rsync app dir + artifacts to remote
93#
94# Excludes:
95# backups/ would recursively copy history
96# db/, mysql-data/ locked, root-owned, useless
97# log/, tmp/ junk
98# .git/ just git clone later
99# node_modules/ reproducible
100#
101# TRAILING SLASH ON THE SOURCE IS ESSENTIAL:
102# src/ -> copies CONTENTS of src into dest
103# src -> creates dest/src (annoying double-nesting)
104#
105# -a archive mode (recursive and preserves perms/times/symlinks)
106# -z compress in flight (code + sql are text so it compresses well)
107# --bwlimit caps throughput so the link stays usable
108############################################################################
109
110echo "[3/3] rsync ${APP_DIR} -> ${REMOTE}:${REMOTE_DIR}"
111ssh "${REMOTE}" "mkdir -p '${REMOTE_DIR}/fanfiction-lol' '${REMOTE_DIR}/artifacts'"
112
113nice -n 19 ionice -c3 rsync -az --bwlimit="${BW_LIMIT}" \
114 --exclude='backups/' \
115 --exclude='log/' \
116 --exclude='tmp/' \
117 --exclude='.git/' \
118 --exclude='node_modules/' \
119 --exclude='db/' \
120 --exclude='mysql-data/' \
121 "${APP_DIR}/" "${REMOTE}:${REMOTE_DIR}/fanfiction-lol/"
122
123# Push just today's dump artifacts
124nice -n 19 ionice -c3 rsync -az --bwlimit="${BW_LIMIT}" \
125 "${DB_DUMP}" \
126 $([ -n "${ES_DATA_DIR}" ] && echo "${ES_DUMP:-}") \
127 "${REMOTE}:${REMOTE_DIR}/artifacts/"
128
129############################################################################
130# Manifest + retention
131############################################################################
132
133ssh "${REMOTE}" "cat > '${REMOTE_DIR}/backup_info.txt'" <<EOF
134BACKUP .NFO
135===========
136Date: ${TS} ($(date))
137Source host: $(hostname)
138App dir: ${APP_DIR}
139Contents: full app dir (code, config, storage, es/) + artifacts/
140Restore: provision containers, import artifacts/db_${TS}.sql.gz into
141 mariadb, restore config/local.yml + storage/, rebuild index.
142EOF
143
144# Drop artifacts older than RETENTION_DAYS on SERVER machine
145find "${BACKUP_DIR}" -name 'db_*.sql.gz' -mtime "+${RETENTION_DAYS}" -delete
146find "${BACKUP_DIR}" -name 'es_*.tar.gz' -mtime "+${RETENTION_DAYS}" -delete
147
148echo "=== done -> ${REMOTE}:${REMOTE_DIR} ==="