1 | #!/bin/bash
|
---|
2 |
|
---|
3 | year=`date +%Y --date="-1day"`
|
---|
4 | month=`date +%m --date="-1day"`
|
---|
5 |
|
---|
6 | today=`date +%F`
|
---|
7 | logfile=/home/`whoami`/DataCheck/log/ZipRaw$today.log
|
---|
8 |
|
---|
9 | echo "" >> $logfile 2>&1
|
---|
10 | echo "" >> $logfile 2>&1
|
---|
11 | echo "" >> $logfile 2>&1
|
---|
12 | rawdir=/loc_data/raw/$year/$month
|
---|
13 | echo `date`": processing files in "$rawdir >> $logfile
|
---|
14 |
|
---|
15 | #find all directories in $rawdir
|
---|
16 | dirs=`find $rawdir -type d | sort`
|
---|
17 |
|
---|
18 | echo `date`": create missing directories in /loc_data/zipraw/$year/$month" >> $logfile
|
---|
19 | for dir in $dirs
|
---|
20 | do
|
---|
21 | zipdir=`echo $dir | sed -e 's/raw/zipraw/'`
|
---|
22 | # check if output directory for zip already exists
|
---|
23 | if [ -d $zipdir ]
|
---|
24 | then
|
---|
25 | continue
|
---|
26 | fi
|
---|
27 | # create output directory for zip
|
---|
28 | mkdir -pv $zipdir >> $logfile
|
---|
29 | done
|
---|
30 |
|
---|
31 | # find all fits-files starting with the oldest file
|
---|
32 | echo `date`": finding files to be zipped in $rawdir..." >> $logfile
|
---|
33 | fitsfiles=`find $rawdir -type f -name '*fits'| sort `
|
---|
34 |
|
---|
35 | # loop to zip files
|
---|
36 | echo `date`": zipping files in $rawdir..." >> $logfile
|
---|
37 | for file in $fitsfiles
|
---|
38 | do
|
---|
39 | # filename for temporary and final zipfile
|
---|
40 | zipfile=`echo $file | sed -e 's/raw/zipraw/' -e 's/fits/fits.gz/'`
|
---|
41 | zipfiletmp=`echo $file | sed -e 's/raw/zipraw/' -e 's/fits/fits.tmp.gz/'`
|
---|
42 | # check if zipped file already exists
|
---|
43 | if [ -e $zipfile ]
|
---|
44 | then
|
---|
45 | continue
|
---|
46 | fi
|
---|
47 |
|
---|
48 | # check if files was accessed in the last 30 minutes
|
---|
49 | isnew=`find $file -amin -30`
|
---|
50 | if [ "$isnew" != "" ]
|
---|
51 | then
|
---|
52 | echo $file" is not older than 30 min => continue" >> $logfile
|
---|
53 | continue
|
---|
54 | fi
|
---|
55 |
|
---|
56 | # check if file is already finished
|
---|
57 | # original file on daq (if data was taken on daq
|
---|
58 | origfile=`echo $file | sed -e 's/loc_data/daq/'`
|
---|
59 | if [ -e $origfile ]
|
---|
60 | then
|
---|
61 | # get time of last modification as seconds since Epoch for both files
|
---|
62 | timeorig=`stat -c %Y $origfile`
|
---|
63 | timecopy=`stat -c %Y $file`
|
---|
64 | # compare times
|
---|
65 | if ! [ $timeorig -eq $timecopy ]
|
---|
66 | then
|
---|
67 | # if times are not the same, the file is still open => no zip
|
---|
68 | echo `date`": file "$file" not yet closed => continue" >> $logfile
|
---|
69 | continue
|
---|
70 | fi
|
---|
71 | else
|
---|
72 | # if the origfile doesn't exist, the data was probably written not on daq but on data
|
---|
73 | echo `date`": file "$file" was probably taken on data and not daq " >> $logfile
|
---|
74 | fi
|
---|
75 |
|
---|
76 | echo `date`": zipping "$file" to "$zipfile" ..." >> $logfile
|
---|
77 | # zip file to stdout and pipe it to outputfile
|
---|
78 | if pigz -1 -c -f $file > $zipfiletmp;
|
---|
79 | then
|
---|
80 | # if successful, move temporary to final zipfile
|
---|
81 | mv -v $zipfiletmp $zipfile >> $logfile
|
---|
82 | else
|
---|
83 | # if not successful, remove temporary zipfile
|
---|
84 | rm -v $zipfiletmp >> $logfile
|
---|
85 | fi
|
---|
86 | done
|
---|
87 |
|
---|
88 |
|
---|