summaryrefslogtreecommitdiff
path: root/backup-progress.in
blob: 6b3d513551a4668b4fbdcea14503a8494421032f (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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#!/bin/bash

. #ETCDIR#/backup.conf

export LC_ALL=C

declare -A nm
declare -A pc
declare -A sz

for backupID in "${!backups[@]}"; do
  [ -s "/tmp/${backupID}.pid" ] || continue
  kill -0 $(cat "/tmp/${backupID}.pid") || continue
  dir="${backups["${backupID}"]%% *}"
  dir="${dir%/}"
  nm[${#nm[@]}]=$(
    echo ${dir} \
    | sed "s#/\(${recognSubdirRegex}\)\$#_\1#" \
    | sed "s#/rsync\$##; s#/duplicity\$##" \
    | sed 's#^.*/##'
  )
  last=$(
    ls -1 "$dir" \
      | grep '[0-9]\{4\}_[0-9]\{2\}_[0-9]\{2\}' \
      | sort \
      | tail -n1
  )
  last_size=$(
    ${du_timeout+timeout "${du_timeout}"} du -sb "${dir}/${last}" \
      | awk '{print $1}'
  )
  if [ -z "${last_size}" ]; then
    last_size='4096'
  fi
  current_size=$(
    ${du_timeout+timeout "${du_timeout}"} du -sb "${dir}/aktuell" \
      | awk '{print $1}'
  )
  if [ -z "${current_size}" ]; then
    current_size='0'
  fi
  size="${current_size}"
  decimal=''
  for unit in '' k M G T P Y; do
    if [ "${size}" -lt 1024 ]; then
      break
    fi
    decimal=".$(((size%1024*10)/1024))"
    size=$((size/1024))
  done
  percentage=$(
    printf '100*%s/%s\n' "${current_size}" "${last_size}" \
      | bc -l
  )
  pc[${#pc[@]}]=$(
    printf '%6.2f %%' "${percentage}"
  )
  sz[${#sz[@]}]=$(
    printf '(%s%s %sB)' "${size}" "${decimal}" "${unit}"
  )
done

nm_len=$(
  for n in "${nm[@]}"; do
    printf '%s\n' "${#n}"
  done \
  | sort -n \
  | tail -n1
)

pc_len=$(
  for p in "${pc[@]}"; do
    printf '%s\n' "${#p}"
  done \
  | sort -n \
  | tail -n1
)

sz_len=$(
  for s in "${sz[@]}"; do
    printf '%s\n' "${#s}"
  done \
  | sort -n \
  | tail -n1
)

for i in "${!nm[@]}"; do
  printf '%-'"${nm_len}"'s  %'"${pc_len}"'s  %'"${sz_len}"'s\n' \
    "${nm[${i}]}" \
    "${pc[${i}]}" \
    "${sz[${i}]}"
done