qubes.Restore 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. #!/bin/sh
  2. echo Starting Restorecopy >&2
  3. read -r args
  4. read -r paths
  5. echo "Arguments: $args" >&2
  6. echo "Paths: $paths" >&2
  7. if [ -f "$args" ] ; then
  8. echo "Performing restore from backup file $args" >&2
  9. TARGET="$args"
  10. echo "Copying $TARGET to STDOUT" >&2
  11. # shellcheck disable=SC2086
  12. /usr/lib/qubes/tar2qfile "$TARGET" $paths
  13. else
  14. echo "Checking if arguments is matching a command" >&2
  15. COMMAND=$(echo "$args" | cut -d ' ' -f 1)
  16. if command -v "$COMMAND" >/dev/null; then
  17. tmpdir=$(mktemp -d)
  18. mkfifo "$tmpdir/backup-data"
  19. echo "Redirecting $args to STDOUT" >&2
  20. # Parsing args to handle quotes correctly
  21. # Dangerous method if args are uncontrolled
  22. eval "set -- $args"
  23. # Use named pipe to pass original stdin to tar2file
  24. "$@" > "$tmpdir/backup-data" < /dev/null &
  25. # shellcheck disable=SC2086
  26. /usr/lib/qubes/tar2qfile "$tmpdir/backup-data" $paths
  27. # Restoration may be terminated earlier because of selected files. This
  28. # will be seen as EPIPE to the retrieving process, which may cause retcode
  29. # other than 0 in some cases - which would be incorrectly treated as backup
  30. # restore error. So instead of that, use tar2qfile exit code (and have dom0
  31. # detect if anything wrong with actual data)
  32. retcode=$?
  33. wait -n
  34. rm "$tmpdir/backup-data"
  35. rmdir "$tmpdir"
  36. exit "$retcode"
  37. else
  38. echo "Invalid command $COMMAND" >&2
  39. exit 2
  40. fi
  41. fi