Skip to content

Instantly share code, notes, and snippets.

@zed9h
Created April 24, 2010 18:48
Show Gist options
  • Save zed9h/377841 to your computer and use it in GitHub Desktop.
Save zed9h/377841 to your computer and use it in GitHub Desktop.
backup/mirror one or more dirs, with progress bar and logging
DISK=sdc
PART=$(grep $DISK[0-9] /proc/partitions | sed -n "s/.*[0-9][0-9] $DISK\([0-9]\+\)/\1/p")
for i in $PART
do
grep -q $DISK$i /proc/mounts || { mkdir -p /mnt/$DISK$i && mount /dev/$DISK$i /mnt/$DISK$i ; }
done
DEV=$(df | grep $DISK | sort -rn -k 4 | head -1 | awk '{ print $1 }')
for i in $PART
do
umount /dev/$DISK$i
rmdir /mnt/$DISK$i
done
MNT=/mnt/backup
mkdir -p $MNT
mount $DEV $MNT
df -h $MNT
ls $MNT
./mirror / $MNT
umount $DEV
rmdir $MNT
/proc/*
/sys/*
/dev/*
/media/*
/mnt/*
/archive/*
#!/bin/bash
# depends: pv curl cpio bc find sed test date basename dirname df tail wc tr cp cat mkdir
EX_FILE="$(dirname "$0")/exclude"
test -n "$1" -a -n "$2" || {
echo "usage: '$0' <source> [<source> ...] <destination-dir>"
echo "* there also can be exclude path name patterns on '$EX_FILE'"
exit 1
}
LAST=$(( $# - 1 ))
SRC=("$@")
DST=${SRC[$LAST]}/$(date +%Y%m%d)
unset SRC[$LAST]
test -e "$DST" || mkdir -p "$DST"
test -d "$DST" || { echo "destination-dir should be a directory" ; exit 1 ; }
LOG="$DST/$(basename "$0").$(date +%Y%m%d_%H%M%S)"
if test -s "$EX_FILE"
then
EX="-X $EX_FILE"
EX_FIND=$( sed "s/.*/-not -path '&' -and/" "$EX_FILE" | tr "\n" " ")
fi
FIND="find \"${SRC[@]}\" $EX_FIND-not -type d"
SIZE=$(
(
echo -n "x=-("
# scan directories to calculate cpio header overhead (26+filename+1)
eval "$FIND" | awk '{ n=length($0)+1 ; t+=(26+n+(n%2)) } END { printf "%d+", t }' # aligned-2
# scan inodes to calculate data size (directory structure already cached)
# TODO progress bar while getting size (count lines)
eval "$FIND -printf '%s\n'" | awk '{ printf "%d+", $1+($1%2) }' # aligned-2
echo "36)" # cpio trailer (26+10 "TRAILER!!\0")
echo "scale=0 ; xx=x/512 ; if(xx>x)xx-=1 ; -xx*512" # whole cpio 512-byte-aligned
) | bc | tail -1
)
FREE=$(
(
df "$DST" -B 1 |
sed -n 's/.* \+[0-9]\+ \+[0-9]\+ \+\([0-9]\+\) .*/\1/p' |
tr "\n" "*"
echo "0.9" # filesystem overhead
) | bc | cut -d . -f 1
)
test "$FREE" -gt "$SIZE" || {
echo "too few space for ~$SIZE bytes on destination '$DST'"
exit 1
}
# TODO calc md5 and detailed transfer log using: tee >(proc1) >(proc2) | proc3
eval "$FIND" |
tee "$LOG.transfer" |
cpio -o |
pv -i 15 -B 256m -s $SIZE |
(cd "$DST" && cpio -id --no-absolute-filenames --preserve-modification-time) 2> "$LOG.error"
mkdir -p transfer
cp -v "$LOG.transfer" "$(dirname "$0")/transfer"
cat "$LOG.error"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment