apt.cron.daily 7.6 KB

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