aboutsummaryrefslogtreecommitdiff
path: root/contrib/directory-archive/tar-them-up
blob: 2775ca9ee9f09ab8455d5f716b2ef75c9230cedd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
#!/bin/sh

# Tar up dumped consensuses, statuses, descriptors etc from per-month folders
# into per-month tarballs.

# Copyright (c) 2006, 2007, 2008 Peter Palfrader
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.

set -e
set -x
set -u

usage() {
	echo "Usage: $0 <year> <month>" >&2
	echo "       $0 last            (does last month)" >&2
	exit 1
}

if [ -z "${1:-}" ]; then
	usage
fi

if [ "$1" = "last" ]; then
	year=`date --date="last month" +'%Y'`
	month=`date --date="last month" +'%m'`
elif [ -z "${2:-}" ]; then
	usage
else
	year="$1"
	month="$2"
fi

if [ "$year" -lt 2000 ] || [ "$year" -gt 2020 ] ||
   [ "$month" -lt 1 ] || [ "$month" -gt 12 ] ||
   [ "`echo -n $month | wc -c`" != 2 ]; then
	usage
fi


this_year=`date --utc +'%Y'`
this_month=`date --utc +'%m'`

if [ "`date -d $this_year-$this_month-01 +%s`" -le "`date -d $year-$month-01 +%s`" ]; then
	echo "Date in the future or current month?" >&2
	exit 1
fi





for file in \
	"extra-infos-$year-$month.tar.bz2"		\
	"server-descriptors-$year-$month.tar.bz2"	\
	"consensuses-$year-$month.tar.bz2"		\
	"statuses-$year-$month.tar.bz2"			\
	; do
	if [ -e "$file" ]; then
		echo "$file already exists" >&2
		exit 1
	fi
done

for dir in \
	"extra-infos-$year-$month"		\
	"server-descriptors-$year-$month"	\
	"consensus/$year/$month"		\
	"status/$year/$month"			\
	; do
	if ! [ -d "$dir" ]; then
		echo "$dir not found" >&2
		exit 1
	fi
done

for dir in \
	"consensuses-$year-$month"		\
	"statuses-$year-$month"			\
	; do
	if [ -e "$dir" ]; then
		echo "$dir already exists" >&2
		exit 1
	fi
done

for kind in consensus status; do
	mv "$kind"/$year/$month "$kind"es-$year-$month
	find "$kind"es-$year-$month -type f -name '*.bz2' -print0 | xargs -0 bunzip2 -v
	tar cjvf "$kind"es-$year-$month.tar.bz2 "$kind"es-$year-$month
	rm -rf "$kind"es-$year-$month
done

for kind in extra-infos server-descriptors; do
	tar cjvf "$kind"-$year-$month.tar.bz2 "$kind"-$year-$month
	rm -rf "$kind"-$year-$month
done



[ -d Archive ] || mkdir Archive

for kind in consensus status; do
	t="$kind"es-$year-$month.tar.bz2
	! [ -e Archive/"$t" ] && mv "$t" Archive/"$t"
done

for kind in extra-infos server-descriptors; do
	t="$kind"-$year-$month.tar.bz2
	! [ -e Archive/"$t" ] && mv "$t" Archive/"$t"
done