#!/bin/bash # Genera los ficheros .torrent de las imágenes almacenadas en el repositorio. #Version 0.3 Ejecución desde cron cada minuto, ## echo "* * * * * root /opt/opengnsys/bin/torrent-creator" > /etc/cron.d/torrentcreator # Comprobar si el proceso ya está en ejecución. PROG=$(basename $0) [ "$(pgrep "$PROG")" != "$$" ] && exit # Variables. OPENGNSYS=${OPENGNSYS:-"/opt/opengnsys"} PATH=$PATH:$OPENGNSYS/bin OGIMG="$OPENGNSYS/images" REPOCFG="$OPENGNSYS/etc/ogAdmRepo.cfg" LOGFILE="$OPENGNSYS/log/$PROG.log" # Error si no está bien configurado el repositorio de imágenes. [ -d $OGIMG -a -f $REPOCFG ] || exit 1 source $REPOCFG TRACKERURL="http://$IPlocal:6969/announce" # Directorio de imágenes. pushd $OGIMG >/dev/null # Procesar ficheros de imágenes. trap 'echo "`date` : Proceso interrumpido" >> $LOGFILE; exit ' 1 2 3 6 9 15 for IMG in *.{img,pgz}; do # Saltar al siguiente si la imagen está bloqueada o si no existe el fichero. if [ -f "$IMG.lock" -o ! -f "$IMG" ]; then continue fi # Comprobar si ya existe el fichero Torrent para esa imagen. TORRENT="$IMG.torrent" if [ -f "$TORRENT" ]; then FILESIZE="$(ls -l $IMG | awk '{print $5}')" read -e TORRFILE TORRSIZE <<<"$(ctorrent -x $TORRENT | awk '$1~/<1>/ {print $2,$3}')" [ "$(basename $IMG)" = "$TORRFILE" -a "[$FILESIZE]" = "$TORRSIZE" ] && continue fi # Crear fichero Torrent. echo "`date` : Inicio creación de fichero $TORRENT" >> $LOGFILE rm -f "$TORRENT" ##### ADV v. 1.0 23/02/2011 rm -f $IMG.sum DATASUM=`md5sum "$IMG" | cut -f1 -d" "` echo $DATASUM > $IMG.sum nice -8 ctorrent -t "$IMG" -u $TRACKERURL -s "$TORRENT" -c $DATASUM #### ADV v. 1.0 23/02/2011 nice -8 ctorrent -t "$IMG" -u $TRACKERURL -s "$TORRENT" if [ -f "$TORRENT" ]; then echo "`date` : Fin creación de fichero $TORRENT" >> $LOGFILE else echo "`date` : ERROR en creación de fichero $TORRENT" >> $LOGFILE fi done popd >/dev/null