#!/usr/bin/env bash set -euo pipefail usage() { echo "Использование: $0 --db <файл.db> [--output <файл.sqlite>] [--force]" } db="" output="" force=0 while [[ $# -gt 0 ]]; do case "$1" in --db) db="${2:-}"; shift 2 ;; --output) output="${2:-}"; shift 2 ;; --force) force=1; shift ;; -h|--help) usage; exit 0 ;; *) echo "Неизвестный аргумент: $1" >&2; usage >&2; exit 2 ;; esac done if [[ -z "$db" ]]; then usage >&2 exit 2 fi if ! command -v sqlite3 >/dev/null 2>&1; then echo "Не найдена программа sqlite3" >&2 exit 2 fi if [[ ! -f "$db" ]]; then echo "База не найдена: $db" >&2 exit 2 fi if [[ -z "$output" ]]; then output="backup_$(date +%Y%m%d_%H%M%S).sqlite" fi if [[ -e "$output" && "$force" -eq 0 ]]; then echo "Файл уже существует: $output. Добавьте --force" >&2 exit 2 fi mkdir -p "$(dirname "$output")" tmp=$(mktemp "${output}.tmp.XXXXXX") trap 'rm -f "$tmp"' EXIT sqlite3 -readonly "$db" ".backup '$tmp'" result=$(sqlite3 -readonly "$tmp" "PRAGMA integrity_check;") if [[ "$result" != "ok" ]]; then echo "Проверка целостности не прошла: $result" >&2 exit 1 fi mv "$tmp" "$output" trap - EXIT echo "База: $db" echo "Копия: $output" echo "Размер: $(du -h "$output" | cut -f1)" echo "Целостность: ok"