]> git.treefish.org Git - broadsync.git/blob - broadsync
Force english locale for date command.
[broadsync.git] / broadsync
1 #!/bin/bash
2
3 BROADSYNCDIR=~/.broadsync
4 SYNCLINEFILE="synclines"
5
6 function returnnewer {
7     fctime=$(stat -c %Y "$1")
8     if [ -z "$2" ] || [ $fctime -gt $2 ]; then
9         echo $fctime
10     else
11         echo $2
12     fi
13 }
14
15 if [ ! -d "$BROADSYNCDIR" ]; then
16     mkdir "$BROADSYNCDIR" || exit 1
17     echo "Created directory $BROADSYNCDIR."
18 fi
19
20 if [ ! -f "$BROADSYNCDIR/$SYNCLINEFILE" ]; then
21     touch "$BROADSYNCDIR/$SYNCLINEFILE" || exit 1
22     chmod 600 "$BROADSYNCDIR/$SYNCLINEFILE" || \
23         rm "$BROADSYNCDIR/$SYNCLINEFILE"
24     echo "# line format:
25 # user:password:port:/remote_directory:/local_directory" > \
26     "$BROADSYNCDIR/$SYNCLINEFILE"
27     echo "Created syncline file $BROADSYNCDIR/$SYNCLINEFILE."
28 fi
29
30 if [ $(cat "$BROADSYNCDIR/$SYNCLINEFILE" | grep -v "#" | wc -l) -eq 0 ]; then
31     echo "Your syncline file $BROADSYNCDIR/$SYNCLINEFILE is empty."
32     echo "You have to add at least one syncline to run broadsync!"
33 fi
34
35 tmpdir="$(mktemp -p /tmp -d broadsync-XXXXXXX)" || exit 1
36
37 subnets=$(ip addr | \
38     sed -En 's/.*inet (([0-9]*\.){3}[0-9]*)\/.*/\1/p' | \
39     grep -v '127.0.0.1' | \
40     sed -En 's/(([0-9]*\.){3})[0-9]*/\1/p')
41
42 echo "Subnets to probe: $(echo "$subnets" | tr '\n' ' ')"
43
44 while read syncline; do
45     syncline_user=$(echo "$syncline" | cut -f1 -d":")
46     syncline_pass=$(echo "$syncline" | cut -f2 -d":")
47     syncline_port=$(echo "$syncline" | cut -f3 -d":")
48     syncline_rdir=$(echo "$syncline" | cut -f4 -d":")
49     syncline_ldir=$(eval echo "$(echo "$syncline" | cut -f5 -d":")")
50
51     syncline_hash=$(echo "$syncline_user:$syncline_port:$syncline_rdir" | \
52         md5sum | cut -f1 -d" ")
53
54     echo "Processing syncline $syncline_user:$syncline_port:$syncline_rdir"
55
56     if [ ! -d "$syncline_ldir" ]; then
57         echo "Creating local syncdir $syncline_ldir"
58         mkdir "$syncline_ldir" || exit 1
59     fi
60
61     statdir="$BROADSYNCDIR/$syncline_hash.stat"
62     if [ ! -d "$statdir" ]; then
63         echo "Creating statdir $statdir"
64         mkdir "$statdir" || exit 1
65     fi
66
67     if [ -f "$statdir/newestdate" ]; then
68         newestdate=$(cat "$statdir/newestdate")
69         echo "Using newestdate $(LANG=en_US date +"%T %B %d %Y" -d @$newestdate)"
70     else
71         newestdate=0
72     fi
73
74     for subnet in $subnets; do
75         echo "Scanning for potential hosts in subnet $subnet"
76
77         for i in $(seq 1 254); do
78             (nc -w 5 -z "$subnet$i" "$syncline_port"; echo $? > "$tmpdir/nc-$subnet-$i") &
79         done
80         wait
81
82         for i in $(seq 1 254); do
83             if [ $(cat "$tmpdir/nc-$subnet-$i") -eq 0 ]; then
84                 echo "Trying to sync with potential host $subnet$i"
85
86                 while read logline; do
87                     [ -n "$lastfile" ] && newestdate=$(returnnewer "$syncline_ldir/$lastfile" "$newestdate")
88                     echo $logline
89
90                     file=$(echo "$logline" | sed -En 's/Transferring file .(.*)./\1/p')
91                     if [ -n $file ]; then
92                         lastfile="$file"
93                     else
94                         lastfile=""
95                     fi
96                 done < <(lftp -p "$syncline_port" \
97                     -u "$syncline_user,$syncline_pass" \
98                     -e "mirror -v -N \"$(LANG=en_US date +"%T %B %d %Y" -d @$newestdate)\" \"$syncline_rdir\" \"$syncline_ldir\"" \
99                     "$subnet$i")
100
101                 [ -n "$lastfile" ] && newestdate=$(returnnewer "$syncline_ldir/$lastfile" "$newestdate")                
102             fi
103         done
104     done
105
106     echo $newestdate > "$statdir/newestdate"
107     
108 done < <(cat "$BROADSYNCDIR/$SYNCLINEFILE" | grep -v '#')
109
110 rm -r "$tmpdir"
111
112 exit 0