apt.cron.daily 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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. MaxSize=0
  65. CacheDir="var/cache/apt"
  66. CacheArchive="archives/"
  67. eval $(apt-config shell MaxAge APT::Archives::MaxAge)
  68. eval $(apt-config shell MaxSize APT::Archives::MaxSize)
  69. eval $(apt-config shell CacheDir Dir::Cache)
  70. eval $(apt-config shell CacheArchive Dir::Cache::archives)
  71. # sanity check
  72. if [ -z "$CacheDir" -o -z "$CacheArchive" ]; then
  73. echo "empty Dir::Cache or Dir::Cache::archives, exiting"
  74. exit
  75. fi
  76. Cache="/"$CacheDir$CacheArchive
  77. # check age
  78. if [ ! $MaxAge -eq 0 ]; then
  79. find $Cache -name "*.deb" -mtime +$MaxAge -print0 | xargs -r -0 rm -f
  80. fi
  81. # check size
  82. if [ ! $MaxSize -eq 0 ]; then
  83. # reverse-sort by mtime
  84. for file in $(ls -rt $Cache/*.deb); do
  85. du=$(du -s $Cache)
  86. size=${du%%/*}
  87. # check if the cache is small enough
  88. if [ $size -lt $MaxSize ]; then
  89. break
  90. fi
  91. # delete oldest file
  92. rm -f $file
  93. done
  94. fi
  95. }
  96. check_size_constraints
  97. # we check here if autoclean was enough sizewise
  98. check_size_constraints()
  99. {
  100. # min-age in days
  101. MaxAge=0
  102. MinAge=2
  103. MaxSize=0
  104. CacheDir="var/cache/apt"
  105. CacheArchive="archives/"
  106. eval $(apt-config shell MaxAge APT::Archives::MaxAge)
  107. eval $(apt-config shell MinAge APT::Archives::MinAge)
  108. eval $(apt-config shell MaxSize APT::Archives::MaxSize)
  109. eval $(apt-config shell Dir Dir)
  110. eval $(apt-config shell CacheDir Dir::Cache)
  111. eval $(apt-config shell CacheArchive Dir::Cache::archives)
  112. # sanity check
  113. if [ -z "$CacheDir" -o -z "$CacheArchive" ]; then
  114. echo "empty Dir::Cache or Dir::Cache::archives, exiting"
  115. exit
  116. fi
  117. Cache="${Dir%/}/${CacheDir%/}/${CacheArchive%/}/"
  118. # check age
  119. if [ ! $MaxAge -eq 0 ] && [ ! $MinAge -eq 0 ]; then
  120. find $Cache -name "*.deb" -mtime +$MaxAge -and -not -mtime -$MinAge -print0 | xargs -r -0 rm -f
  121. elif [ ! $MaxAge -eq 0 ]; then
  122. find $Cache -name "*.deb" -mtime +$MaxAge -print0 | xargs -r -0 rm -f
  123. fi
  124. # check size
  125. if [ ! $MaxSize -eq 0 ]; then
  126. # maxSize is in MB
  127. MaxSize=$(($MaxSize*1024))
  128. #get current time
  129. now=$(date --date=$(date --iso-8601) +%s)
  130. MinAge=$(($MinAge*24*60*60))
  131. # reverse-sort by mtime
  132. for file in $(ls -rt $Cache/*.deb); do
  133. du=$(du -s $Cache)
  134. size=${du%%/*}
  135. # check if the cache is small enough
  136. if [ $size -lt $MaxSize ]; then
  137. break
  138. fi
  139. # check for MinAge of the file
  140. if [ ! $MinAge -eq 0 ]; then
  141. mtime=$(date --date=$(date -r $file --iso-8601) +%s)
  142. delta=$(($now-$mtime))
  143. #echo "$file ($delta), $MinAge"
  144. if [ $delta -le $MinAge ]; then
  145. #echo "Skiping $file (delta=$delta)"
  146. continue
  147. fi
  148. fi
  149. # delete oldest file
  150. rm -f $file
  151. done
  152. fi
  153. }
  154. UpdateInterval=0
  155. DownloadUpgradeableInterval=0
  156. eval $(apt-config shell UpdateInterval APT::Periodic::Update-Package-Lists DownloadUpgradeableInterval APT::Periodic::Download-Upgradeable-Packages)
  157. AutocleanInterval=$DownloadUpgradeableInterval
  158. eval $(apt-config shell AutocleanInterval APT::Periodic::Autoclean)
  159. # laptop check, on_ac_power returns:
  160. # 0 (true) System is on mains power
  161. # 1 (false) System is not on mains power
  162. # 255 (false) Power status could not be determined
  163. # Desktop systems always return 255 it seems
  164. if which on_ac_power >/dev/null; then
  165. on_ac_power
  166. if [ $? -eq 1 ]; then
  167. exit 0
  168. fi
  169. fi
  170. UPDATE_STAMP=/var/lib/apt/periodic/update-stamp
  171. if check_stamp $UPDATE_STAMP $UpdateInterval; then
  172. if apt-get -qq update 2>/dev/null; then
  173. if which dbus-send >/dev/null; then
  174. dbus-send --system / app.apt.dbus.updated boolean:true
  175. fi
  176. update_stamp $UPDATE_STAMP
  177. fi
  178. fi
  179. DOWNLOAD_UPGRADEABLE_STAMP=/var/lib/apt/periodic/download-upgradeable-stamp
  180. if check_stamp $DOWNLOAD_UPGRADEABLE_STAMP $DownloadUpgradeableInterval; then
  181. apt-get -qq -d dist-upgrade 2>/dev/null
  182. update_stamp $DOWNLOAD_UPGRADEABLE_STAMP
  183. fi
  184. AUTOCLEAN_STAMP=/var/lib/apt/periodic/autoclean-stamp
  185. if check_stamp $AUTOCLEAN_STAMP $AutocleanInterval; then
  186. apt-get -qq autoclean
  187. check_size_contrains
  188. update_stamp $AUTOCLEAN_STAMP
  189. fi
  190. # check cache size
  191. check_size_constraints