You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

178 lines
3.2 KiB

#!/usr/bin/env bash
set -eu
#set -x
FORCE=0
DRY=0
SYSTEMS=""
synopsis() {
cat << EOF
Usage : `basename $0` [-hdf] system...
EOF
}
help() {
cat << EOF
`basename $0` dépose les disques associés au(x) système(s) sur le serveur.
`synopsis`
Les disques des systèmes spécifiés sont déposés sur le serveur (rsync via ssh).
Pour cela, la variable SSH_REPOSITORY doit avoir été fixée dans votre
fichier \$HOME/.vdnrc. Exemple :
SSH_REPOSITORY="toto@truc.bidule.org:public_html/files"
Remarque : les fichiers "*.disk" sont préalablement compressés (.gz).
-h : affiche cette aide
-d : dry run
-f : force : pas de demande de confirmation !
EOF
}
usage() {
synopsis
exit 1
}
args() {
local opt
while getopts "hdf" opt; do
case $opt in
h) help; exit 0;;
d) DRY=1;;
f) FORCE=1;;
?) usage;;
esac
done
shift $(($OPTIND - 1))
[ $# -lt 1 ] && usage
SYSTEMS=$@
}
uploadDisks() {
local error=0 absent=0
LIST=""
LIST_DISKS=""
if [ -z "$SSH_REPOSITORY" ]; then
error "SSH_REPOSITORY est vide"
fi
# find all files (uniq)
for i; do
GUEST_NAME="$i"
if echo $GUEST_NAME | grep -q '/'; then
error "$GUEST_NAME est un nom de système invalide"
fi
DISKS_REPOSITOY="";
CDROM_REPOSITORY="";
HDA=""; HDB=""; CDROM=""; BOOT_CDROM="0";
KERNEL=""; INITRAMFS=""
loadGuestVars $GUEST_NAME
[ "$MODE" = "tgz" -o "$MODE" = "overlay" ] && {
[ -n "$KERNEL" ] && LIST="$LIST $DISKS_REPOSITORY/$KERNEL"
[ -n "$INITRAMFS" ] && LIST="$LIST $DISKS_REPOSITORY/$INITRAMFS"
}
[ -n "$CDROM" -a "$BOOT_CDROM" = 1 ] && \
LIST="$LIST $CDROM_REPOSITORY/$CDROM"
[ -n "$HDA" ] && LIST_DISKS="$LIST_DISKS $DISKS_REPOSITORY/$HDA.gz"
[ -n "$HDB" ] && LIST_DISKS="$LIST_DISKS $DISKS_REPOSITORY/$HDB.gz"
done
#set -x
LIST_DISKS=$(echo $LIST_DISKS | tr -s ' ' '\n' | sort -u | grep -v '^$')
LIST=$(echo $LIST | tr -s ' ' '\n' | sort -u | grep -v '^$' ||:)
LIST="$LIST $LIST_DISKS"
# test dates
LIST_UPLOAD=""
for i in $LIST; do
file=$(basename $i .gz)
set +e
reInfos="$(wget --spider -S $i 2>&1)"
if [ $? -ne 0 ]; then
reDate=0
else
reDate=$(echo "$reInfos" | grep "Last-Modified:" | cut -d ':' -f 2-)
reDate=$(date -d "$reDate" +%s)
fi
set -e
locDate=0
if [ -e $VDN_PATH/files/$file ]; then
locDate=$(stat -c %Y $VDN_PATH/files/$file)
else
warning "$(basename $i) not found"
absent=1
continue
fi
[ $locDate -gt $reDate ] && LIST_UPLOAD="$LIST_UPLOAD $i" ||
echo "$file : is up to date"
done
echo $LIST_UPLOAD
# upload
for i in $LIST_UPLOAD; do
file=$(basename $i .gz)
if [[ $i =~ \.gz ]]; then
if [ $DRY = 1 ]; then
echo "Dry run : upload $i"
else
tmp=$(mktemp)
echo "Compression ($file). Patience... (environ 5 mn)"
gzip -c -n --rsyncable $VDN_PATH/files/$file > $tmp
chmod 644 $tmp
echo "Synchronisation. Patience..."
rsync -S --progress $tmp $SSH_REPOSITORY/$file.gz
rm $tmp
fi
else
if [ $DRY = 1 ]; then
echo "Dry run : upload $i"
else
rsync -S --progress $VDN_PATH/files/$file $SSH_REPOSITORY
fi
fi
done
[ $absent = 1 ] && return 1 || :
}
# Programme principal
GUEST_OWNER=$USER
VDN_PATH=$(readlink -f $(dirname $0)/..); . $VDN_PATH/bin/functions.sh
args "$@"
uploadDisks $SYSTEMS