tar-them-up 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. #!/bin/sh
  2. # Tar up dumped consensuses, statuses, descriptors etc from per-month folders
  3. # into per-month tarballs.
  4. # Copyright (c) 2006, 2007, 2008 Peter Palfrader
  5. #
  6. # Permission is hereby granted, free of charge, to any person obtaining a copy
  7. # of this software and associated documentation files (the "Software"), to deal
  8. # in the Software without restriction, including without limitation the rights
  9. # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. # copies of the Software, and to permit persons to whom the Software is
  11. # furnished to do so, subject to the following conditions:
  12. #
  13. # The above copyright notice and this permission notice shall be included in
  14. # all copies or substantial portions of the Software.
  15. #
  16. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19. # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  22. # SOFTWARE.
  23. set -e
  24. set -x
  25. set -u
  26. usage() {
  27. echo "Usage: $0 <year> <month>" >&2
  28. echo " $0 last (does last month)" >&2
  29. exit 1
  30. }
  31. if [ -z "${1:-}" ]; then
  32. usage
  33. fi
  34. if [ "$1" = "last" ]; then
  35. year=`date --date="last month" +'%Y'`
  36. month=`date --date="last month" +'%m'`
  37. elif [ -z "${2:-}" ]; then
  38. usage
  39. else
  40. year="$1"
  41. month="$2"
  42. fi
  43. if [ "$year" -lt 2000 ] || [ "$year" -gt 2020 ] ||
  44. [ "$month" -lt 1 ] || [ "$month" -gt 12 ] ||
  45. [ "`echo -n $month | wc -c`" != 2 ]; then
  46. usage
  47. fi
  48. this_year=`date --utc +'%Y'`
  49. this_month=`date --utc +'%m'`
  50. if [ "`date -d $this_year-$this_month-01 +%s`" -le "`date -d $year-$month-01 +%s`" ]; then
  51. echo "Date in the future or current month?" >&2
  52. exit 1
  53. fi
  54. for file in \
  55. "extra-infos-$year-$month.tar.bz2" \
  56. "server-descriptors-$year-$month.tar.bz2" \
  57. "consensuses-$year-$month.tar.bz2" \
  58. "statuses-$year-$month.tar.bz2" \
  59. ; do
  60. if [ -e "$file" ]; then
  61. echo "$file already exists" >&2
  62. exit 1
  63. fi
  64. done
  65. for dir in \
  66. "extra-infos-$year-$month" \
  67. "server-descriptors-$year-$month" \
  68. "consensus/$year/$month" \
  69. "status/$year/$month" \
  70. ; do
  71. if ! [ -d "$dir" ]; then
  72. echo "$dir not found" >&2
  73. exit 1
  74. fi
  75. done
  76. for dir in \
  77. "consensuses-$year-$month" \
  78. "statuses-$year-$month" \
  79. ; do
  80. if [ -e "$dir" ]; then
  81. echo "$dir already exists" >&2
  82. exit 1
  83. fi
  84. done
  85. for kind in consensus status; do
  86. mv "$kind"/$year/$month "$kind"es-$year-$month
  87. find "$kind"es-$year-$month -type f -name '*.bz2' -print0 | xargs -0 bunzip2 -v
  88. tar cjvf "$kind"es-$year-$month.tar.bz2 "$kind"es-$year-$month
  89. rm -rf "$kind"es-$year-$month
  90. done
  91. for kind in extra-infos server-descriptors; do
  92. tar cjvf "$kind"-$year-$month.tar.bz2 "$kind"-$year-$month
  93. rm -rf "$kind"-$year-$month
  94. done
  95. [ -d Archive ] || mkdir Archive
  96. for kind in consensus status; do
  97. t="$kind"es-$year-$month.tar.bz2
  98. ! [ -e Archive/"$t" ] && mv "$t" Archive/"$t"
  99. done
  100. for kind in extra-infos server-descriptors; do
  101. t="$kind"-$year-$month.tar.bz2
  102. ! [ -e Archive/"$t" ] && mv "$t" Archive/"$t"
  103. done