apt.cron.daily 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473
  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. # we check here if autoclean was enough sizewise
  97. check_size_constraints()
  98. {
  99. # min-age in days
  100. MaxAge=0
  101. MinAge=2
  102. MaxSize=0
  103. CacheDir="var/cache/apt"
  104. CacheArchive="archives/"
  105. eval $(apt-config shell MaxAge APT::Archives::MaxAge)
  106. eval $(apt-config shell MinAge APT::Archives::MinAge)
  107. eval $(apt-config shell MaxSize APT::Archives::MaxSize)
  108. eval $(apt-config shell Dir Dir)
  109. eval $(apt-config shell CacheDir Dir::Cache)
  110. eval $(apt-config shell CacheArchive Dir::Cache::archives)
  111. # sanity check
  112. if [ -z "$CacheDir" -o -z "$CacheArchive" ]; then
  113. echo "empty Dir::Cache or Dir::Cache::archives, exiting"
  114. exit
  115. fi
  116. Cache="${Dir%/}/${CacheDir%/}/${CacheArchive%/}/"
  117. # check age
  118. if [ ! $MaxAge -eq 0 ] && [ ! $MinAge -eq 0 ]; then
  119. find $Cache -name "*.deb" \( -mtime +$MaxAge -and -ctime +$MaxAge \) -and -not \( -mtime -$MinAge -or -ctime -$MinAge \) -print0 | xargs -r -0 rm -f
  120. elif [ ! $MaxAge -eq 0 ]; then
  121. find $Cache -name "*.deb" -ctime +$MaxAge -and -mtime +$MaxAge -print0 | xargs -r -0 rm -f
  122. fi
  123. # check size
  124. if [ ! $MaxSize -eq 0 ]; then
  125. # maxSize is in MB
  126. MaxSize=$(($MaxSize*1024))
  127. #get current time
  128. now=$(date --date=$(date --iso-8601) +%s)
  129. MinAge=$(($MinAge*24*60*60))
  130. # reverse-sort by mtime
  131. for file in $(ls -rt $Cache/*.deb 2>/dev/null); do
  132. du=$(du -s $Cache)
  133. size=${du%%/*}
  134. # check if the cache is small enough
  135. if [ $size -lt $MaxSize ]; then
  136. break
  137. fi
  138. # check for MinAge of the file
  139. if [ ! $MinAge -eq 0 ]; then
  140. # check both ctime and mtime
  141. mtime=$(stat -c %Y $file)
  142. ctime=$(stat -c %Z $file)
  143. if [ $mtime -gt $ctime ]; then
  144. delta=$(($now-$mtime))
  145. else
  146. delta=$(($now-$ctime))
  147. fi
  148. #echo "$file ($delta), $MinAge"
  149. if [ $delta -le $MinAge ]; then
  150. #echo "Skiping $file (delta=$delta)"
  151. break
  152. fi
  153. fi
  154. # delete oldest file
  155. rm -f $file
  156. done
  157. fi
  158. }
  159. # sleep for a random interval of time (default 30min)
  160. # (some code taken from cron-apt, thanks)
  161. random_sleep()
  162. {
  163. RandomSleep=1800
  164. eval $(apt-config shell RandomSleep APT::Periodic::RandomSleep)
  165. if [ $RandomSleep -eq 0 ]; then
  166. return
  167. fi
  168. if [ -z "$RANDOM" ] ; then
  169. # A fix for shells that do not have this bash feature.
  170. RANDOM=$(dd if=/dev/urandom count=1 2> /dev/null | cksum | cut -c"1-5")
  171. fi
  172. TIME=$(($RANDOM % $RandomSleep))
  173. sleep $TIME
  174. }
  175. debug_echo()
  176. {
  177. # Display message if $VERBOSE >= 1
  178. if [ "$VERBOSE" -ge 1 ]; then
  179. echo $1 1>&2
  180. fi
  181. }
  182. # main
  183. # check apt-config exstance
  184. if ! which apt-config >/dev/null ; then
  185. exit 0
  186. fi
  187. # Set VERBOSE mode from apt-config (or inherit from environment)
  188. eval $(apt-config shell VERBOSE APT::Periodic::Verbose)
  189. if [ -z "$VERBOSE" ]; then
  190. VERBOSE="0"
  191. fi
  192. if [ "$VERBOSE" -le 2 ]; then
  193. # quiet for 0,1,2
  194. XSTDOUT=">/dev/null"
  195. XSTDERR="2>/dev/null"
  196. XAPTOPT="-qq"
  197. XUUPOPT=""
  198. else
  199. XSTDOUT=""
  200. XSTDERR=""
  201. XAPTOPT=""
  202. XUUPOPT="-d"
  203. fi
  204. if [ "$VERBOSE" -ge 3 ]; then
  205. # trace output
  206. set -x
  207. fi
  208. # laptop check, on_ac_power returns:
  209. # 0 (true) System is on main power
  210. # 1 (false) System is not on main power
  211. # 255 (false) Power status could not be determined
  212. # Desktop systems always return 255 it seems
  213. if which on_ac_power >/dev/null; then
  214. on_ac_power
  215. POWER=$?
  216. if [ $POWER -eq 1 ]; then
  217. debug_echo "exit: system on main power."
  218. exit 0
  219. elif [ $POWER -ne 0 ]; then
  220. debug_echo "exit: power status ($POWER) undetermined."
  221. exit 0
  222. fi
  223. debug_echo "system is on main power."
  224. fi
  225. # check if we can lock the cache and if the cache is clean
  226. if which apt-get >/dev/null && ! eval apt-get check $XAPTOPT $XSTDERR ; then
  227. debug_echo "error encountered in cron job with \"apt-get check\"."
  228. exit 0
  229. fi
  230. # No need to check for apt-get below
  231. # Global current time in seconds since 1970-01-01 00:00:00 UTC
  232. now=$(date +%s)
  233. # Set default values and normalize
  234. Dir="/"
  235. eval $(apt-config shell Dir Dir)
  236. Dir=${Dir%/}
  237. CacheDir="var/cache/apt/"
  238. eval $(apt-config shell CacheDir Dir::Cache)
  239. CacheDir=${CacheDir%/}
  240. if [ -z "$CacheDir" ]; then
  241. debug_echo "practically empty Dir::Cache, exiting"
  242. exit 0
  243. fi
  244. CacheArchive="archives/"
  245. eval $(apt-config shell CacheArchive Dir::Cache::Archives)
  246. CacheArchive=${CacheArchive%/}
  247. if [ -z "$CacheArchive" ]; then
  248. debug_echo "practically empty Dir::Cache::archives, exiting"
  249. exit 0
  250. fi
  251. BackupArchiveInterval=0
  252. eval $(apt-config shell BackupArchiveInterval APT::Periodic::BackupArchiveInterval)
  253. BackupLevel=3
  254. eval $(apt-config shell BackupLevel APT::Periodic::BackupLevel)
  255. if [ $BackupLevel -le 1 ]; then BackupLevel=2 ; fi
  256. CacheBackup="backup/"
  257. eval $(apt-config shell CacheBackup Dir::Cache::Backup)
  258. CacheBackup=${CacheBackup%/}
  259. if [ -z "$CacheBackup" ]; then
  260. echo "practically empty Dir::Cache::Backup, exiting" 1>&2
  261. exit 0
  262. fi
  263. # Support old Archive for compatibility.
  264. # Document only Periodic for all controling parameters of this script.
  265. MaxAge=0
  266. eval $(apt-config shell MaxAge APT::Archives::MaxAge)
  267. eval $(apt-config shell MaxAge APT::Periodic::MaxAge)
  268. MinAge=2
  269. eval $(apt-config shell MinAge APT::Archives::MinAge)
  270. eval $(apt-config shell MinAge APT::Periodic::MinAge)
  271. MaxSize=0
  272. eval $(apt-config shell MaxSize APT::Archives::MaxSize)
  273. eval $(apt-config shell MaxSize APT::Periodic::MaxSize)
  274. UpdateInterval=0
  275. eval $(apt-config shell UpdateInterval APT::Periodic::Update-Package-Lists)
  276. DownloadUpgradeableInterval=0
  277. eval $(apt-config shell DownloadUpgradeableInterval APT::Periodic::Download-Upgradeable-Packages)
  278. UnattendedUpgradeInterval=0
  279. eval $(apt-config shell UnattendedUpgradeInterval APT::Periodic::Unattended-Upgrade)
  280. AutocleanInterval=0
  281. eval $(apt-config shell AutocleanInterval APT::Periodic::AutocleanInterval)
  282. Cache="${Dir}/${CacheDir}/${CacheArchive}/"
  283. Back="${Dir}/${CacheDir}/${CacheBackup}/"
  284. BackX="${Back}${CacheArchive}/"
  285. for x in $(seq 0 1 $((${BackupLevel}-1))); do
  286. eval "Back${x}=${Back}${x}/"
  287. done
  288. # check if we actually have to do anything
  289. if [ $UpdateInterval -eq 0 ] &&
  290. [ $DownloadUpgradeableInterval -eq 0 ] &&
  291. [ $UnattendedUpgradeInterval -eq 0 ] &&
  292. [ $BackupArchiveInterval -eq 0 ] &&
  293. [ $AutocleanInterval -eq 0 ]; then
  294. exit 0
  295. fi
  296. # backup after n-days if archive contents changed.
  297. # (This uses hardlink to save disk space)
  298. BACKUP_ARCHIVE_STAMP=/var/lib/apt/periodic/backup-archive-stamp
  299. if check_stamp $BACKUP_ARCHIVE_STAMP $BackupArchiveInterval; then
  300. 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
  301. mkdir -p $Back
  302. rm -rf $Back$((${BackupLevel}-1))
  303. for y in $(seq $((${BackupLevel}-1)) -1 1); do
  304. eval BackY=${Back}$y
  305. eval BackZ=${Back}$(($y-1))
  306. if [ -e $BackZ ]; then mv -f $BackZ $BackY ; fi
  307. done
  308. cp -la $Cache $Back ; mv -f $BackX $Back0
  309. update_stamp $BACKUP_ARCHIVE_STAMP
  310. debug_echo "backup with hardlinks. (success)"
  311. else
  312. debug_echo "skip backup since same content."
  313. fi
  314. else
  315. debug_echo "skip backup since too new."
  316. fi
  317. # package archive contnts removal by package age
  318. if [ $MaxAge -ne 0 ] && [ $MinAge -ne 0 ]; then
  319. find $Cache -name "*.deb" \( -mtime +$MaxAge -and -ctime +$MaxAge \) -and -not \( -mtime -$MinAge -or -ctime -$MinAge \) -print0 | xargs -r -0 rm -f
  320. debug_echo "aged: ctime <$MaxAge and mtime <$MaxAge and ctime>$MinAge and mtime>$MinAge"
  321. elif [ $MaxAge -ne 0 ]; then
  322. find $Cache -name "*.deb" -ctime +$MaxAge -and -mtime +$MaxAge -print0 | xargs -r -0 rm -f
  323. debug_echo "aged: ctime <$MaxAge and mtime <$MaxAge only"
  324. else
  325. debug_echo "skip aging since MaxAge is 0"
  326. fi
  327. # package archive contnts removal down to $MaxSize
  328. if [ $MaxSize -ne 0 ]; then
  329. MinAgeSec=$(($MinAge*24*60*60))
  330. # reverse-sort by mtime
  331. for file in $(ls -rt $Cache/*.deb 2>/dev/null); do
  332. du=$(du -m -s $Cache)
  333. size=${du%%/*}
  334. # check if the cache is small enough
  335. if [ $size -lt $MaxSize ]; then
  336. debug_echo "end remove by archive size: size=$size < $MaxSize"
  337. break
  338. fi
  339. # check for MinAge in second of the file
  340. if [ $MinAgeSec -ne 0 ]; then
  341. # check both ctime and mtime
  342. mtime=$(stat -c %Y $file)
  343. ctime=$(stat -c %Z $file)
  344. if [ $mtime -gt $ctime ]; then
  345. delta=$(($now-$mtime))
  346. else
  347. delta=$(($now-$ctime))
  348. fi
  349. if [ $delta -le $MinAgeSec ]; then
  350. debug_echo "skip remove by archive size: $file, delta=$delta < $MinAgeSec"
  351. else
  352. # delete oldest file
  353. debug_echo "remove by archive size: $file, delta=$delta >= $MinAgeSec (sec), size=$size >= $MaxSize"
  354. rm -f $file
  355. fi
  356. fi
  357. done
  358. fi
  359. # update package lists
  360. UPDATE_STAMP=/var/lib/apt/periodic/update-stamp
  361. if check_stamp $UPDATE_STAMP $UpdateInterval; then
  362. if eval apt-get $XAPTOPT -y update $XSTDERR; then
  363. debug_echo "download updated metadata (success)."
  364. if which dbus-send >/dev/null && pidof dbus-daemon >/dev/null; then
  365. if dbus-send --system / app.apt.dbus.updated boolean:true ; then
  366. debug_echo "send dbus signal (success)"
  367. else
  368. debug_echo "send dbus signal (error)"
  369. fi
  370. else
  371. debug_echo "dbus signal not send (command not available)"
  372. fi
  373. update_stamp $UPDATE_STAMP
  374. # download all upgradeable packages if it is requested
  375. DOWNLOAD_UPGRADEABLE_STAMP=/var/lib/apt/periodic/download-upgradeable-stamp
  376. if check_stamp $DOWNLOAD_UPGRADEABLE_STAMP $DownloadUpgradeableInterval; then
  377. if eval apt-get $XAPTOPT -y -d dist-upgrade $XSTDERR; then
  378. update_stamp $DOWNLOAD_UPGRADEABLE_STAMP
  379. debug_echo "download upgradable (success)."
  380. # auto upgrade all upgradeable packages
  381. UPGRADE_STAMP=/var/lib/apt/periodic/upgrade-stamp
  382. if which unattended-upgrade >/dev/null && check_stamp $UPGRADE_STAMP $UnattendedUpgradeInterval; then
  383. if unattended-upgrade $XUUPOPT; then
  384. update_stamp $UPGRADE_STAMP
  385. debug_echo "unattended-upgrade (success)."
  386. else
  387. debug_echo "unattended-upgrade (error)."
  388. fi
  389. else
  390. debug_echo "unattended-upgrade (not run)."
  391. fi
  392. else
  393. debug_echo "download upgradable (error)."
  394. fi
  395. else
  396. debug_echo "download upgradable (not run)."
  397. fi
  398. else
  399. debug_echo "download updated metadata (error)."
  400. fi
  401. else
  402. debug_echo "download updated metadata (not run)."
  403. fi
  404. # autoclean package archive
  405. AUTOCLEAN_STAMP=/var/lib/apt/periodic/autoclean-stamp
  406. if check_stamp $AUTOCLEAN_STAMP $AutocleanInterval; then
  407. if apt-get $XAPTOPT -y autoclean $XSTDERR; then
  408. debug_echo "autoclean (success)."
  409. update_stamp $AUTOCLEAN_STAMP
  410. else
  411. debug_echo "autoclean (error)."
  412. fi
  413. else
  414. debug_echo "autoclean (not run)."
  415. fi
  416. #
  417. # vim: set sts=4 ai :
  418. #