brennan revised this gist 3 weeks ago. Go to revision
1 file changed, 2 insertions, 2 deletions
backup.sh
| @@ -38,7 +38,7 @@ ES_DATA_DIR="${APP_DIR}/es" | |||
| 38 | 38 | ||
| 39 | 39 | # Remote target. user@host must already accept this machine's SSH key. | |
| 40 | 40 | REMOTE="you@backup-host" | |
| 41 | - | REMOTE_BASE="/home/you/backups" # parent dir on the remote | |
| 41 | + | REMOTE_BASE="/home/you/backups" # parent dir on the remote | |
| 42 | 42 | ||
| 43 | 43 | # Bandwidth cap for rsync, in KB/s, ~10 MB/s | |
| 44 | 44 | # Raise it (e.g. 51200 = 50 MB/s) if yr machines are fast. Zoom zoom. | |
| @@ -92,7 +92,7 @@ fi | |||
| 92 | 92 | # Step 3: rsync app dir + artifacts to remote | |
| 93 | 93 | # | |
| 94 | 94 | # Excludes: | |
| 95 | - | # backups/ ould recursively copy history | |
| 95 | + | # backups/ would recursively copy history | |
| 96 | 96 | # db/, mysql-data/ locked, root-owned, useless | |
| 97 | 97 | # log/, tmp/ junk | |
| 98 | 98 | # .git/ just git clone later | |
brennan revised this gist 3 weeks ago. Go to revision
1 file changed, 148 insertions
backup.sh(file created)
| @@ -0,0 +1,148 @@ | |||
| 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 | + | ||
| 20 | + | set -euo pipefail | |
| 21 | + | ||
| 22 | + | ############################ CONFIG (edit me) ############################## | |
| 23 | + | ||
| 24 | + | # Where the AO3 directory exists on the server (the dir bind-mounted to /otwa) | |
| 25 | + | APP_DIR="/home/YOU/ao3" | |
| 26 | + | ||
| 27 | + | # The MariaDB/MySQL docker container name (`docker ps` to check) | |
| 28 | + | DB_CONTAINER="db" | |
| 29 | + | ||
| 30 | + | # Database credentials, match the docker-compose.yml / config/database.yml | |
| 31 | + | DB_NAME="otwarchive_production" | |
| 32 | + | DB_USER="root" | |
| 33 | + | DB_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. | |
| 37 | + | ES_DATA_DIR="${APP_DIR}/es" | |
| 38 | + | ||
| 39 | + | # Remote target. user@host must already accept this machine's SSH key. | |
| 40 | + | REMOTE="you@backup-host" | |
| 41 | + | REMOTE_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. | |
| 45 | + | BW_LIMIT=10240 | |
| 46 | + | ||
| 47 | + | # How many days of *dump artifacts* to keep locally. | |
| 48 | + | RETENTION_DAYS=30 | |
| 49 | + | ||
| 50 | + | ############################################################################ | |
| 51 | + | ||
| 52 | + | TS="$(date +%Y%m%d_%H%M%S)" | |
| 53 | + | TODAY="$(date +%Y%m%d)" | |
| 54 | + | BACKUP_DIR="${APP_DIR}/backups" | |
| 55 | + | REMOTE_DIR="${REMOTE_BASE}/${TODAY}-full" | |
| 56 | + | ||
| 57 | + | mkdir -p "${BACKUP_DIR}" | |
| 58 | + | ||
| 59 | + | echo "=== 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 | + | ||
| 69 | + | DB_DUMP="${BACKUP_DIR}/db_${TS}.sql.gz" | |
| 70 | + | echo "[1/3] mysqldump -> ${DB_DUMP}" | |
| 71 | + | # nice -n 19: gzip runs at lowest CPU priority. | |
| 72 | + | docker exec "${DB_CONTAINER}" sh -c \ | |
| 73 | + | "mysqldump -u ${DB_USER} -p'${DB_PASS}' --no-tablespaces ${DB_NAME}" \ | |
| 74 | + | | nice -n 19 gzip > "${DB_DUMP}" | |
| 75 | + | echo " done: $(du -h "${DB_DUMP}" | cut -f1)" | |
| 76 | + | ||
| 77 | + | ############################################################################ | |
| 78 | + | # Step 2: Elasticsearch data (tarball) | |
| 79 | + | ############################################################################ | |
| 80 | + | ||
| 81 | + | if [ -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)" | |
| 87 | + | else | |
| 88 | + | echo "[2/3] skipping ES (ES_DATA_DIR unset or missing)" | |
| 89 | + | fi | |
| 90 | + | ||
| 91 | + | ############################################################################ | |
| 92 | + | # Step 3: rsync app dir + artifacts to remote | |
| 93 | + | # | |
| 94 | + | # Excludes: | |
| 95 | + | # backups/ ould 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 | + | ||
| 110 | + | echo "[3/3] rsync ${APP_DIR} -> ${REMOTE}:${REMOTE_DIR}" | |
| 111 | + | ssh "${REMOTE}" "mkdir -p '${REMOTE_DIR}/fanfiction-lol' '${REMOTE_DIR}/artifacts'" | |
| 112 | + | ||
| 113 | + | nice -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 | |
| 124 | + | nice -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 | + | ||
| 133 | + | ssh "${REMOTE}" "cat > '${REMOTE_DIR}/backup_info.txt'" <<EOF | |
| 134 | + | BACKUP .NFO | |
| 135 | + | =========== | |
| 136 | + | Date: ${TS} ($(date)) | |
| 137 | + | Source host: $(hostname) | |
| 138 | + | App dir: ${APP_DIR} | |
| 139 | + | Contents: full app dir (code, config, storage, es/) + artifacts/ | |
| 140 | + | Restore: provision containers, import artifacts/db_${TS}.sql.gz into | |
| 141 | + | mariadb, restore config/local.yml + storage/, rebuild index. | |
| 142 | + | EOF | |
| 143 | + | ||
| 144 | + | # Drop artifacts older than RETENTION_DAYS on SERVER machine | |
| 145 | + | find "${BACKUP_DIR}" -name 'db_*.sql.gz' -mtime "+${RETENTION_DAYS}" -delete | |
| 146 | + | find "${BACKUP_DIR}" -name 'es_*.tar.gz' -mtime "+${RETENTION_DAYS}" -delete | |
| 147 | + | ||
| 148 | + | echo "=== done -> ${REMOTE}:${REMOTE_DIR} ===" | |