mirror of
https://github.com/retspen/webvirtcloud
synced 2026-11-08 19:26:48 +00:00
The only backups of the panel were the ones taken by hand before an update. Now `manage.py backup` writes a new directory under backups/ with a consistent copy of the sqlite database (the backup API, while the panel runs, checked with PRAGMA integrity_check), the secret key, the worker's node_id, settings.py, the SSH keys of the panel's user and, outside Docker, the commit. It keeps the newest 7 (--keep N) and deletes older ones only after a good backup; a failed one leaves nothing, and a second run while one runs fails. Files are mode 600, directories 700: the database holds compute passwords and keys. With PostgreSQL the database is left out. It runs daily at 03:15: in Docker from the image's cron into a new volume, webvirtcloud-backups; otherwise from webvirt-backup.timer, which the installer and the manual steps install. A backup has the layout of the update backups, so the restore steps take either.
44 lines
1.8 KiB
Bash
Executable file
44 lines
1.8 KiB
Bash
Executable file
#!/bin/bash
|
|
set -e
|
|
|
|
APP_DIR="/srv/webvirtcloud"
|
|
DATA_DIR="$APP_DIR/data"
|
|
mkdir -p "$DATA_DIR" "/var/www/.ssh" "$APP_DIR/backups"
|
|
|
|
# If settings.py doesn't exist, generate from template
|
|
if [ ! -f "$APP_DIR/webvirtcloud/settings.py" ]; then
|
|
echo "* Generating webvirtcloud/settings.py from template..."
|
|
cp "$APP_DIR/webvirtcloud/settings.py.template" "$APP_DIR/webvirtcloud/settings.py"
|
|
fi
|
|
|
|
# Ensure SECRET_KEY is persisted across container restarts if not supplied via environment
|
|
if [ -z "$SECRET_KEY" ]; then
|
|
if [ ! -s "$DATA_DIR/secret_key" ]; then
|
|
echo "* Generating fresh random SECRET_KEY..."
|
|
"$APP_DIR/venv/bin/python3" -c 'import secrets; print(secrets.token_urlsafe(50))' > "$DATA_DIR/secret_key"
|
|
chmod 600 "$DATA_DIR/secret_key"
|
|
fi
|
|
fi
|
|
|
|
# Persist SQLite database in DATA_DIR
|
|
if [ ! -f "$DATA_DIR/db.sqlite3" ] && [ -f "$APP_DIR/db.sqlite3" ] && [ ! -L "$APP_DIR/db.sqlite3" ]; then
|
|
mv "$APP_DIR/db.sqlite3" "$DATA_DIR/db.sqlite3"
|
|
fi
|
|
touch "$DATA_DIR/db.sqlite3"
|
|
ln -sf "$DATA_DIR/db.sqlite3" "$APP_DIR/db.sqlite3"
|
|
|
|
# Run database migrations
|
|
echo "* Running database migrations..."
|
|
"$APP_DIR/venv/bin/python3" "$APP_DIR/manage.py" migrate --noinput
|
|
|
|
# Set proper permissions on runtime and data directories
|
|
chown -R www-data:www-data "$DATA_DIR" "/var/www/.ssh" "$APP_DIR/backups"
|
|
chown www-data:www-data "$APP_DIR/webvirtcloud/settings.py" 2>/dev/null || true
|
|
chmod 700 "/var/www/.ssh" 2>/dev/null || true
|
|
# The database holds compute passwords and password hashes; keep it, the
|
|
# secret key and settings private to the app user (as the installer does)
|
|
chmod 700 "$DATA_DIR" "$APP_DIR/backups"
|
|
chmod 600 "$DATA_DIR/db.sqlite3" "$APP_DIR/webvirtcloud/settings.py"
|
|
for f in "$DATA_DIR/db.sqlite3-wal" "$DATA_DIR/db.sqlite3-shm"; do
|
|
if [ -e "$f" ]; then chown www-data:www-data "$f"; chmod 600 "$f"; fi
|
|
done
|