apt.cron.daily 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  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_file="$stamp"
  49. stamp=$(date --date=$(date -r $stamp_file --iso-8601) +%s 2>/dev/null)
  50. if [ "$?" != "0" ]; then
  51. # Due to some timezones returning 'invalid date' for midnight on
  52. # certain dates (eg America/Sao_Paulo), if date returns with error
  53. # remove the stamp file and return 0. See coreutils bug:
  54. # http://lists.gnu.org/archive/html/bug-coreutils/2007-09/msg00176.html
  55. rm -f "$stamp_file"
  56. return 0
  57. fi
  58. now=$(date --date=$(date --iso-8601) +%s 2>/dev/null)
  59. if [ "$?" != "0" ]; then
  60. # As above, due to some timezones returning 'invalid date' for midnight
  61. # on certain dates (eg America/Sao_Paulo), if date returns with error
  62. # return 0.
  63. return 0
  64. fi
  65. delta=$(($now-$stamp))
  66. # intervall is in days,
  67. interval=$(($interval*60*60*24))
  68. #echo "stampfile: $1"
  69. #echo "interval=$interval, now=$now, stamp=$stamp, delta=$delta"
  70. if [ $delta -ge $interval ]; then
  71. return 0
  72. fi
  73. return 1
  74. }
  75. update_stamp()
  76. {
  77. stamp="$1"
  78. touch $stamp
  79. }
  80. # we check here if autoclean was enough sizewise
  81. check_size_constraints()
  82. {
  83. # min-age in days
  84. MaxAge=0
  85. MinAge=2
  86. MaxSize=0
  87. CacheDir="var/cache/apt"
  88. CacheArchive="archives/"
  89. eval $(apt-config shell MaxAge APT::Archives::MaxAge)
  90. eval $(apt-config shell MinAge APT::Archives::MinAge)
  91. eval $(apt-config shell MaxSize APT::Archives::MaxSize)
  92. eval $(apt-config shell Dir Dir)
  93. eval $(apt-config shell CacheDir Dir::Cache)
  94. eval $(apt-config shell CacheArchive Dir::Cache::archives)
  95. # sanity check
  96. if [ -z "$CacheDir" -o -z "$CacheArchive" ]; then
  97. echo "empty Dir::Cache or Dir::Cache::archives, exiting"
  98. exit
  99. fi
  100. Cache="${Dir%/}/${CacheDir%/}/${CacheArchive%/}/"
  101. # check age
  102. if [ ! $MaxAge -eq 0 ] && [ ! $MinAge -eq 0 ]; then
  103. find $Cache -name "*.deb" \( -mtime +$MaxAge -and -ctime +$MaxAge \) -and -not \( -mtime -$MinAge -or -ctime -$MinAge \) -print0 | xargs -r -0 rm -f
  104. elif [ ! $MaxAge -eq 0 ]; then
  105. find $Cache -name "*.deb" -ctime +$MaxAge -and -mtime +$MaxAge -print0 | xargs -r -0 rm -f
  106. fi
  107. # check size
  108. if [ ! $MaxSize -eq 0 ]; then
  109. # maxSize is in MB
  110. MaxSize=$(($MaxSize*1024))
  111. #get current time
  112. now=$(date --date=$(date --iso-8601) +%s)
  113. MinAge=$(($MinAge*24*60*60))
  114. # reverse-sort by mtime
  115. for file in $(ls -rt $Cache/*.deb 2>/dev/null); do
  116. du=$(du -s $Cache)
  117. size=${du%%/*}
  118. # check if the cache is small enough
  119. if [ $size -lt $MaxSize ]; then
  120. break
  121. fi
  122. # check for MinAge of the file
  123. if [ ! $MinAge -eq 0 ]; then
  124. # check both ctime and mtime
  125. mtime=$(stat -c %Y $file)
  126. ctime=$(stat -c %Z $file)
  127. if [ $mtime -gt $ctime ]; then
  128. delta=$(($now-$mtime))
  129. else
  130. delta=$(($now-$ctime))
  131. fi
  132. #echo "$file ($delta), $MinAge"
  133. if [ $delta -le $MinAge ]; then
  134. #echo "Skiping $file (delta=$delta)"
  135. break
  136. fi
  137. fi
  138. # delete oldest file
  139. rm -f $file
  140. done
  141. fi
  142. }
  143. # sleep for a random interval of time (default 30min)
  144. # (some code taken from cron-apt, thanks)
  145. random_sleep()
  146. {
  147. RandomSleep=1800
  148. eval $(apt-config shell RandomSleep APT::Periodic::RandomSleep)
  149. if [ $RandomSleep -eq 0 ]; then
  150. return
  151. fi
  152. if [ -z "$RANDOM" ] ; then
  153. # A fix for shells that do not have this bash feature.
  154. RANDOM=$(dd if=/dev/urandom count=1 2> /dev/null | cksum | cut -c"1-5")
  155. fi
  156. TIME=$(($RANDOM % $RandomSleep))
  157. sleep $TIME
  158. }
  159. # main
  160. if ! which apt-config >/dev/null; then
  161. exit 0
  162. fi
  163. UpdateInterval=0
  164. DownloadUpgradeableInterval=0
  165. eval $(apt-config shell UpdateInterval APT::Periodic::Update-Package-Lists DownloadUpgradeableInterval APT::Periodic::Download-Upgradeable-Packages)
  166. AutocleanInterval=$DownloadUpgradeableInterval
  167. eval $(apt-config shell AutocleanInterval APT::Periodic::AutocleanInterval)
  168. UnattendedUpgradeInterval=0
  169. eval $(apt-config shell UnattendedUpgradeInterval APT::Periodic::Unattended-Upgrade)
  170. # check if we actually have to do anything
  171. if [ $UpdateInterval -eq 0 ] &&
  172. [ $DownloadUpgradeableInterval -eq 0 ] &&
  173. [ $UnattendedUpgradeInterval -eq 0 ] &&
  174. [ $AutocleanInterval -eq 0 ]; then
  175. exit 0
  176. fi
  177. # laptop check, on_ac_power returns:
  178. # 0 (true) System is on mains power
  179. # 1 (false) System is not on mains power
  180. # 255 (false) Power status could not be determined
  181. # Desktop systems always return 255 it seems
  182. if which on_ac_power >/dev/null; then
  183. on_ac_power
  184. if [ $? -eq 1 ]; then
  185. exit 0
  186. fi
  187. fi
  188. # sleep random amount of time to avoid hitting the
  189. # mirrors at the same time
  190. random_sleep
  191. # check if we can access the cache
  192. if ! apt-get check -q -q 2>/dev/null; then
  193. # wait random amount of time before retrying
  194. random_sleep
  195. # check again
  196. if ! apt-get check -q -q 2>/dev/null; then
  197. echo "$0: could not lock the APT cache while performing daily cron job. "
  198. echo "Is another package manager working?"
  199. exit 1
  200. fi
  201. fi
  202. UPDATE_STAMP=/var/lib/apt/periodic/update-stamp
  203. if check_stamp $UPDATE_STAMP $UpdateInterval; then
  204. if apt-get -qq update 2>/dev/null; then
  205. if which dbus-send >/dev/null && pidof dbus-daemon >/dev/null; then
  206. dbus-send --system / app.apt.dbus.updated boolean:true
  207. fi
  208. update_stamp $UPDATE_STAMP
  209. fi
  210. fi
  211. DOWNLOAD_UPGRADEABLE_STAMP=/var/lib/apt/periodic/download-upgradeable-stamp
  212. if check_stamp $DOWNLOAD_UPGRADEABLE_STAMP $DownloadUpgradeableInterval; then
  213. apt-get -qq -d dist-upgrade 2>/dev/null
  214. update_stamp $DOWNLOAD_UPGRADEABLE_STAMP
  215. fi
  216. UPGRADE_STAMP=/var/lib/apt/periodic/upgrade-stamp
  217. if check_stamp $UPGRADE_STAMP $UnattendedUpgradeInterval; then
  218. unattended-upgrade
  219. update_stamp $UPGRADE_STAMP
  220. fi
  221. AUTOCLEAN_STAMP=/var/lib/apt/periodic/autoclean-stamp
  222. if check_stamp $AUTOCLEAN_STAMP $AutocleanInterval; then
  223. apt-get -qq autoclean
  224. update_stamp $AUTOCLEAN_STAMP
  225. fi
  226. # check cache size
  227. check_size_constraints