archive_per_month.sh 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. #!/bin/sh
  2. # $Id: archive_per_month.sh,v 1.7 2015/11/05 17:31:33 gilles Exp gilles $
  3. # Translate Jan to 01, Feb to 02 etc.
  4. month_number() {
  5. test X"$1" = X"Jan" && echo 01 && return
  6. test X"$1" = X"Feb" && echo 02 && return
  7. test X"$1" = X"Mar" && echo 03 && return
  8. test X"$1" = X"Apr" && echo 04 && return
  9. test X"$1" = X"May" && echo 05 && return
  10. test X"$1" = X"Jun" && echo 06 && return
  11. test X"$1" = X"Jul" && echo 07 && return
  12. test X"$1" = X"Aug" && echo 08 && return
  13. test X"$1" = X"Sep" && echo 09 && return
  14. test X"$1" = X"Oct" && echo 10 && return
  15. test X"$1" = X"Nov" && echo 11 && return
  16. test X"$1" = X"Dec" && echo 12 && return
  17. echo 00
  18. }
  19. # Calculates the last day of month
  20. # Expect GNU date command
  21. last_day_of_year_month() {
  22. year_ld=$1
  23. month_ld=$2
  24. next_month_day_01=`date -d "$year_ld-$month_ld-15 next month" +%Y-%m-01`
  25. #echo $next_month_day_1
  26. # last day is 1st day of next month minus 1 day
  27. date -d "$next_month_day_01 -1 day" +%d
  28. }
  29. # Replace ... with standard options like --host1 --user1 --password1 --host2 --user2 --password2
  30. # Remove the echo at the beginning
  31. archive_year_month() {
  32. year=$1
  33. month=$2
  34. month_n=`month_number $month`
  35. last_day=`last_day_of_year_month $year $month_n`
  36. echo imapsync ... \
  37. --search "SENTSINCE 1-$month-$year SENTBEFORE $last_day-$month-$year" \
  38. --regextrans2 "s{.*}{Archive/$year/$month_n}"
  39. }
  40. #
  41. for year_archive in 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013; do
  42. for month_archive in "Jan" "Feb" "Mar" "Apr" "May" "Jun" "Jul" "Aug" "Sep" "Oct" "Nov" "Dec"; do
  43. archive_year_month $year_archive $month_archive
  44. done
  45. done
  46. # End of $Id: archive_per_month.sh,v 1.7 2015/11/05 17:31:33 gilles Exp gilles $