apt.cron.daily 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. #!/bin/sh
  2. #
  3. #set -e
  4. #
  5. # This file understands the following apt configuration variables:
  6. #
  7. # "APT::Periodic::Update-Package-Lists=1"
  8. # - Do "apt-get update" automatically every n-days (0=disable)
  9. #
  10. # "APT::Periodic::Download-Upgradeable-Packages=0",
  11. # - Do "apt-get upgrade --download-only" every n-days (0=disable)
  12. #
  13. # "APT::Periodic::AutocleanInterval"
  14. # - Do "apt-get autoclean" every n-days (0=disable)
  15. #
  16. # "APT::Archives::MaxAge",
  17. # - Set maximum allowed age of a cache package file. If a cache
  18. # package file is older it is deleted (0=disable)
  19. #
  20. # "APT::Archives::MaxSize",
  21. # - Set maximum size of the cache in MB (0=disable). If the cache
  22. # is bigger, cached package files are deleted until the size
  23. # requirement is met (the biggest packages will be deleted
  24. # first).
  25. #
  26. # "APT::Archives::MinAge"
  27. # - Set minimum age of a package file. If a file is younger it
  28. # will not be deleted (0=disable). Usefull to prevent races
  29. # and to keep backups of the packages for emergency.
  30. #
  31. check_stamp()
  32. {
  33. stamp="$1"
  34. interval="$2"
  35. if [ $interval -eq 0 ]; then
  36. return 1
  37. fi
  38. if [ ! -f $stamp ]; then
  39. return 0
  40. fi
  41. # compare midnight today to midnight the day the stamp was updated
  42. stamp=$(date --date=$(date -r $stamp --iso-8601) +%s)
  43. now=$(date --date=$(date --iso-8601) +%s)
  44. delta=$(($now-$stamp))
  45. # intervall is in days,
  46. interval=$(($interval*60*60*24))
  47. #echo "stampfile: $1"
  48. #echo "interval=$interval, now=$now, stamp=$stamp, delta=$delta"
  49. if [ $delta -ge $interval ]; then
  50. return 0
  51. fi
  52. return 1
  53. }
  54. update_stamp()
  55. {
  56. stamp="$1"
  57. touch $stamp
  58. }
  59. # we check here if autoclean was enough sizewise
  60. check_size_constraints()
  61. {
  62. # min-age in days
  63. MaxAge=0
  64. MinAge=2
  65. MaxSize=0
  66. CacheDir="var/cache/apt"
  67. CacheArchive="archives/"
  68. eval $(apt-config shell MaxAge APT::Archives::MaxAge)
  69. eval $(apt-config shell MinAge APT::Archives::MinAge)
  70. eval $(apt-config shell MaxSize APT::Archives::MaxSize)
  71. eval $(apt-config shell Dir Dir)
  72. eval $(apt-config shell CacheDir Dir::Cache)
  73. eval $(apt-config shell CacheArchive Dir::Cache::archives)
  74. # sanity check
  75. if [ -z "$CacheDir" -o -z "$CacheArchive" ]; then
  76. echo "empty Dir::Cache or Dir::Cache::archives, exiting"
  77. exit
  78. fi
  79. Cache="${Dir%/}/${CacheDir%/}/${CacheArchive%/}/"
  80. # check age
  81. if [ ! $MaxAge -eq 0 ] && [ ! $MinAge -eq 0 ]; then
  82. find $Cache -name "*.deb" -mtime +$MaxAge -and -not -mtime -$MinAge -print0 | xargs -r -0 rm -f
  83. elif [ ! $MaxAge -eq 0 ]; then
  84. find $Cache -name "*.deb" -mtime +$MaxAge -print0 | xargs -r -0 rm -f
  85. fi
  86. # check size
  87. if [ ! $MaxSize -eq 0 ]; then
  88. # maxSize is in MB
  89. MaxSize=$(($MaxSize*1024))
  90. #get current time
  91. now=$(date --date=$(date --iso-8601) +%s)
  92. MinAge=$(($MinAge*24*60*60))
  93. # reverse-sort by mtime
  94. for file in $(ls -rt $Cache/*.deb 2>/dev/null); do
  95. du=$(du -s $Cache)
  96. size=${du%%/*}
  97. # check if the cache is small enough
  98. if [ $size -lt $MaxSize ]; then
  99. break
  100. fi
  101. # check for MinAge of the file
  102. if [ ! $MinAge -eq 0 ]; then
  103. mtime=$(date --date=$(date -r $file --iso-8601) +%s)
  104. delta=$(($now-$mtime))
  105. #echo "$file ($delta), $MinAge"
  106. if [ $delta -le $MinAge ]; then
  107. #echo "Skiping $file (delta=$delta)"
  108. break
  109. fi
  110. fi
  111. # delete oldest file
  112. rm -f $file
  113. done
  114. fi
  115. }
  116. UpdateInterval=0
  117. DownloadUpgradeableInterval=0
  118. eval $(apt-config shell UpdateInterval APT::Periodic::Update-Package-Lists DownloadUpgradeableInterval APT::Periodic::Download-Upgradeable-Packages)
  119. AutocleanInterval=$DownloadUpgradeableInterval
  120. eval $(apt-config shell AutocleanInterval APT::Periodic::Autoclean)
  121. # laptop check, on_ac_power returns:
  122. # 0 (true) System is on mains power
  123. # 1 (false) System is not on mains power
  124. # 255 (false) Power status could not be determined
  125. # Desktop systems always return 255 it seems
  126. if which on_ac_power >/dev/null; then
  127. on_ac_power
  128. if [ $? -eq 1 ]; then
  129. exit 0
  130. fi
  131. fi
  132. UPDATE_STAMP=/var/lib/apt/periodic/update-stamp
  133. if check_stamp $UPDATE_STAMP $UpdateInterval; then
  134. if apt-get -qq update 2>/dev/null; then
  135. if which dbus-send >/dev/null; then
  136. dbus-send --system / app.apt.dbus.updated boolean:true
  137. fi
  138. update_stamp $UPDATE_STAMP
  139. fi
  140. fi
  141. DOWNLOAD_UPGRADEABLE_STAMP=/var/lib/apt/periodic/download-upgradeable-stamp
  142. if check_stamp $DOWNLOAD_UPGRADEABLE_STAMP $DownloadUpgradeableInterval; then
  143. apt-get -qq -d dist-upgrade 2>/dev/null
  144. update_stamp $DOWNLOAD_UPGRADEABLE_STAMP
  145. fi
  146. AUTOCLEAN_STAMP=/var/lib/apt/periodic/autoclean-stamp
  147. if check_stamp $AUTOCLEAN_STAMP $AutocleanInterval; then
  148. apt-get -qq autoclean
  149. update_stamp $AUTOCLEAN_STAMP
  150. fi
  151. # check cache size
  152. check_size_constraints