apt.cron.daily 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  1. #!/bin/sh
  2. #set -e
  3. #
  4. # This file understands the following apt configuration variables:
  5. # Values here are the default.
  6. # Create /etc/apt/apt.conf.d/02periodic file to set your preference.
  7. #
  8. # Dir "/";
  9. # - RootDir for all configuration files
  10. #
  11. # Dir::Cache "var/apt/cache/";
  12. # - Set apt package cache directory
  13. #
  14. # Dir::Cache::Archive "archives/";
  15. # - Set package archive directory
  16. #
  17. # APT::Periodic::BackupArchiveInterval "0";
  18. # - Backup after n-days if archive contents changed.(0=disable)
  19. #
  20. # APT::Periodic::BackupLevel "3";
  21. # - Backup level.(0=disable), 1 is invalid.
  22. #
  23. # Dir::Cache::Backup "backup/";
  24. # - Set periodic package backup directory
  25. #
  26. # APT::Archives::MaxAge "0"; (old, deprecated)
  27. # APT::Periodic::MaxAge "0"; (new)
  28. # - Set maximum allowed age of a cache package file. If a cache
  29. # package file is older it is deleted (0=disable)
  30. #
  31. # APT::Archives::MinAge "2"; (old, deprecated)
  32. # APT::Periodic::MinAge "2"; (new)
  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. # APT::Archives::MaxSize "0"; (old, deprecated)
  38. # APT::Periodic::MaxSize "0"; (new)
  39. # - Set maximum size of the cache in MB (0=disable). If the cache
  40. # is bigger, cached package files are deleted until the size
  41. # requirement is met (the biggest packages will be deleted
  42. # first).
  43. #
  44. # APT::Periodic::Update-Package-Lists "0";
  45. # - Do "apt-get update" automatically every n-days (0=disable)
  46. #
  47. # APT::Periodic::Download-Upgradeable-Packages "0";
  48. # - Do "apt-get upgrade --download-only" every n-days (0=disable)
  49. #
  50. # APT::Periodic::Unattended-Upgrade "0";
  51. # - Run the "unattended-upgrade" security upgrade script
  52. # every n-days (0=disabled)
  53. # Requires the package "unattended-upgrades" and will write
  54. # a log in /var/log/unattended-upgrades
  55. #
  56. # APT::Periodic::AutocleanInterval "0";
  57. # - Do "apt-get autoclean" every n-days (0=disable)
  58. #
  59. # APT::Periodic::Verbose "0";
  60. # - Send report mail to root
  61. # 0: no report (or null string)
  62. # 1: progress report (actually any string)
  63. # 2: + command outputs (remove -qq, remove 2>/dev/null, add -d)
  64. # 3: + trace on
  65. check_stamp()
  66. {
  67. stamp="$1"
  68. interval="$2"
  69. if [ $interval -eq 0 ]; then
  70. debug_echo "check_stamp: interval=0."
  71. # treat as no time has passed
  72. return 1
  73. fi
  74. if [ ! -f $stamp ]; then
  75. update_stamp $stamp
  76. debug_echo "check_stamp: missing time stamp file: $stamp."
  77. # treat as enough time has passed
  78. return 0
  79. fi
  80. # compare midnight today to midnight the day the stamp was updated
  81. stamp=$(date -r $stamp '+%s')
  82. delta=$(($now-$stamp))
  83. # intervall is in days, convert to sec.
  84. interval=$(($interval*60*60*24))
  85. debug_echo "check_stamp: interval=$interval, now=$now, stamp=$stamp, delta=$delta (sec)"
  86. if [ $delta -ge $interval ]; then
  87. return 0
  88. fi
  89. return 1
  90. }
  91. update_stamp()
  92. {
  93. stamp="$1"
  94. touch $stamp
  95. }
  96. debug_echo()
  97. {
  98. # Display message if $VERBOSE >= 1
  99. if [ "$VERBOSE" -ge 1 ]; then
  100. echo $1 1>&2
  101. fi
  102. }
  103. # check apt-config exstance
  104. if ! which apt-config >/dev/null ; then
  105. exit 0
  106. fi
  107. # Set VERBOSE mode from apt-config (or inherit from environment)
  108. eval $(apt-config shell VERBOSE APT::Periodic::Verbose)
  109. if [ -z "$VERBOSE" ]; then
  110. VERBOSE="0"
  111. fi
  112. if [ "$VERBOSE" -le 2 ]; then
  113. # quiet for 0,1,2
  114. XSTDOUT=">/dev/null"
  115. XSTDERR="2>/dev/null"
  116. XAPTOPT="-qq"
  117. XUUPOPT=""
  118. else
  119. XSTDOUT=""
  120. XSTDERR=""
  121. XAPTOPT=""
  122. XUUPOPT="-d"
  123. fi
  124. if [ "$VERBOSE" -ge 3 ]; then
  125. # trace output
  126. set -x
  127. fi
  128. # laptop check, on_ac_power returns:
  129. # 0 (true) System is on main power
  130. # 1 (false) System is not on main power
  131. # 255 (false) Power status could not be determined
  132. # Desktop systems always return 255 it seems
  133. if which on_ac_power >/dev/null; then
  134. on_ac_power
  135. POWER=$?
  136. if [ $POWER -eq 1 ]; then
  137. debug_echo "exit: system on main power."
  138. exit 0
  139. elif [ $POWER -ne 0 ]; then
  140. debug_echo "exit: power status ($POWER) undetermined."
  141. exit 0
  142. fi
  143. debug_echo "system is on main power."
  144. fi
  145. # check if we can lock the cache and if the cache is clean
  146. if which apt-get >/dev/null && ! eval apt-get check $XAPTOPT $XSTDERR ; then
  147. debug_echo "error encountered in cron job with \"apt-get check\"."
  148. exit 0
  149. fi
  150. # No need to check for apt-get below
  151. # Global current time in seconds since 1970-01-01 00:00:00 UTC
  152. now=$(date +%s)
  153. # Set default values and normalize
  154. Dir="/"
  155. eval $(apt-config shell Dir Dir)
  156. Dir=${Dir%/}
  157. CacheDir="var/cache/apt/"
  158. eval $(apt-config shell CacheDir Dir::Cache)
  159. CacheDir=${CacheDir%/}
  160. if [ -z "$CacheDir" ]; then
  161. debug_echo "practically empty Dir::Cache, exiting"
  162. exit 0
  163. fi
  164. CacheArchive="archives/"
  165. eval $(apt-config shell CacheArchive Dir::Cache::Archives)
  166. CacheArchive=${CacheArchive%/}
  167. if [ -z "$CacheArchive" ]; then
  168. debug_echo "practically empty Dir::Cache::archives, exiting"
  169. exit 0
  170. fi
  171. BackupArchiveInterval=0
  172. eval $(apt-config shell BackupArchiveInterval APT::Periodic::BackupArchiveInterval)
  173. BackupLevel=3
  174. eval $(apt-config shell BackupLevel APT::Periodic::BackupLevel)
  175. if [ $BackupLevel -le 1 ]; then BackupLevel=2 ; fi
  176. CacheBackup="backup/"
  177. eval $(apt-config shell CacheBackup Dir::Cache::Backup)
  178. CacheBackup=${CacheBackup%/}
  179. if [ -z "$CacheBackup" ]; then
  180. echo "practically empty Dir::Cache::Backup, exiting" 1>&2
  181. exit 0
  182. fi
  183. # Support old Archive for compatibility.
  184. # Document only Periodic for all controling parameters of this script.
  185. MaxAge=0
  186. eval $(apt-config shell MaxAge APT::Archives::MaxAge)
  187. eval $(apt-config shell MaxAge APT::Periodic::MaxAge)
  188. MinAge=2
  189. eval $(apt-config shell MinAge APT::Archives::MinAge)
  190. eval $(apt-config shell MinAge APT::Periodic::MinAge)
  191. MaxSize=0
  192. eval $(apt-config shell MaxSize APT::Archives::MaxSize)
  193. eval $(apt-config shell MaxSize APT::Periodic::MaxSize)
  194. UpdateInterval=0
  195. eval $(apt-config shell UpdateInterval APT::Periodic::Update-Package-Lists)
  196. DownloadUpgradeableInterval=0
  197. eval $(apt-config shell DownloadUpgradeableInterval APT::Periodic::Download-Upgradeable-Packages)
  198. UnattendedUpgradeInterval=0
  199. eval $(apt-config shell UnattendedUpgradeInterval APT::Periodic::Unattended-Upgrade)
  200. AutocleanInterval=0
  201. eval $(apt-config shell AutocleanInterval APT::Periodic::AutocleanInterval)
  202. Cache="${Dir}/${CacheDir}/${CacheArchive}/"
  203. Back="${Dir}/${CacheDir}/${CacheBackup}/"
  204. BackX="${Back}${CacheArchive}/"
  205. for x in $(seq 0 1 $((${BackupLevel}-1))); do
  206. eval "Back${x}=${Back}${x}/"
  207. done
  208. # check if we actually have to do anything
  209. if [ $UpdateInterval -eq 0 ] &&
  210. [ $DownloadUpgradeableInterval -eq 0 ] &&
  211. [ $UnattendedUpgradeInterval -eq 0 ] &&
  212. [ $BackupArchiveInterval -eq 0 ] &&
  213. [ $AutocleanInterval -eq 0 ]; then
  214. exit 0
  215. fi
  216. # backup after n-days if archive contents changed.
  217. # (This uses hardlink to save disk space)
  218. BACKUP_ARCHIVE_STAMP=/var/lib/apt/periodic/backup-archive-stamp
  219. if check_stamp $BACKUP_ARCHIVE_STAMP $BackupArchiveInterval; then
  220. if [ $({(cd $Cache 2>/dev/null; find . -name "*.deb"); (cd $Back0 2>/dev/null;find . -name "*.deb") ;}| sort|uniq -u|wc -l) -ne 0 ]; then
  221. mkdir -p $Back
  222. rm -rf $Back$((${BackupLevel}-1))
  223. for y in $(seq $((${BackupLevel}-1)) -1 1); do
  224. eval BackY=${Back}$y
  225. eval BackZ=${Back}$(($y-1))
  226. if [ -e $BackZ ]; then mv -f $BackZ $BackY ; fi
  227. done
  228. cp -la $Cache $Back ; mv -f $BackX $Back0
  229. update_stamp $BACKUP_ARCHIVE_STAMP
  230. debug_echo "backup with hardlinks. (success)"
  231. else
  232. debug_echo "skip backup since same content."
  233. fi
  234. else
  235. debug_echo "skip backup since too new."
  236. fi
  237. # package archive contnts removal by package age
  238. if [ $MaxAge -ne 0 ] && [ $MinAge -ne 0 ]; then
  239. find $Cache -name "*.deb" \( -mtime +$MaxAge -and -ctime +$MaxAge \) -and -not \( -mtime -$MinAge -or -ctime -$MinAge \) -print0 | xargs -r -0 rm -f
  240. debug_echo "aged: ctime <$MaxAge and mtime <$MaxAge and ctime>$MinAge and mtime>$MinAge"
  241. elif [ $MaxAge -ne 0 ]; then
  242. find $Cache -name "*.deb" -ctime +$MaxAge -and -mtime +$MaxAge -print0 | xargs -r -0 rm -f
  243. debug_echo "aged: ctime <$MaxAge and mtime <$MaxAge only"
  244. else
  245. debug_echo "skip aging since MaxAge is 0"
  246. fi
  247. # package archive contnts removal down to $MaxSize
  248. if [ $MaxSize -ne 0 ]; then
  249. MinAgeSec=$(($MinAge*24*60*60))
  250. # reverse-sort by mtime
  251. for file in $(ls -rt $Cache/*.deb 2>/dev/null); do
  252. du=$(du -m -s $Cache)
  253. size=${du%%/*}
  254. # check if the cache is small enough
  255. if [ $size -lt $MaxSize ]; then
  256. debug_echo "end remove by archive size: size=$size < $MaxSize"
  257. break
  258. fi
  259. # check for MinAge in second of the file
  260. if [ $MinAgeSec -ne 0 ]; then
  261. # check both ctime and mtime
  262. mtime=$(stat -c %Y $file)
  263. ctime=$(stat -c %Z $file)
  264. if [ $mtime -gt $ctime ]; then
  265. delta=$(($now-$mtime))
  266. else
  267. delta=$(($now-$ctime))
  268. fi
  269. if [ $delta -le $MinAgeSec ]; then
  270. debug_echo "skip remove by archive size: $file, delta=$delta < $MinAgeSec"
  271. else
  272. # delete oldest file
  273. debug_echo "remove by archive size: $file, delta=$delta >= $MinAgeSec (sec), size=$size >= $MaxSize"
  274. rm -f $file
  275. fi
  276. fi
  277. done
  278. fi
  279. # update package lists
  280. UPDATE_STAMP=/var/lib/apt/periodic/update-stamp
  281. if check_stamp $UPDATE_STAMP $UpdateInterval; then
  282. if eval apt-get $XAPTOPT -y update $XSTDERR; then
  283. debug_echo "download updated metadata (success)."
  284. if which dbus-send >/dev/null && pidof dbus-daemon >/dev/null; then
  285. if dbus-send --system / app.apt.dbus.updated boolean:true ; then
  286. debug_echo "send dbus signal (success)"
  287. else
  288. debug_echo "send dbus signal (error)"
  289. fi
  290. else
  291. debug_echo "dbus signal not send (command not available)"
  292. fi
  293. update_stamp $UPDATE_STAMP
  294. # download all upgradeable packages if it is requested
  295. DOWNLOAD_UPGRADEABLE_STAMP=/var/lib/apt/periodic/download-upgradeable-stamp
  296. if check_stamp $DOWNLOAD_UPGRADEABLE_STAMP $DownloadUpgradeableInterval; then
  297. if eval apt-get $XAPTOPT -y -d dist-upgrade $XSTDERR; then
  298. update_stamp $DOWNLOAD_UPGRADEABLE_STAMP
  299. debug_echo "download upgradable (success)."
  300. # auto upgrade all upgradeable packages
  301. UPGRADE_STAMP=/var/lib/apt/periodic/upgrade-stamp
  302. if which unattended-upgrade >/dev/null && check_stamp $UPGRADE_STAMP $UnattendedUpgradeInterval; then
  303. if unattended-upgrade $XUUPOPT; then
  304. update_stamp $UPGRADE_STAMP
  305. debug_echo "unattended-upgrade (success)."
  306. else
  307. debug_echo "unattended-upgrade (error)."
  308. fi
  309. else
  310. debug_echo "unattended-upgrade (not run)."
  311. fi
  312. else
  313. debug_echo "download upgradable (error)."
  314. fi
  315. else
  316. debug_echo "download upgradable (not run)."
  317. fi
  318. else
  319. debug_echo "download updated metadata (error)."
  320. fi
  321. else
  322. debug_echo "download updated metadata (not run)."
  323. fi
  324. # autoclean package archive
  325. AUTOCLEAN_STAMP=/var/lib/apt/periodic/autoclean-stamp
  326. if check_stamp $AUTOCLEAN_STAMP $AutocleanInterval; then
  327. if apt-get $XAPTOPT -y autoclean $XSTDERR; then
  328. debug_echo "autoclean (success)."
  329. update_stamp $AUTOCLEAN_STAMP
  330. else
  331. debug_echo "autoclean (error)."
  332. fi
  333. else
  334. debug_echo "autoclean (not run)."
  335. fi
  336. #
  337. # vim: set sts=4 ai :
  338. #