Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
name: test

on:
pull_request:
branches: [main, dev]
push:
branches: [main]

permissions:
contents: read

concurrency:
group: test-${{ github.ref }}
cancel-in-progress: true

jobs:
test:
runs-on: ubuntu-24.04
timeout-minutes: 10
steps:
- uses: actions/checkout@v4
with:
persist-credentials: false
- name: Install dependencies
run: |
sudo apt-get update
sudo apt-get install -y shellcheck bubblewrap libarchive-tools
- name: Allow unprivileged user namespaces
run: sudo sysctl -w kernel.apparmor_restrict_unprivileged_userns=0
- name: Test
run: make test
33 changes: 33 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
SHELL := bash
SHELLFILES := mgrecover.sh $(wildcard tests/*.sh)

define need
@command -v $(1) >/dev/null || { echo "missing tool: $(1) (Debian/Ubuntu: apt install $(2); Arch: pacman -S $(3))" >&2; exit 1; }
endef

.NOTPARALLEL:
.PHONY: test check-tools lint unit sim

test: check-tools lint unit sim

check-tools:
$(call need,shellcheck,shellcheck,shellcheck)
$(call need,bwrap,bubblewrap,bubblewrap)
$(call need,bsdtar,libarchive-tools,libarchive)
$(call need,python3,python3,python)

lint:
$(call need,shellcheck,shellcheck,shellcheck)
shellcheck -x -P SCRIPTDIR $(SHELLFILES)

unit:
$(call need,bsdtar,libarchive-tools,libarchive)
$(call need,python3,python3,python)
bash tests/unit.sh

sim:
$(call need,bwrap,bubblewrap,bubblewrap)
$(call need,bsdtar,libarchive-tools,libarchive)
$(call need,python3,python3,python)
bash tests/sim-root.sh
bash tests/sim-sandbox.sh
57 changes: 56 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,56 @@
# Recovery
# MediaGarrd Recovery

`mgrecover.sh` restores a [MediaGarrd](https://mediagarrd.github.io/MediaGarrd/) backup archive (`<timestamp>-media-backup.zip`) to the original paths of its files, with contents, permissions, owners (as root) and file modification times.

## Requirements

- bash 4+, `bsdtar` (Debian/Ubuntu: `apt install libarchive-tools`, Arch: `pacman -S libarchive`, Fedora: `dnf install bsdtar`), coreutils, `awk`, `sed`

## Install

```sh
git clone --depth 1 https://github.com/MediaGarrd/Recovery.git
cd Recovery
```

Optional: `sudo install -m 755 mgrecover.sh /usr/local/bin/mgrecover`.

## Usage

```sh
sudo ./mgrecover.sh 20261112-140649-938-media-backup.zip
./mgrecover.sh -n 20261112-140649-938-media-backup.zip
./mgrecover.sh -C /tmp/restore 20261112-140649-938-media-backup.zip
```

| Option | Meaning |
| --- | --- |
| `-C, --root DIR` | Restore under `DIR` instead of `/`. For inspection: no symlinks are created. |
| `-n, --dry-run` | Print the plan and write nothing. |
| `-y, --yes` | Do not ask for confirmation (required without a terminal). |
| `-h, --help` | Show usage. |

Exit status is `0` on success (also with warnings) and non-zero on errors.

## Symlinks

- Restoring to `/`: a link `L -> T` is recreated only if its target (following links stored in the archive, up to 8 hops) exists on the machine or is stored in the archive. Otherwise the link is not created, files stored below it go to a real directory at `L` (a dangling link already there is removed), and a link with nothing below it is skipped with a warning. Real files or directories already at `L` are never replaced. Links already on the machine, such as `/var/run`, are followed. Relative `..` targets are judged by name, not through other links. Link times are not restored, link owners only as root, and directories that receive content through kept links (the link targets) get the current time.
- With `-C DIR`: no link is created; files stored below a link are restored as a real directory. Existing symlinks under `DIR` are not followed. Files are restored without owners, but setuid/setgid bits are kept, so as root review the tree before making it reachable by others.
- Both modes refuse the whole archive, before writing anything, if a name has `..`, empty or `.` components, a leading `/`, a control character or a newline.

## Testing

`make test` runs shellcheck, unit tests and simulated restores (some inside a `bwrap` sandbox with a private fake `/`; the real `/` is never touched). It needs `shellcheck`, `bubblewrap`, `bsdtar` and `python3` (Debian/Ubuntu: `apt install shellcheck bubblewrap libarchive-tools python3`). Link owners (`chown -h`) are not tested, since the sandbox maps only uid 0.

## Stop the server first

The archive contains the MediaGarrd server's database (`mediagarrd.db`). Stop the server before restoring, or the live database is corrupted.

## Links

- Website: <https://mediagarrd.github.io/MediaGarrd/>
- Server: <https://github.com/MediaGarrd/Server>

## License

GPL-3.0, see [LICENSE](LICENSE).
208 changes: 208 additions & 0 deletions mgrecover.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,208 @@
#!/usr/bin/env bash
set -euo pipefail

PROG=${0##*/}
SEP=$'\037'
ROOT=/ DRY=0 YES=0 WARNS=0 ERRS=0 FULL=1 ARGS=()

