Hello, On Mon, 12 Oct 2009, Dean Hilkewich wrote:
David Haller wrote:
On Mon, 12 Oct 2009, Dean Hilkewich wrote:
Anders Johansson wrote:
Hm, ok, something like this then
for clientdir in /backup/*; do cd $clientdir for daydir in $clientdir/*; do rarfile=$daydir/*.rar if [ ! -e $rarfile ]; then rarfile=$daydir/*.r01 fi
if ! test -e $rarfile; then echo "cannot find rarfile in $clientdir" >&2 continue; ### or exit? Depends what you want fi
unrar x $rarfile [ $? -eq 0 ] && rm -rf $daydir
Why the 'test'? And 'x' is the wrong command (fullpath, you want current dir, it might make no difference here though, that depends on how the rar was packed). And 'rar' is more robust, so one should prefer that over 'unrar' if rar is installed (which I assume).
rar e "$rarfile" && rm -rf "$daydir"
I'm not that sure on what conditions rar exits with a status != 0, so one might want to test / read up on that before really running that 'rm -rf'. That affects both variants.
done done
So what would the script look like if you were to put it though David?
Something like this: ==== This script was brought to you by a very tired 'dnh', to the sound of "The Allman Brothers Band" playing "Whipping Post" and more than a couple of good german beers and should be very closely scrutinized before use! I AM NOT RESPONSIBLE FOR ANYTHING! ==== #!/bin/bash BASEDIR="/backup" verbose=1 ### log to which file? Or use tee and/or logger ... Lots of nifty ### things possible ;) Default (no exec "rewiring"): log to ### STDERR i.e. /dev/stderr # exec 2>/var/log/backup_extraction_logfile pushd "$BASEDIR" || exit 3 ### ensuring a) we can change there, and b) ### gets us a defined stack for the popd in ### following the loop for clientdir in "${BASEDIR}"/*; do popd ### Untested, should work, because the "continue 2" ... pushd "$clientdir" || { echo "Cannot pushd to '$clientdir'" >&2; exit 1; ### one could try to 'continue' instead } for daydir in "$clientdir"/*; do for exts in rar r00 r01; do ### I think I remember seeing .r00 as the first part of ### a split rar-Archive, at least more than .r01. This ### way, we should cover it, going by likelyhood of ### extensions. rarfile="$daydir"/*.${ext} ### the archive should not only exist (-e) but also be readable (-r)! if test -r "$rarfile"; then # ### Simple case: skip deletion on extraction failure: # rar e "$rarfile" && rm -rf "$daydir" # ### Alternatively something like this: rar e "$rarfile" ### Handle rar return-codes as wanted/needed: case $? in ### USER BREAK 255) exit 2;; #### 9 CREATE ERROR Create file error 9) echo "CREATE ERROR" >&2; continue 2;; ### MEMORY ERROR Not enough memory for operation 8) echo "ENOMEM" >&2; continue 2;; ### 7 USER ERROR Command line option error ### should not happen in this script 7) echo "Internal script-error: Invalid option to rar" >&2; continue 2 ;; ### 6 OPEN ERROR Open file error 6) echo "Could not open file" >&2; continue 2;; ### 5 WRITE ERROR Write to disk error 5) echo "Could not write file" >&2; continue 2;; ### 4 LOCKED ARCHIVE Attempt to modify ### an archive previously locked by the 'k' ### command ### 3 CRC ERROR A CRC error occurred ### when unpacking 3) echo "rar CRC ERROR" >&2; continue 2;; ### 2 FATAL ERROR A fatal error occurred 2) echo "rar FATAL ERROR" >&2; exit 1; ### 1 WARNING Non fatal error(s) occurred # 1) echo "warning ignored" >&2;; ### 0 == Success 0) if test $verbose -gt 0; then echo "Successfully extracted '$rarfile', removing '$daydir'" >&2; rm -rf "$daydir" fi continue 2 ;; ### default (should not happen), so abort if it does *) echo "Unknown 'rar' return code: $?" >&2; exit 2;; esac fi done ### no .rar, r00 nor .r01 found ... Emit message and continue ... echo "cannot find a rarfile in '$clientdir'" >&2 continue; ### continue next loop "out", i.e. skip this ### dir. Exit script instead?? That'll depend ### on what you want done done ==== I hope I've got it right. Basic "sanity checking" should suffice though ;)
If it exits other then 0 would it be possible to log the failure and continue on to the next leaving the failed extractions source directory untouched?
Sure. BTW: I think you really DO want to change your dir- and archivenames to the ISO format of YYYYMMDD (e.g. 20091013) in the long run ... HTH, -dnh, very tired and definitely not sober anymore ;) -- Such an archaic device, the letter -- but personal in a way no recording could achieve. -- from "Dune Messiah" by Frank Herbert -- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org For additional commands, e-mail: opensuse+help@opensuse.org