apt.cron.daily 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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 -ctime +$MaxAge \) -and -not \( -mtime -$MinAge -or -ctime -$MinAge \) -print0 | xargs -r -0 rm -f
  83. elif [ ! $MaxAge -eq 0 ]; then
  84. find $Cache -name "*.deb" -ctime +$MaxAge -and -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. # check both ctime and mtime
  104. mtime=$(stat -c %Y $file)
  105. ctime=$(stat -c %Z $file)
  106. if [ $mtime -gt $ctime ]; then
  107. delta=$(($now-$mtime))
  108. else
  109. delta=$(($now-$ctime))
  110. fi
  111. #echo "$file ($delta), $MinAge"
  112. if [ $delta -le $MinAge ]; then
  113. #echo "Skiping $file (delta=$delta)"
  114. break
  115. fi
  116. fi
  117. # delete oldest file
  118. rm -f $file
  119. done
  120. fi
  121. }
  122. UpdateInterval=0
  123. DownloadUpgradeableInterval=0
  124. eval $(apt-config shell UpdateInterval APT::Periodic::Update-Package-Lists DownloadUpgradeableInterval APT::Periodic::Download-Upgradeable-Packages)
  125. AutocleanInterval=$DownloadUpgradeableInterval
  126. eval $(apt-config shell AutocleanInterval APT::Periodic::Autoclean)
  127. # laptop check, on_ac_power returns:
  128. # 0 (true) System is on mains power
  129. # 1 (false) System is not on mains power
  130. # 255 (false) Power status could not be determined
  131. # Desktop systems always return 255 it seems
  132. if which on_ac_power >/dev/null; then
  133. on_ac_power
  134. if [ $? -eq 1 ]; then
  135. exit 0
  136. fi
  137. fi
  138. UPDATE_STAMP=/var/lib/apt/periodic/update-stamp
  139. if check_stamp $UPDATE_STAMP $UpdateInterval; then
  140. if apt-get -qq update 2>/dev/null; then
  141. if which dbus-send >/dev/null; then
  142. dbus-send --system / app.apt.dbus.updated boolean:true
  143. fi
  144. update_stamp $UPDATE_STAMP
  145. fi
  146. fi
  147. DOWNLOAD_UPGRADEABLE_STAMP=/var/lib/apt/periodic/download-upgradeable-stamp
  148. if check_stamp $DOWNLOAD_UPGRADEABLE_STAMP $DownloadUpgradeableInterval; then
  149. apt-get -qq -d dist-upgrade 2>/dev/null
  150. update_stamp $DOWNLOAD_UPGRADEABLE_STAMP
  151. fi
  152. AUTOCLEAN_STAMP=/var/lib/apt/periodic/autoclean-stamp
  153. if check_stamp $AUTOCLEAN_STAMP $AutocleanInterval; then
  154. apt-get -qq autoclean
  155. update_stamp $AUTOCLEAN_STAMP
  156. fi
  157. # check cache size
  158. check_size_constraints