apt.cron.daily 14 KB

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