aboutsummaryrefslogtreecommitdiffstats
path: root/ddrescue_r
blob: 38fddc4cdd6b45deb8803965456703606e9256c8 (plain)
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
#!/bin/bash

set -o nounset
set -o errexit
set -o pipefail

readonly ARGS=( "${@}" );

main()
{
  local _input_root;
  _input_root="${1}"; shift;

  local _output_root;
  _output_root="${1}"; shift;

  local input_root;
  input_root="$(readlink --canonicalize-existing "${_input_root}")";

  local output_root;
  output_root="$(readlink --canonicalize-missing "${_output_root}")";

  local input_length;
  input_length=$((${#input_root} + 1));

  find "${input_root}" -type d -print0 \
   | while read -r -d $'\0' d;
     do
       local dname;
       dname="${d:${input_length}}";

       local target;
       target="${output_root}/${dname}";

	   >&2 echo "*** ${dname}/"
       mkdir -p "${target}"
     done

  >&2 echo "Directory tree restored"

  find "${input_root}" -type f -print0 \
   | while read -r -d $'\0' f;
     do
       local fname;
       fname="${f:${input_length}}";

       >&2 echo "+++ ${fname}"
	   ddrescue --quiet "${input_root}/${fname}" "${output_root}/${fname}"
     done

  >&2 echo "Done."
}

main "${ARGS[@]}"