GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — VortexPanel/panel/sitebackup.py at main · BrowserlessAPI/VortexPanel Expand file tree/ Copy pathsitebackup.pyMore file actions 596 lines (522 loc) · 23.3 KB / Copy pathsitebackup.pyFile metadata and controls596 lines (522 loc) · 23.3 KB 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 """Per-website backups: the site's files and its databases in one archive, optional schedules, retention, and upload to the configured cloud storage. Archive layout (site_<domain>_<YYYYmmdd_HHMMSS>.tar.gz in the backups folder): manifest.json what is inside and where it came from databases/<name>.sql.gz one dump per linked database files/... the site's document root Used by the Backups page (panel/routes/backups.py) and by the scheduled task runner (panel/tasks.py), which runs due schedules every 5 minutes. """ import os, re, json, time, glob, shutil, tarfile, tempfile, subprocess, fcntl, calendar BACKUP_DIR = '/opt/vortexpanel/backups' SCHED_FILE = '/opt/vortexpanel/data/backup_schedules.json' LOCK_DIR = '/opt/vortexpanel/data/locks' LOG_FILE = '/var/log/vortexpanel/site-backups.log' DOMAIN_RE = re.compile(r'^(\*\.)?[a-z0-9]([a-z0-9-]*[a-z0-9])?(\.[a-z0-9]([a-z0-9-]*[a-z0-9])?)+$') DB_RE = re.compile(r'^[A-Za-z0-9_$][A-Za-z0-9_$-]{0,63}$') NAME_RE = re.compile(r'^site_(.+)_(\d{8}_\d{6})\.tar\.gz$') FREQS = ('hourly', 'daily', 'weekly', 'monthly') class BackupError(Exception): pass # --- helpers ------------------------------------------------------------------- def _slug(domain): return domain.replace('*', 'wildcard') def _unslug(s): return s.replace('wildcard', '*', 1) if s.startswith('wildcard.') else s def archive_domain(name): m = NAME_RE.match(name or '') return _unslug(m.group(1)) if m else None def _now(): return int(time.time()) def _log_line(msg): try: os.makedirs(os.path.dirname(LOG_FILE), exist_ok=True) with open(LOG_FILE, 'a') as f: f.write(time.strftime('%Y-%m-%d %H:%M:%S ') + msg + '\n') except OSError: pass class _Lock: """Non-blocking per-site lock shared by the panel workers and the timer.""" def __init__(self, key): os.makedirs(LOCK_DIR, exist_ok=True) self.path = os.path.join(LOCK_DIR, 'site-backup-' + re.sub(r'[^A-Za-z0-9._-]', '_', key) + '.lock') self.f = None def __enter__(self): self.f = open(self.path, 'w') try: fcntl.flock(self.f, fcntl.LOCK_EX | fcntl.LOCK_NB) except OSError: self.f.close() raise BackupError('A backup or restore of this site is already running') return self def __exit__(self, *a): try: fcntl.flock(self.f, fcntl.LOCK_UN) finally: self.f.close() def sites(): """{domain: path} for every website on the server.""" from panel.routes.backups import get_websites return {s['domain'].lower(): os.path.realpath(s['path']) for s in get_websites()} def db_map(): from panel.routes.backups import _db_map try: return _db_map() except Exception: return {} # --- schedules --------------------------------------------------------------------- def load_schedules(): try: with open(SCHED_FILE) as f: d = json.load(f) return d if isinstance(d, dict) else {} except Exception: return {} def save_schedules(d): os.makedirs(os.path.dirname(SCHED_FILE), exist_ok=True) tmp = SCHED_FILE + '.tmp' fd = os.open(tmp, os.O_WRONLY | os.O_CREAT | os.O_TRUNC, 0o600) with os.fdopen(fd, 'w') as f: json.dump(d, f, indent=2, sort_keys=True) os.replace(tmp, SCHED_FILE) def _update_site_state(domain, **kw): d = load_schedules() e = d.setdefault(domain, {}) e.update(kw) save_schedules(d) return e DEFAULT_SCHEDULE = {'enabled': False, 'frequency': 'daily', 'time': '03:00', 'weekday': 0, 'monthday': 1, 'every_hours': 6, 'keep_local': 7, 'cloud': False, 'target': 's3', 'keep_cloud': 30, 'databases': None, 'excludes': []} def get_schedule(domain): e = dict(DEFAULT_SCHEDULE) e.update(load_schedules().get(domain, {})) return e def validate_schedule(d, dbs_on_server=None): """Clean a schedule posted by the UI. Returns (schedule, error).""" s = {} s['enabled'] = bool(d.get('enabled')) freq = d.get('frequency', 'daily') if freq not in FREQS: return None, 'Unknown frequency' s['frequency'] = freq t = str(d.get('time') or '03:00') if not re.fullmatch(r'([01]\d|2[0-3]):[0-5]\d', t): return None, 'Time must be HH:MM (24-hour)' s['time'] = t try: s['weekday'] = int(d.get('weekday', 0)) % 7 s['monthday'] = min(28, max(1, int(d.get('monthday', 1)))) s['every_hours'] = int(d.get('every_hours', 6)) s['keep_local'] = max(0, min(365, int(d.get('keep_local', 7)))) s['keep_cloud'] = max(0, min(3650, int(d.get('keep_cloud', 30)))) except (TypeError, ValueError): return None, 'Numbers expected for day, hours and retention' if s['every_hours'] not in (1, 2, 3, 4, 6, 8, 12): return None, 'Hourly backups can run every 1, 2, 3, 4, 6, 8 or 12 hours' s['cloud'] = bool(d.get('cloud')) s['target'] = str(d.get('target') or 's3') if s['cloud']: from panel import storage if not storage.get_target(s['target']): return None, 'Choose a backup destination (Backups > Storage) for the remote copy' if s['enabled'] and s['keep_local'] == 0 and not s['cloud']: return None, 'Keep at least one local copy, or upload to cloud storage' dbs = d.get('databases') if dbs is None: s['databases'] = None # detect automatically at backup time else: if not isinstance(dbs, list) or any(not DB_RE.match(str(x)) for x in dbs): return None, 'Invalid database name' s['databases'] = sorted(set(str(x) for x in dbs)) ex = d.get('excludes') or [] if isinstance(ex, str): ex = [x.strip() for x in ex.splitlines()] clean = [] for x in ex: x = str(x).strip() if not x: continue if x.startswith('/') or '..' in x.split('/') or len(x) > 200 or '\n' in x: return None, f'Exclude patterns are paths inside the site, like wp-content/cache ({x})' clean.append(x) s['excludes'] = clean[:50] return s, None def next_run(s, after=None): """Next time (epoch) a schedule is due, strictly after `after`.""" after = int(after if after is not None else time.time()) hh, mm = (int(x) for x in s.get('time', '03:00').split(':')) freq = s.get('frequency', 'daily') lt = time.localtime(after) if freq == 'hourly': step = int(s.get('every_hours', 6)) * 3600 # slots aligned to local midnight + minute offset base = time.mktime((lt.tm_year, lt.tm_mon, lt.tm_mday, 0, mm, 0, 0, 0, -1)) t = base while t <= after: t += step return int(t) for add in range(0, 400): day = time.localtime(time.mktime((lt.tm_year, lt.tm_mon, lt.tm_mday + add, 12, 0, 0, 0, 0, -1))) if freq == 'weekly' and day.tm_wday != int(s.get('weekday', 0)): continue if freq == 'monthly' and day.tm_mday != int(s.get('monthday', 1)): continue t = time.mktime((day.tm_year, day.tm_mon, day.tm_mday, hh, mm, 0, 0, 0, -1)) if t > after: return int(t) return after + 86400 # --- database detection -------------------------------------------------------- _WP_DB = re.compile(r"""define\(\s*['"]DB_NAME['"]\s*,\s*['"]([^'"]+)['"]""") _ENV_DB = re.compile(r'^\s*DB_(?:DATABASE|NAME)\s*=\s*["\']?([A-Za-z0-9_$-]+)', re.M) def detect_databases(domain, path, existing=None): """Databases a site uses: WordPress wp-config.php, a Laravel-style .env, and the database the panel creates with a new site (domain with _).""" existing = existing if existing is not None else db_map() found = [] for rel, rx in (('wp-config.php', _WP_DB), ('.env', _ENV_DB)): fp = os.path.join(path, rel) try: if os.path.isfile(fp) and os.path.getsize(fp) < 2 * 1024 * 1024: with open(fp, errors='replace') as f: for m in rx.finditer(f.read()): found.append(m.group(1)) except OSError: pass guess = re.sub(r'[^a-zA-Z0-9_]', '_', domain.replace('.', '_'))[:32] found.append(guess) out = [] for n in found: if n in existing and n not in out: out.append(n) return out def site_databases(domain, path, sched=None, existing=None): sched = sched or get_schedule(domain) existing = existing if existing is not None else db_map() if sched.get('databases') is not None: return [n for n in sched['databases'] if n in existing], existing return detect_databases(domain, path, existing), existing # --- create ---------------------------------------------------------------------- def create_backup(domain, log=None, trigger='manual'): """Back up one site. Returns {'name', 'path', 'size', 'databases'}.""" log = log or (lambda m: None) domain = (domain or '').lower() if not DOMAIN_RE.match(domain): raise BackupError('Invalid domain') all_sites = sites() path = all_sites.get(domain) if not path or not os.path.isdir(path): raise BackupError(f'Website {domain} was not found on this server') if path in ('/', '/www', '/www/wwwroot', '/var/www', '/root', '/home') or path.count('/') < 2: raise BackupError(f'Refusing to back up {path}') sched = get_schedule(domain) from panel.routes.backups import _dump_db with _Lock(domain): os.makedirs(BACKUP_DIR, mode=0o700, exist_ok=True) while True: ts = time.strftime('%Y%m%d_%H%M%S') name = f'site_{_slug(domain)}_{ts}.tar.gz' dest = os.path.join(BACKUP_DIR, name) if not os.path.exists(dest): break time.sleep(1) stage = tempfile.mkdtemp(prefix='.site-', dir=BACKUP_DIR) try: dbs, existing = site_databases(domain, path, sched) os.makedirs(os.path.join(stage, 'databases')) dumped = [] for n in dbs: engine = existing.get(n, 'mysql') log(f'Dumping database {n} ({"PostgreSQL" if engine == "postgresql" else "MySQL/MariaDB"})...') err = _dump_db(n, engine, os.path.join(stage, 'databases', n + '.sql.gz')) if err: raise BackupError(f'Database {n} could not be dumped: {err[:400]}') dumped.append({'name': n, 'engine': engine}) if not dbs: log('No database is linked to this site - backing up files only.') manifest = { 'format': 'vortexpanel-site-backup', 'version': 1, 'domain': domain, 'path': path, 'created': _now(), 'trigger': trigger, 'databases': dumped, 'excludes': sched.get('excludes') or [], 'panel_version': _panel_version(), } with open(os.path.join(stage, 'manifest.json'), 'w') as f: json.dump(manifest, f, indent=2) log(f'Archiving {path}...') cmd = ['tar', '-czf', dest, '-C', stage, 'manifest.json', 'databases', '--transform', r's,^\.\(/\|$\),files\1,'] for ex in manifest['excludes']: cmd.append('--exclude=./' + ex.strip('/')) # never archive the backups folder into itself if a site lives above it cmd += ['--exclude=./' + os.path.relpath(BACKUP_DIR, path)] if BACKUP_DIR.startswith(path + '/') else [] cmd += ['-C', path, '.'] try: r = subprocess.run(cmd, capture_output=True, text=True, timeout=6 * 3600) rc, err = r.returncode, r.stderr.strip() except subprocess.TimeoutExpired: rc, err = 2, 'timed out after 6 hours' # tar exit 1 = some files changed while being read (live site): archive is complete if rc >= 2: try: os.unlink(dest) except OSError: pass raise BackupError(f'Archiving failed: {err[-400:] or rc}') os.chmod(dest, 0o600) finally: shutil.rmtree(stage, ignore_errors=True) size = os.path.getsize(dest) log(f'Backup complete: {name} ({_fmt(size)})') return {'name': name, 'path': dest, 'size': size, 'databases': [d['name'] for d in dumped]} def _panel_version(): for p in ('/opt/vortexpanel/VERSION', os.path.join(os.path.dirname(__file__), '..', 'VERSION')): try: with open(p) as f: return f.read().strip() except OSError: continue return '' def _fmt(n): for u in ('B', 'KB', 'MB', 'GB', 'TB'): if n < 1024 or u == 'TB': return f'{n:.0f} {u}' if u == 'B' else f'{n:.1f} {u}' n /= 1024.0 # --- list / prune --------------------------------------------------------------- def list_backups(domain=None): out = [] for p in glob.glob(os.path.join(BACKUP_DIR, 'site_*.tar.gz')): name = os.path.basename(p) dom = archive_domain(name) if not dom or (domain and dom != domain): continue try: st = os.stat(p) except OSError: continue out.append({'name': name, 'domain': dom, 'size': st.st_size, 'mtime': int(st.st_mtime)}) out.sort(key=lambda e: e['name'], reverse=True) return out def prune_local(domain, keep, log=None): log = log or (lambda m: None) if keep is None or keep < 0: return 0 removed = 0 for e in list_backups(domain)[keep:]: try: os.unlink(os.path.join(BACKUP_DIR, e['name'])) removed += 1 log(f'Removed old local backup {e["name"]}') except OSError: pass return removed def read_manifest(name): path = os.path.join(BACKUP_DIR, os.path.basename(name)) try: with tarfile.open(path, 'r:gz') as tf: m = tf.next() if m is None or m.name != 'manifest.json' or not m.isfile() or m.size > 1024 * 1024: return None return json.loads(tf.extractfile(m).read().decode('utf-8')) except Exception: return None # --- cloud ----------------------------------------------------------------------- def cloud_config(): try: from panel.routes.cloud_backup import load_config cfg = load_config() return cfg if cfg.get('bucket') else None except Exception: return None def _target_of(domain): """The backup destination of a site schedule ('s3' = Cloud Storage settings).""" return get_schedule(domain).get('target') or 's3' def cloud_upload(path, name, log=None, target=None): from panel import storage target = target or 's3' if target == 's3' and not cloud_config(): raise BackupError('Cloud storage is not connected (Backups > Storage)') try: storage.upload(target, path, name, log=log) except storage.StorageError as e: raise BackupError(str(e)) def cloud_prune(domain, keep, log=None, target=None): from panel import storage target = target or 's3' if keep is None or keep <= 0 or not storage.get_target(target): return 0 removed = 0 files = [f for f in storage.list_files(target, f'site_{_slug(domain)}_') if NAME_RE.match(f['name']) and archive_domain(f['name']) == domain] files.sort(key=lambda x: x['name'], reverse=True) for f in files[keep:]: storage.delete(target, f['name']) removed += 1 (log or (lambda m: None))(f'Removed old cloud backup {f["name"]}') return removed def restore_backup(name, log=None, files=True, databases=True, target=None): """Restore a site backup: files go back to the site folder (or `target`), databases are imported into the databases they came from. The old files are kept until the new ones are in place, and put back if extracting fails.""" log = log or (lambda m: None) name = os.path.basename(name or '') path = os.path.join(BACKUP_DIR, name) if not NAME_RE.match(name) or not os.path.isfile(path): raise BackupError('Site backup not found') man = read_manifest(name) if not man or man.get('format') != 'vortexpanel-site-backup': raise BackupError('This archive has no site backup manifest') domain = man.get('domain', '') if not DOMAIN_RE.match(domain): raise BackupError('The manifest names an invalid domain') from panel.routes.backups import _check_target, _restore_db, _member_ok dest = target or man.get('path') or '' real, err = _check_target(dest) if err: raise BackupError(err) if real.count('/') < 2: raise BackupError(f'Refusing to restore into {real}') # Check every member before extracting anything. try: with tarfile.open(path, 'r:gz') as tf: members = tf.getmembers() except (tarfile.TarError, OSError, EOFError) as e: raise BackupError(f'Archive is not readable: {e}') for m in members: if not _member_ok(m.name) or not (m.name in ('manifest.json', 'databases', 'files') or m.name.startswith(('databases/', 'files/'))): raise BackupError(f'Archive contains an unexpected path: {m.name}') if m.islnk() and not _member_ok(m.linkname): raise BackupError(f'Archive contains an unsafe hard link: {m.name}') with _Lock(domain): if files: parent = os.path.dirname(real.rstrip('/')) or '/' os.makedirs(parent, exist_ok=True) ts = time.strftime('%Y%m%d%H%M%S') tmp = os.path.join(parent, f'.{os.path.basename(real)}.vp-restore-{ts}') old = os.path.join(parent, f'.{os.path.basename(real)}.vp-old-{ts}') os.makedirs(tmp) log(f'Extracting files to {real}...') r = subprocess.run(['tar', '-xzf', path, '-C', tmp, '--strip-components=1', '--no-same-owner', 'files'], capture_output=True, text=True, timeout=6 * 3600) if r.returncode != 0: shutil.rmtree(tmp, ignore_errors=True) raise BackupError(f'Extracting files failed: {r.stderr.strip()[-400:]}') had_old = os.path.isdir(real) if had_old: os.rename(real, old) try: os.rename(tmp, real) except OSError as e: if had_old: os.rename(old, real) shutil.rmtree(tmp, ignore_errors=True) raise BackupError(f'Could not put the restored files in place: {e}') if had_old: shutil.rmtree(old, ignore_errors=True) try: from panel.routes.websites_core import ensure_web_ownership ensure_web_ownership(real) except Exception: pass log('Files restored') if databases and man.get('databases'): work = tempfile.mkdtemp(prefix='.restore-', dir=BACKUP_DIR) try: r = subprocess.run(['tar', '-xzf', path, '-C', work, 'databases'], capture_output=True, text=True, timeout=6 * 3600) if r.returncode != 0: raise BackupError(f'Extracting the database dumps failed: {r.stderr.strip()[-400:]}') class _J: def line(self, m): log(m) for d in man['databases']: n = d.get('name', '') if not DB_RE.match(n): raise BackupError(f'Invalid database name in manifest: {n}') dump = os.path.join(work, 'databases', n + '.sql.gz') if not os.path.isfile(dump): raise BackupError(f'The archive has no dump for {n}') err = _restore_db(_J(), dump, n) if err: raise BackupError(f'Database {n}: {err}') log('Databases restored: ' + ', '.join(d['name'] for d in man['databases'])) finally: shutil.rmtree(work, ignore_errors=True) return {'domain': domain, 'path': real} # --- scheduled runs -------------------------------------------------------------- def run_one(domain, trigger='schedule', log=None): """Back up a site and apply its upload and retention settings. Records the result on the site's schedule entry. Returns (ok, message).""" lines = [] def _log(m): lines.append(m) _log_line(f'[{domain}] {m}') if log: log(m) sched = get_schedule(domain) started = _now() try: res = create_backup(domain, log=_log, trigger=trigger) cloud_msg = '' if sched.get('cloud'): try: tgt = sched.get('target') or 's3' cloud_upload(res['path'], res['name'], log=_log, target=tgt) cloud_prune(domain, sched.get('keep_cloud', 30), log=_log, target=tgt) cloud_msg = ', uploaded to remote storage' except Exception as e: cloud_msg = f', but the cloud upload failed: {e}' _log(f'Cloud upload failed: {e}') if sched.get('cloud') and not cloud_msg.startswith(', but') and sched.get('keep_local', 7) == 0: os.unlink(res['path']) _log('Local copy removed (keep 0 local copies)') else: prune_local(domain, max(1, sched.get('keep_local', 7)) if sched.get('enabled') or trigger == 'schedule' else None, log=_log) ok = not cloud_msg.startswith(', but') msg = f'{res["name"]} ({_fmt(res["size"])}){cloud_msg}' _update_site_state(domain, last_run=started, last_ok=ok, last_message=msg, last_file=res['name'], last_trigger=trigger) return ok, msg except Exception as e: _log(f'Backup failed: {e}') _update_site_state(domain, last_run=started, last_ok=False, last_message=str(e)[:500], last_trigger=trigger) return False, str(e) def run_due(now=None): """Run every enabled schedule that is due. Called by the task timer.""" now = int(now or time.time()) data = load_schedules() existing = sites() ran = [] for domain, raw in sorted(data.items()): s = dict(DEFAULT_SCHEDULE) s.update(raw) if not s.get('enabled'): continue if domain not in existing: continue nr = int(s.get('next_run') or 0) if not nr: _update_site_state(domain, next_run=next_run(s, now)) continue if nr > now: continue ok, msg = run_one(domain, trigger='schedule') _update_site_state(domain, next_run=next_run(get_schedule(domain), int(time.time()))) ran.append((domain, ok, msg)) return ran def recent_failures(hours=48): """Scheduled backups whose last run failed in the last `hours` (for alerts).""" out = [] cutoff = time.time() - hours * 3600 for domain, e in load_schedules().items(): if e.get('last_ok') is False and e.get('last_trigger') == 'schedule': t = e.get('last_run') try: ts = time.mktime(time.strptime(str(t)[:19], '%Y-%m-%d %H:%M:%S')) if isinstance(t, str) else float(t or 0) except Exception: ts = 0 if ts >= cutoff: out.append({'id': f'{domain}:{t}', 'domain': domain, 'time': t, 'error': e.get('last_message', '')}) return out |
| You are visitor 01402792 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |