๐Ÿ“ฆ Chocorean / pibackup

๐Ÿ“„ pibackup.sh ยท 240 lines
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240#!/bin/bash

set -eo pipefail

script_name="$(basename "$0")"
script_path="$(readlink -f "${BASH_SOURCE[0]}")"
script_dir="$(cd "$(dirname "$script_path")" &> /dev/null && pwd)"
script_version=$(cd $script_dir ; git describe --tags --abbrev=0 || echo '0.5')

#######################
# print related stuff #
#######################

function err() {
  echo "$1 $2 $3" >&2
}

function p() {
  $quiet || echo "[$script_name] $* ..."
}


#####################
# CLI related stuff #
#####################

function usage() {
  cat << END
---
$script_name $script_version
---

usage: $script_name -o <output> [options]

Required parameters:
  -o, --output-dir [DIRECTORY]  Where backup will be saved and rotated.

Optional parameters:
  -h, --help                    Display this message.
  -n, --image-name [NAME]       Rename the backup file as '<NAME>.img.x'.
                                  Default: self (\$ uname -n)
  -r, --rotation-count [COUNT]  Quantity of files to be kept. Default: 8
  -t, --tmp-dir [DIRECTORY]     Temporary directory to use on the remote node. Default: /tmp
  -T, --target [HOSTNAME]       Name of the host to backup. Default: self ($ uname -n)
  -q, --quiet                   Silent mode.
  -z, --gzip                    Compress image using gzip.
  -Z, --xz                      Compress image using xz.
END
}

read_var() {
  if [ -n "$1" ]; then
    echo $1
  else
    err "$(usage)"
    exit 1
  fi
}

##########################
# pibackup related stuff #
##########################

function check() {
  if ! which "$1" > /dev/null; then
    err "check: $1 not found"
    exit 1
  fi
}

# Rotate files
function rotate() {
  src="$1"
  dst="$2"
  count="$3"

  filename="$(basename "$src")"
  for i in $(seq 0 $((count-2)) | tac)
  do
    target="$dst/$filename.$i"
    test -e "$target" && mv "$target" "$dst/$filename.$((i+1))"
    # empty command to avoid exit if test fails
    :;
  done
  mv "$src" "$dst/$filename.0"
}

#########
# BEGIN #
#########

node_name=$(uname -n)

######################
# Reading parameters #
######################

# Preparing optional parameters with default values
compress=false
target=$node_name
image_name=$target.img
ps_opt=''
rotation_count=8
tmp_dir=/tmp  # /mnt/hdd/tmp
quiet=false
z_ext=''

# This one is requried and has to be defined later
#output_dir=/mnt/hdd/backups/$node_name

# Reading parameters
while [ -n "$1" ]; do
  case "$1" in
    # Required
    -o | --output-dir)
      shift
      output_dir=$1
      ;;
    # Optional
    -h | --help)
      usage
      exit 0
      ;;
    -n | --image-name)
      shift
      image_name=$1
      force_name=true
      ;;
    -r | --rotation-count)
      shift
      rotation_count=$1
      ;;
    -t | --tmp-dir)
      shift
      tmp_dir=$1
      ;;
    -T | --target)
      shift
      target=$1
      if [ -z "$force_name" ]; then
        image_name="$target.img"
      fi
      ;;
    -q | --quiet)
      quiet=true
      ;;
    -z | --gzip)
      compress=true
      ps_opt='-z'
      z_ext='gz'
      ;;
    -Z | --xz)
      compress=true
      ps_opt='-Z'
      z_ext='xz'
      ;;
    # Default
    *)
      err "$(usage)"
      exit 1
      ;;
  esac
  shift
done

# Checking required parameters
if [ -z "$output_dir" ]; then
  err 'Missing required parameter: -o, --output-dir'
  err "$(usage)"
  exit 1
fi

########
# Init #
########

image_path=$tmp_dir/$image_name

# Making sure requirements are satisfied
check pishrink.sh

# Defining commands
chown_cmd='sudo chown pi:pi'
shrink_cmd='sudo pishrink.sh -a'
rotate_cmd="rotate"

# dd command is made of two parts
if [[ "$node_name" == "$target" ]]; then  # local
  dd_cmd='sudo dd if=/dev/mmcblk0 bs=4M conv=noerror,sync'
else  # remote
  dd_cmd="ssh $target sudo dd if=/dev/mmcblk0 bs=4M conv=noerror,sync"
fi
if $quiet; then
  dd_cmd="$dd_cmd 2> /dev/null | dd of=$image_path bs=4M 2> /dev/null"
else
  dd_cmd="$dd_cmd | dd of=$image_path bs=4M"
fi

# Compression options
if $compress; then
  shrink_cmd="$shrink_cmd $ps_opt"
fi

# More quiet commands
if $quiet; then
  shrink_cmd="$shrink_cmd > /dev/null"
  rotate_cmd="$rotate_cmd > /dev/null"
fi

###############################
# Actual backup starting here #
###############################

# Splitting dd command in half so root doesn't write the image
p 'Dumping sdcard'
eval "$dd_cmd"

p 'Setting permissions'
eval "$chown_cmd $image_path"

p 'Shrinking image'
eval "$shrink_cmd $image_path"

# PiShrink will rename image if -z/-Z
if $compress; then
  image_path="$image_path.$z_ext"
fi

p 'Rotating previous images'
mkdir -p $output_dir/$target
eval "$rotate_cmd $image_path $output_dir/$target $rotation_count"

# We've made it!
p 'Done'

#######
# END #
#######