usage() {
cat <<USAGE
Usage: $PROG [-C DIR] [-n] [-y] <backup.zip>
-C, --root DIR restore under DIR, without creating symlinks (inspection)
-n, --dry-run show the plan and write nothing
-y, --yes do not ask for confirmation
-h, --help show this help
Stop the MediaGarrd server first. Run as root to restore owners.
USAGE
}
die() { printf '%s: error: %s\n' "$PROG" "$*" >&2; exit 1; }
warn() { printf 'warning: %s\n' "$*" >&2; WARNS=$((WARNS + 1)); }
mp() { local c="" x a; IFS=/ read -ra a <<<"$1"; for x in ${a[@]+"${a[@]}"}; do c+=${c:+/}$x; if [[ -n ${KP[$c]+y} ]]; then c=${KP[$c]}; fi; done; MP=$c; }
pat() { sed -e 's/[][\\*?^$]/\\&/g' -e "s|^|$1|" -e "s|\$|$2|"; }
chk() { LC_ALL=C awk -v d="$1" -v S="$SEP" 'function par(s) { if (!sub(/\/[^\/]*$/, "", s)) s = ""; return s }
function rs(s, o, i) { while ((i = index(s, "\001"))) { o = o substr(s, 1, i - 1) "\\"; s = substr(s, i + 1) } return o s }
function rk(l, r, s, p, i, v) {
if (l in rank) return rank[l]
rank[l] = 0
for (i = 1; i <= 2; i++) {
p = (i == 1) ? par(l) : pp[l]
while (p != "") { if (p in tg) { v = rk(p) + 1; if (v > r) r = v } p = par(p) }
}
return rank[l] = r + 0
}
function norm(p, n, a, i, k, o, r) {
n = split(p, a, "/"); k = 0
for (i = 1; i <= n; i++) {
if (a[i] == "" || a[i] == ".") continue
if (a[i] == "..") { if (k > 0) k--; continue }
o[++k] = a[i]
}
for (i = 1; i <= k; i++) r = r (i > 1 ? "/" : "") o[i]
return r
}
NR == FNR { nm[FNR] = $0; nn = FNR; next }
{
raw = nm[FNR]; ln = $0
gsub(/\\\\/, "\001", raw); gsub(/\\\\/, "\001", ln)
if (index(raw, "\\") || index(ln, "\\")) { print "unsupported name or link target (control character): " nm[FNR] > "/dev/stderr"; bad = 1; next }
raw = rs(raw); ln = rs(ln)
c = raw; sub(/\/$/, "", c)
if (c == "" || index("/" c "/", "//") || index("/" c "/", "/./") || index("/" c "/", "/../")) { print "unsafe entry name: " raw > "/dev/stderr"; bad = 1; next }
total++; bytes += $5
if (c ~ /(^|\/)mediagarrd\.db$/) db = 1
if (substr($0, 1, 1) == "l") {
i = index(ln, " " raw " -> ")
if (!i) { print "cannot parse link entry: " raw > "/dev/stderr"; bad = 1; next }
tg[c] = substr(ln, i + length(raw) + 5); u[c] = $3; g[c] = $4
} else {
nl[c] = 1
p = par(c); while (p != "" && !(p in nla)) { nla[p] = 1; p = par(p) }
}
p = par(c); while (p != "" && !(p in all)) { all[p] = 1; p = par(p) }
}
END {
if (FNR != nn) { print "archive listing is inconsistent (newline in a name?)" > "/dev/stderr"; bad = 1 }
printf "%d %.0f %d %d\n", total, bytes, db, bad > (d "/stats")
if (bad) exit 3
for (p in nl) print p > (d "/prov")
for (p in nla) print p > (d "/prov")
for (l in tg) {
cur = l; h = 0; ok = 1; P = ""
while (1) {
t = tg[cur]
if (t == "") { ok = 0; break }
P = (t ~ /^\//) ? norm(t) : norm(par(cur) "/" t)
if (!(P in tg)) break
if (++h > 8) { ok = 0; break }
cur = P
}
pp[l] = P; okk[l] = ok
}
for (l in tg) print rk(l) S l S tg[l] S pp[l] S okk[l] S ((pp[l] in nl) || (pp[l] in nla) ? 1 : 0) S ((l in all) ? 1 : 0) S u[l] S g[l] > (d "/links.raw")
}' "$1/n" "$1/v"; }
if [[ ${BASH_SOURCE[0]-$0} != "$0" ]]; then return 0; fi

TMP=$(mktemp -d "${TMPDIR:-/tmp}/mgrecover.XXXXXX")
trap 'rm -rf -- "$TMP"' EXIT
trap 'exit 130' INT
trap 'exit 143' TERM

while (($#)); do
case $1 in
-C | --root) (($# > 1)) || die "$1 needs an argument"; ROOT=$2; shift ;;
--root=*) ROOT=${1#*=} ;;
-n | --dry-run) DRY=1 ;;
-y | --yes) YES=1 ;;
-h | --help) usage; exit 0 ;;
--) shift; ARGS+=("$@"); break ;;
-?*) usage >&2; exit 2 ;;
*) ARGS+=("$1") ;;
esac
shift
done
((${#ARGS[@]} == 1)) || { usage >&2; exit 2; }
ARCH=${ARGS[0]}

command -v bsdtar >/dev/null || die "bsdtar not found (Debian/Ubuntu: apt install libarchive-tools; Arch: pacman -S libarchive; Fedora: dnf install bsdtar)"
[[ -f $ARCH && -r $ARCH ]] || die "cannot read $ARCH"
[[ $(head -c2 -- "$ARCH") == PK ]] || die "not a zip archive: $ARCH"
[[ -n $ROOT ]] || die "empty root"
if [[ -d $ROOT ]]; then ROOT=$(CDPATH='' cd -- "$ROOT" && pwd -P); elif [[ -e $ROOT ]]; then die "not a directory: $ROOT"; fi
if [[ $ROOT =~ ^/+$ ]]; then ROOT=/; else FULL=0; fi

u=0
if [[ $(locale charmap 2>/dev/null) == UTF-8 ]]; then u=1; fi
for l in C.UTF-8 C.utf8 en_US.UTF-8 en_US.utf8; do
if ((u)); then break; fi
if ! command -v locale >/dev/null || locale -a 2>/dev/null | grep -xF "$l" >/dev/null; then export LC_ALL=$l; u=1; fi
done
((u)) || warn "no UTF-8 locale found; non-ASCII names may fail"

bsdtar -tf "$ARCH" >"$TMP/n" || die "bsdtar cannot read $ARCH"
bsdtar --numeric-owner -tvf "$ARCH" >"$TMP/v" || die "bsdtar cannot read $ARCH"
[[ -s $TMP/n ]] || die "archive is empty"
: >"$TMP/links.raw"; : >"$TMP/prov"

chk "$TMP" || die "archive has entries this tool refuses to restore; nothing was written"
read -r TOTAL BYTES DB _ <"$TMP/stats"
LC_ALL=C sort -n -t "$SEP" -k1,1 "$TMP/links.raw" >"$TMP/links"

: >"$TMP/all"; : >"$TMP/kept"; : >"$TMP/sub"; : >"$TMP/nofile"; : >"$TMP/inc"; : >"$TMP/rm"
declare -A KP=()
D=${ROOT%/}
nk=0 nc=0 ns=0
while IFS=$SEP read -r _ L T P OK PROV KIDS U G; do
printf '%s\n' "$L" >>"$TMP/all"
mp "$P"; pm=$MP; mp "$L"; lm=$MP
why=""
if ((!FULL)); then why="no links under --root"
elif ((!OK)); then why="broken link chain"
elif ((!PROV)) && [[ ! -e /$pm ]] && ! { [[ $pm != "$P" ]] && grep -qxF -- "$pm" "$TMP/prov"; }; then why="target /$P does not exist and is not in the archive"
elif [[ -e /$lm && ! -L /$lm ]]; then why="$D/$L already exists; real data is kept"
fi
if [[ -z $why ]]; then
printf '%s\n' "$L" >>"$TMP/sub"
if ((KIDS)); then printf '%s\n' "$L" >>"$TMP/inc"; fi
printf '%s\n' "$L$SEP$T$SEP$P$SEP$KIDS$SEP$U$SEP$G" >>"$TMP/kept"
KP[$L]=$pm
echo "keep $D/$L -> $T"
((++nk))
elif ((KIDS)); then
if ((FULL)) && [[ -e /$lm && ! -L /$lm && ! -d /$lm ]]; then
printf '%s\n' "$L" >>"$TMP/nofile"
warn "$D/$L is a file: entries below it are not restored (link to $T not created)"
((++ns))
else
how="restored as a real directory"
if ((FULL)) && [[ -L /$lm ]]; then
if [[ -e /$lm ]]; then how="written through the existing link"; else printf '%s\n' "/$lm" >>"$TMP/rm"; fi
fi
echo "convert $D/$L -> $T ($why): contents $how"
((++nc))
fi
else
if ((FULL)); then warn "$D/$L -> $T skipped ($why)"; else echo "skip $D/$L -> $T ($why)"; fi
((++ns))
fi
done <"$TMP/links"
pat '^' '$' <"$TMP/all" >"$TMP/x1"
pat '^' / <"$TMP/sub" >"$TMP/x2"
pat '^' / <"$TMP/nofile" >"$TMP/x4"
pat '' '/*' <"$TMP/inc" >"$TMP/x3"

echo "Archive : $ARCH ($TOTAL entries, $(numfmt --to=iec "$BYTES"))"
echo "Root : $ROOT"
echo "Links : $nk kept, $nc with content below them converted, $ns skipped"
if ((DB)); then warn "archive contains mediagarrd.db: the MediaGarrd server must be stopped before restoring"; fi
if ((FULL && EUID != 0)); then warn "not running as root: owners cannot be restored and some paths may fail"; fi
if ((!FULL && EUID == 0)); then warn "running as root: setuid/setgid files from the archive keep their bits under $D"; fi
if ((DRY)); then echo "Dry run: nothing was written."; exit 0; fi

if ((!YES)); then
[[ -t 0 ]] || die "no terminal to ask for confirmation; use --yes"
read -r -p "Restore now? [y/N] " a || a=
[[ $a == [yY]* ]] || die "aborted"
fi

mkdir -p -- "$ROOT"
xt=(bsdtar -xpf "$ARCH" -C "$ROOT")
if ((FULL)); then xt+=(-P); else xt+=(--no-same-owner); fi
while IFS= read -r p; do rm -f -- "$p" || ERRS=$((ERRS + 1)); done <"$TMP/rm"
cat "$TMP/x1" "$TMP/x2" "$TMP/x4" >"$TMP/xa"
cat "$TMP/x1" "$TMP/x4" >"$TMP/xc"
"${xt[@]}" -X "$TMP/xa" || { ERRS=$((ERRS + 1)); echo "$PROG: bsdtar reported errors" >&2; }
if [[ -s $TMP/kept ]]; then
while IFS=$SEP read -r L T P KIDS U G; do
if ! mkdir -p -- "$(dirname -- "/$L")" || { ((KIDS)) && [[ ! -e /$P ]] && ! mkdir -p -- "/$P"; }; then
ERRS=$((ERRS + 1)); continue
fi
ln -sfnT -- "$T" "/$L" || ERRS=$((ERRS + 1))
if ((EUID == 0)); then chown -h "$U:$G" "/$L" || true; fi
done <"$TMP/kept"
fi
if [[ -s $TMP/inc ]]; then
"${xt[@]}" -X "$TMP/xc" -T "$TMP/x3" || { ERRS=$((ERRS + 1)); echo "$PROG: bsdtar reported errors" >&2; }
fi

echo "Done: $TOTAL entries, $nk links kept, $nc converted, $ns skipped, $WARNS warnings, $ERRS errors."
((ERRS == 0))
22 changes: 22 additions & 0 deletions tests/common.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#!/usr/bin/env bash
# shellcheck disable=SC2034
GP=('gen/sp dir' 'gen/sp dir/a b.txt' 'gen/glob/*[x]?.txt' 'gen/glob/[a]' 'gen/utf8/héllo-日本.txt' 'gen/arrow/a -> b.txt' 'gen/back/we\ird.txt' 'gen/mode/exec.sh' 'gen/mode/secret')
GEN_SPEC='d|gen/sp dir|750
f|gen/sp dir/a b.txt|600|spaces
f|gen/glob/*[x]?.txt|644|glob
f|gen/glob/[a]|755|bracket
f|gen/utf8/héllo-日本.txt|644|utf8
f|gen/arrow/a -> b.txt|644|arrow
f|gen/back/we\\ird.txt|644|backslash
f|gen/mode/exec.sh|755|exec
f|gen/mode/secret|600|secret'
GEN_WANT='gen/sp dir|directory|750|1577836800
gen/sp dir/a b.txt|regular file|600|1577836800
gen/glob/*[x]?.txt|regular file|644|1577836800
gen/glob/[a]|regular file|755|1577836800
gen/utf8/héllo-日本.txt|regular file|644|1577836800
gen/arrow/a -> b.txt|regular file|644|1577836800
gen/back/we\ird.txt|regular file|644|1577836800
gen/mode/exec.sh|regular file|755|1577836800
gen/mode/secret|regular file|600|1577836800'
STAT_FMT='%n|%F|%a|%Y'
16 changes: 16 additions & 0 deletions tests/inner.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#!/usr/bin/env bash
dump() {
local skip=(\( -path /usr -o -path /proc -o -path /dev -o -path /tmp -o -path /.mg -o -path /bin -o -path /lib -o -path /lib64 -o -path /sbin -o -path /etc/alternatives \) -prune -o)
find / -mindepth 1 "${skip[@]}" ! -path /etc -printf '%p|%y|%m|%U|%l\n' | LC_ALL=C sort
find / -mindepth 1 "${skip[@]}" -type f -exec sha256sum {} + | LC_ALL=C sort
}
cd /
eval "$SETUP"
dump >/.mg/out/tree0
for ((i = 1; i <= ${RUNS:-1}; i++)); do
bash /.mg/mgrecover.sh "$@" >"/.mg/out/log$i" 2>&1 <"${STDIN:-/dev/null}"
echo $? >"/.mg/out/rc$i"
dump >"/.mg/out/tree$i"
done
eval "$POST" >/.mg/out/post 2>&1
exit 0
Loading
Loading