aboutsummaryrefslogtreecommitdiffstats
path: root/dorsync
blob: 67def1ab6a98ff6ab9f20421176d9e6be483142e (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
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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
#!/bin/bash
############################################
# If dcfldd is installed you will get a    #
# progress indicator when dumping to USB   #
############################################
# Enter a few variables.                   #
# location is where you stored the ISOs    #
# release is the directory on the server   #
# user is the rsync login username         #
# password is the rsync password           #
############################################

location=""
release=""
user=""
password=""

############################################
# To exclude directories from sync, create #
# a file, dorsync.skip in the start folder #         #
# containing the directory names only, one #
# per line.                                #
############################################

####### Don't touch below this line ########
#                                          #
############################################
#              dorsync v2.8                #
############################################
#          Claire Robinson (MrsB)          #
#             for Mageia QA                #
############################################
#            Thanks also go to..           #
#        Alfred Kretschmer (alfred)        #
#        Barry Jackson (barjac)            #
############################################

red='\e[0;31m'
green='\e[0;32m'
bold='\e[01m'
endcolour='\e[0m'
rsyncargs=()
rsyncargs+=" -avHP"
if [[ -e dorsync.skip ]]; then
  while read line; do
  rsyncargs+=" --exclude=$line"
  done < dorsync.skip
fi
rsyncargs+=" rsync://$user@bcd.mageia.org/isos/$release/ ."
cd "$location"
# Gap so I remember to add command line arguments

# Function to do the actual rsync and run checks afterwards
dorsync () {
echo "Starting rsync.."
RSYNC_PASSWORD="$password" rsync ${rsyncargs[@]}
OUT=$?
echo ""
if [ $OUT -eq 0 ];then
   echo "Completed rsync"
else
   echo "Rsync failed. Exiting."
   exit
fi

echo ""
du -h | sort -k 2 | sed "s|\./||" | sed -e "s|\(\s\)\.|\1Total\n|"
echo ""
echo "Starting checks, please ensure the dates are correct"
echo ""
# Loop through folders in $location and read info into arrays
# Hopefully able to use this later to dump to USB stick
# Perform checks too and highlight green OK red Failed
i=0
OIFS="$IFS"
IFS=$'\n'
for line in $(find . -type f -name "*.iso" -printf '%P\n')
do
  directory[$i]=`dirname "$line"`
  iso[$i]=`basename "$line"`
  echo "PATH: ${directory[$i]}"
  echo "ISO:  ${iso[$i]}"
  cd "${directory[$i]}"
  # Check for DATE.txt and display it or warn if missing
  if [ -e DATE.txt ]
  then
    echo "DATE: `cat DATE.txt`"
    missingdate[$i]=false
  else
    echo -e "DATE: ${red}Missing${endcolour}"
    missingdate[$i]=true
  fi
  # Check for md5 file and then md5sum it or warn if missing
  if [ -e "${iso[$i]}.md5" ]
  then
    md5sum -c --quiet --status *.md5 2>/dev/null
    OUT=$?
    if [ $OUT -eq 0 ];then
      echo -e "MD5:  ${green}OK${endcolour}     \c"
      md5ok[$i]=true
      missingmd5[$i]=false
    else
      echo -e "MD5:  ${red}Failed${endcolour}     \c"
      md5ok[$i]=false
      missingmd5[$i]=false
    fi
  else
    echo -e "MD5:  ${red}Missing${endcolour}     \c"
    missingmd5[$i]=true
  fi

  # Check for sha1 file and then sha1sum it or warn if missing
  if [ -e ${iso[$i]}.sha1 ]
  then
    sha1sum -c --quiet --status *.sha1 2>/dev/null
    OUT=$?
    if [ $OUT -eq 0 ];then
      echo -e "SHA1: ${green}OK${endcolour}"
      sha1ok[$i]=true
      missingsha1[$i]=false
    else
      echo -e "SHA1: ${red}Failed${endcolour}"
      sha1ok[$i]=false
      missingsha1[$i]=false
    fi
  else
    echo -e "SHA1: ${red}Missing${endcolour}"
    sha1ok[$i]=false
    missingsha1[$i]=true
  fi
  echo ""
  cd "$location"
  (( i++ ))
done
IFS="$OIFS"
# Check if anything failed and provide a summary
total=${#directory[@]}
warnings=0
startwarning="--------------------------------------------------\n${bold}Warning:${endcolour}\n"
for (( i=0; i<=$(( $total -1 )); i++ )) 
do
  # Warn of missing DATE.txt files
  if [ ${missingdate[$i]} = true ]
  then
    if [ $warnings = 0 ]
    then
      echo -e $startwarning
    fi
    echo "${directory[$i]}/DATE.txt missing"
    (( warnings++ ))
  fi
  # Warn of missing md5 files 
  if [ ${missingmd5[$i]} = true ]
  then
    if [ $warnings = 0 ]
    then
      echo -e $startwarning
    fi
    echo "${directory[$i]}/${iso[$i]}.md5 missing"
    (( warnings++ ))
  else
    # Warn of bad md5sum if present
    if [ ${md5ok[$i]} = false ]
    then
      if [ $warnings = 0 ]
      then
        echo -e $startwarning
      fi
      echo "${iso[$i]} md5sum failed"
      (( warnings++ ))
    fi
  fi
  # Warn of missing sha1 files
  if [ ${missingsha1[$i]} = true ]
  then
    if [ $warnings = 0 ]
    then
      echo -e $startwarning
    fi
    echo "${directory[$i]}/${iso[$i]}.sha1 missing"
    (( warnings++ ))
  else
    # Warn of bad sha1sum if present
    if [ ${sha1ok[$i]} = false ]
    then
      if [ $warnings = 0 ]
      then
        echo -e $startwarning
      fi
      echo "${iso[$i]} sha1sum failed"
      (( warnings++ ))
    fi
  fi
done
echo ""
# If anything failed, provide a count
if [ $warnings != 0 ]
then
  if [ $warnings -gt 1 ]
  then
    message="There are $warnings potential problems."
  else
    message="There is 1 potential problem."
  fi
  echo -e "${bold}$message${endcolour}"
  echo "--------------------------------------------------"
else
  echo "------------------------------------"
  echo ""
  echo -e "${bold}All tests passed OK. Yippee!${endcolour}"
  echo "Please check the dates are correct."
  echo ""
  echo "------------------------------------"
fi

}


# Function to list USB sticks
liststicks () {
  usbs=""
  for device in /sys/block/*/device
  do
    if echo $(readlink -f $device)|egrep -q "usb"
    then
      adisk=`echo $device | cut -f4 -d/`
      usbs="$usbs /dev/$adisk"
    fi
  done

  # Find info for each from udisks
  i=0
  for usb in $usbs
  do
    showinfo=`udisks --show-info $usb`
    isremovable=`echo "$showinfo" | grep "  removable:"| cut -f2 -d: | sed 's/^[ ]*//'`

    # Check udisks shows it as removable
    if [ $isremovable = 1 ]
    then
      usbdev[$i]="$usb"
      found[$i]=`echo "$showinfo" | grep "  detected at:"| cut -f2- -d: | sed 's/^[ ]*//'`
      vendor[$i]=`echo "$showinfo" | grep "vendor:" | cut -f2 -d: | sed 's/^[ ]*//'`
      model[$i]=`echo "$showinfo" | grep "model:" | cut -f2 -d: | sed 's/^[ ]*//'`
      sizebytes[$i]=`echo "$showinfo" | grep "  size:" | cut -f2 -d: | sed 's/^[ ]*//'`
      sizegb[$i]=`echo "scale=1;${sizebytes[$i]}/1024/1024/1024" | bc`
      (( i++ ))
    fi
  done
}


# Minimal function to find isos if rsync is skipped
findisos () {
  i=0
  OIFS="$IFS"
  IFS=$'\n'
  for line in $(find . -type f -name "*.iso" -printf '%P\n')
  do
    directory[$i]=`dirname "$line"`
    iso[$i]=`basename "$line"`
    (( i++ ))
  done
  IFS="$OIFS"
}


# Function to wait for usb stick to be inserted
dodoh () {
  while true
  do
    echo "No USB drives found. Please insert one now."
    read -n1 -e -p "Press Q to quit or any other key to continue: " num
    case $num in
      [Qq]* ) exit;;
      * ) liststicks
	  echo ""
	  if ! [ ${#found[@]} = 0 ]
	  then
	    doh="Yay"
	    break
	  fi
	  ;;
    esac
  done
}


# Function to actually do the dump onto usb
dothedeed () {
  echo ""
  echo "Please be patient this can take a while"
  echo ""
  echo "This operation requires root privileges"
  echo "Please enter your root password.."
  echo ""
  export rootwhere="${usbdev[$(($usbchoice-1))]}"
  export rootiso="$fullisopath"
  if [ -e /usr/bin/dcfldd ]
  then
    su -c 'dcfldd sizeprobe=if statusinterval=5 if="$rootiso" of="$rootwhere" bs=1M && sync'
  else
    su -c 'dd if="$rootiso" of="$rootwhere" bs=1M && sync'
  fi
  OUT=$?
  echo ""
  if [ $OUT -eq 0 ];then
    echo "Completed OK."
  else
    echo "Failed. Exiting."
    exit
  fi
}

# Function to choose usb stick & iso and check it'll fit
dodump () {
  echo ""
  didrsync=$1
  if [ $didrsync == 1 ]
  then
    # Ask to dump to USB or exit
    echo ""
    while true
    do
      read -n1 -e -p "Do you want to dump one onto USB? [y/n]" yn
      case $yn in
	[Yy] ) liststicks
		break;;
	[Nn] ) exit;;
	   * ) echo "Please choose y or n";;
      esac
    done
    echo ""
  else
    liststicks
  fi

  # If no USBs found D'oh! loop until there is one..
  total=${#found[@]}
  if [ $total = 0 ]
  then
    doh="doh"
    while [ "$doh" = "doh" ]
    do
      dodoh
    done
    total=${#found[@]}
  fi

  # Now there is a USB print the info
  for (( i=0; i<=$(( $total -1 )); i++ ))
  do
    if [ $i = 0 ]
    then
      echo ""
      echo "USB drives found:"
      echo ""
      echo -e "    Device\tSize\tWhen Found\t\t\tModel"
    fi
    echo -e "$((i+1))) ${usbdev[$i]}\t${sizegb[$i]}Gb\t${found[$i]}\t${vendor[$i]} ${model[$i]}"
  done

  # Choose which USB to use
echo ""
  while true
  do
  read -e -p "Please choose which USB to use or Q to quit and press <enter>: " usbchoice
   if [[ $usbchoice = [Qq] ]]; then 
   exit
   elif [[ "$usbchoice" =~ ^[0-9]+$ ]] && (( $usbchoice <= $total )) && (( $usbchoice > 0 )); then 
   break
   else echo "Invalid selection"
   fi
  done
  
  # List the ISOs
  total=${#iso[@]}
  for (( i=0; i<=$(( $total -1 )); i++ ))
  do
    if [ $i = 0 ]
    then
      echo ""
      echo "List of ISOs:"
    fi
    echo "$(($i+1))) ${iso[$i]}"
  done
  echo ""

  # Choose which ISO to use
  while true
  do
   read -e -p "Please choose which ISO to use or Q to quit and press <enter>: " isochoice
   if [[ $isochoice = [Qq] ]]; then 
   exit
   elif [[ "$isochoice" =~ ^[0-9]+$ ]] && (( $isochoice <= $total )) && (( $isochoice > 0 )); then 
   break
   else echo "Invalid selection"
   fi
  done

  # Will it fit?
  fullisopath=`readlink -f "${directory[$(($isochoice-1))]}/${iso[$(($isochoice-1))]}"`
  isosize=`du -b "$fullisopath" | cut -f1`
  usbsize=${sizebytes[$(($usbchoice-1))]}
  if [ $usbsize -lt $isosize ]
  then
    echo ""
    echo "Sorry. It won't fit! Exiting."
    exit
  fi

  # Warn
  usbinfo="${usbdev[$(($usbchoice-1))]} (${sizegb[$(($usbchoice-1))]}Gb ${vendor[$(($usbchoice-1))]} ${model[$(($usbchoice-1))]} found at ${found[$(($usbchoice-1))]})"
  echo ""
  echo -e "${bold}About to dump $fullisopath${endcolour}" 
  echo -e "${bold}onto $usbinfo${endcolour}"
  echo ""
  echo "This will destroy any data already on the USB device."

  # Confirm
  while true
  do
    read -n1 -e -p "Press Y to confirm or Q to quit: " yq
    case $yq in
      [Qq] ) exit;;
      [Yy] ) dothedeed
	     break;;
	 * ) echo -e "Invalid selection\n";;
    esac
  done
}


# Offer to skip rsync and just dump to usb
echo ""
while true
do
  read -n1 -e -p "Do you want to Rsync (R), skip Rsync and just dump to usb (D) or quit (Q): " rdq
case $rdq in
    [Rr] )  dorsync; dodump 1; exit;;
    [Dd] )  findisos; dodump 0; exit;;
    [Qq] )  exit;;
       * )  echo " $rdq Invalid selection"; echo "";;
  esac
done