apt.cron.daily 5.4 KB

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