apt.cron.daily 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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. # sleep for a random interval of time (default 30min)
  129. # (some code taken from cron-apt, thanks)
  130. random_sleep()
  131. {
  132. RandomSleep=1800
  133. eval $(apt-config shell RandomSleep APT::Periodic::RandomSleep)
  134. if [ $RandomSleep -eq 0 ]; then
  135. return
  136. fi
  137. if [ -z "$RANDOM" ] ; then
  138. # A fix for shells that do not have this bash feature.
  139. RANDOM=$(dd if=/dev/urandom count=1 2> /dev/null | cksum | cut -c"1-5")
  140. fi
  141. TIME=$(($RANDOM % $RandomSleep))
  142. sleep $TIME
  143. }
  144. # main
  145. if ! which apt-config >/dev/null; then
  146. exit 0
  147. fi
  148. UpdateInterval=0
  149. DownloadUpgradeableInterval=0
  150. eval $(apt-config shell UpdateInterval APT::Periodic::Update-Package-Lists DownloadUpgradeableInterval APT::Periodic::Download-Upgradeable-Packages)
  151. AutocleanInterval=$DownloadUpgradeableInterval
  152. eval $(apt-config shell AutocleanInterval APT::Periodic::AutocleanInterval)
  153. UnattendedUpgradeInterval=0
  154. eval $(apt-config shell UnattendedUpgradeInterval APT::Periodic::Unattended-Upgrade)
  155. # check if we actually have to do anything
  156. if [ $UpdateInterval -eq 0 ] &&
  157. [ $DownloadUpgradeableInterval -eq 0 ] &&
  158. [ $UnattendedUpgradeInterval -eq 0 ] &&
  159. [ $AutocleanInterval -eq 0 ]; then
  160. exit 0
  161. fi
  162. # laptop check, on_ac_power returns:
  163. # 0 (true) System is on mains power
  164. # 1 (false) System is not on mains power
  165. # 255 (false) Power status could not be determined
  166. # Desktop systems always return 255 it seems
  167. if which on_ac_power >/dev/null; then
  168. on_ac_power
  169. if [ $? -eq 1 ]; then
  170. exit 0
  171. fi
  172. fi
  173. # sleep random amount of time to avoid hitting the
  174. # mirrors at the same time
  175. random_sleep
  176. # check if we can access the cache
  177. if ! apt-get check -q -q 2>/dev/null; then
  178. # wait random amount of time before retrying
  179. random_sleep
  180. # check again
  181. if ! apt-get check -q -q 2>/dev/null; then
  182. echo "$0: could not lock the APT cache while performing daily cron job. "
  183. echo "Is another package manager working?"
  184. exit 1
  185. fi
  186. fi
  187. UPDATE_STAMP=/var/lib/apt/periodic/update-stamp
  188. if check_stamp $UPDATE_STAMP $UpdateInterval; then
  189. if apt-get -qq update 2>/dev/null; then
  190. if which dbus-send >/dev/null && pidof dbus-daemon >/dev/null; then
  191. dbus-send --system / app.apt.dbus.updated boolean:true
  192. fi
  193. update_stamp $UPDATE_STAMP
  194. fi
  195. fi
  196. DOWNLOAD_UPGRADEABLE_STAMP=/var/lib/apt/periodic/download-upgradeable-stamp
  197. if check_stamp $DOWNLOAD_UPGRADEABLE_STAMP $DownloadUpgradeableInterval; then
  198. apt-get -qq -d dist-upgrade 2>/dev/null
  199. update_stamp $DOWNLOAD_UPGRADEABLE_STAMP
  200. fi
  201. UPGRADE_STAMP=/var/lib/apt/periodic/upgrade-stamp
  202. if check_stamp $UPGRADE_STAMP $UnattendedUpgradeInterval; then
  203. unattended-upgrade
  204. update_stamp $UPGRADE_STAMP
  205. fi
  206. AUTOCLEAN_STAMP=/var/lib/apt/periodic/autoclean-stamp
  207. if check_stamp $AUTOCLEAN_STAMP $AutocleanInterval; then
  208. apt-get -qq autoclean
  209. update_stamp $AUTOCLEAN_STAMP
  210. fi
  211. # check cache size
  212. check_size_constraints