1 | #!/bin/bash |
---|
2 | # Genera los ficheros .torrent de las imágenes almacenadas en el repositorio. |
---|
3 | #Version 0.3 Ejecución desde cron cada minuto, |
---|
4 | ## echo "* * * * * root /opt/opengnsys/bin/torrent-creator" > /etc/cron.d/torrentcreator |
---|
5 | |
---|
6 | ## ver moficifcacione en linea 41 - 46 |
---|
7 | |
---|
8 | # Comprobar si el proceso ya está en ejecución. |
---|
9 | PROG=$(basename $0) |
---|
10 | [ "$(pgrep "$PROG")" != "$$" ] && exit |
---|
11 | |
---|
12 | # Variables. |
---|
13 | OPENGNSYS=${OPENGNSYS:-"/opt/opengnsys"} |
---|
14 | PATH=$PATH:$OPENGNSYS/bin |
---|
15 | OGIMG="$OPENGNSYS/images" |
---|
16 | REPOCFG="$OPENGNSYS/etc/ogAdmRepo.cfg" |
---|
17 | LOGFILE="$OPENGNSYS/log/$PROG.log" |
---|
18 | # Error si no está bien configurado el repositorio de imágenes. |
---|
19 | [ -d $OGIMG -a -f $REPOCFG ] || exit 1 |
---|
20 | source $REPOCFG |
---|
21 | TRACKERURL="http://$IPlocal:6969/announce" |
---|
22 | |
---|
23 | # Directorio de imágenes. |
---|
24 | pushd $OGIMG >/dev/null |
---|
25 | |
---|
26 | # Procesar ficheros de imágenes. |
---|
27 | trap 'echo "`date` : Proceso interrumpido" >> $LOGFILE; exit ' 1 2 3 6 9 15 |
---|
28 | for IMG in *.{img,pgz}; do |
---|
29 | # Saltar al siguiente si la imagen está bloqueada o si no existe el fichero. |
---|
30 | LOCKFILE="$IMG.lock" |
---|
31 | if [ -f "$LOCKFILE" -o ! -f "$IMG" ]; then |
---|
32 | continue |
---|
33 | fi |
---|
34 | # Comprobar si ya existe el fichero Torrent para esa imagen. |
---|
35 | TORRENT="$IMG.torrent" |
---|
36 | SUMFILE="$IMG.sum" |
---|
37 | if [ -f "$TORRENT" ]; then |
---|
38 | FILESIZE="$(ls -l $IMG | awk '{print $5}')" |
---|
39 | read -e TORRFILE TORRSIZE <<<"$(ctorrent -x $TORRENT | awk '$1~/<1>/ {print $2,$3}')" |
---|
40 | [ "$(basename $IMG)" = "$TORRFILE" -a "[$FILESIZE]" = "$TORRSIZE" ] && continue |
---|
41 | fi |
---|
42 | # Bloquear imagen, crear ficheros Torrent y Checksum y desbloquear imagen. |
---|
43 | echo "`date` : Inicio creación de fichero $TORRENT" >> $LOGFILE |
---|
44 | touch "$LOCKFILE" |
---|
45 | trap "rm -f $LOCKFILE" 1 2 3 6 9 |
---|
46 | rm -f "$TORRENT" "$SUMFILE" |
---|
47 | DATASUM=`md5sum "$IMG" | cut -f1 -d" "` |
---|
48 | echo $DATASUM > "$SUMFILE" |
---|
49 | nice -8 ctorrent -t "$IMG" -u $TRACKERURL -s "$TORRENT" -c $DATASUM |
---|
50 | rm -f "$LOCKFILE" |
---|
51 | if [ -f "$TORRENT" ]; then |
---|
52 | echo "`date` : Fin creación de fichero $TORRENT" >> $LOGFILE |
---|
53 | else |
---|
54 | echo "`date` : ERROR en creación de fichero $TORRENT" >> $LOGFILE |
---|
55 | fi |
---|
56 | done |
---|
57 | |
---|
58 | popd >/dev/null |
---|
59 | |
---|