apt.cron.daily 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521
  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/10periodic file to set your preference.
  7. #
  8. # Dir "/";
  9. # - RootDir for all configuration files
  10. #
  11. # Dir::Cache "var/cache/apt/";
  12. # - Set apt package cache directory
  13. #
  14. # Dir::Cache::Archives "archives/";
  15. # - Set package archive directory
  16. #
  17. # APT::Periodic::Enable "1";
  18. # - Enable the update/upgrade script (0=disable)
  19. #
  20. # APT::Periodic::BackupArchiveInterval "0";
  21. # - Backup after n-days if archive contents changed.(0=disable)
  22. #
  23. # APT::Periodic::BackupLevel "3";
  24. # - Backup level.(0=disable), 1 is invalid.
  25. #
  26. # Dir::Cache::Backup "backup/";
  27. # - Set periodic package backup directory
  28. #
  29. # APT::Archives::MaxAge "0"; (old, deprecated)
  30. # APT::Periodic::MaxAge "0"; (new)
  31. # - Set maximum allowed age of a cache package file. If a cache
  32. # package file is older it is deleted (0=disable)
  33. #
  34. # APT::Archives::MinAge "2"; (old, deprecated)
  35. # APT::Periodic::MinAge "2"; (new)
  36. # - Set minimum age of a package file. If a file is younger it
  37. # will not be deleted (0=disable). Useful to prevent races
  38. # and to keep backups of the packages for emergency.
  39. #
  40. # APT::Archives::MaxSize "0"; (old, deprecated)
  41. # APT::Periodic::MaxSize "0"; (new)
  42. # - Set maximum size of the cache in MB (0=disable). If the cache
  43. # is bigger, cached package files are deleted until the size
  44. # requirement is met (the oldest packages will be deleted
  45. # first).
  46. #
  47. # APT::Periodic::Update-Package-Lists "0";
  48. # - Do "apt-get update" automatically every n-days (0=disable)
  49. #
  50. # APT::Periodic::Download-Upgradeable-Packages "0";
  51. # - Do "apt-get upgrade --download-only" every n-days (0=disable)
  52. #
  53. # APT::Periodic::Download-Upgradeable-Packages-Debdelta "1";
  54. # - Use debdelta-upgrade to download updates if available (0=disable)
  55. #
  56. # APT::Periodic::Unattended-Upgrade "0";
  57. # - Run the "unattended-upgrade" security upgrade script
  58. # every n-days (0=disabled)
  59. # Requires the package "unattended-upgrades" and will write
  60. # a log in /var/log/unattended-upgrades
  61. #
  62. # APT::Periodic::AutocleanInterval "0";
  63. # - Do "apt-get autoclean" every n-days (0=disable)
  64. #
  65. # APT::Periodic::Verbose "0";
  66. # - Send report mail to root
  67. # 0: no report (or null string)
  68. # 1: progress report (actually any string)
  69. # 2: + command outputs (remove -qq, remove 2>/dev/null, add -d)
  70. # 3: + trace on
  71. #
  72. # APT::Periodic::RandomSleep "1800";
  73. # - The apt cron job will delay its execution by a random
  74. # time span between zero and 'APT::Periodic::RandomSleep'
  75. # seconds.
  76. # This is done because otherwise everyone would access the
  77. # mirror servers at the same time and put them collectively
  78. # under very high strain.
  79. # You can set this to '0' if you are using a local mirror and
  80. # do not care about the load spikes.
  81. # Note that sleeping in the apt job will be delaying the
  82. # execution of all subsequent cron.daily jobs.
  83. #
  84. check_stamp()
  85. {
  86. stamp="$1"
  87. interval="$2"
  88. if [ $interval -eq 0 ]; then
  89. debug_echo "check_stamp: interval=0"
  90. # treat as no time has passed
  91. return 1
  92. fi
  93. if [ ! -f $stamp ]; then
  94. debug_echo "check_stamp: missing time stamp file: $stamp."
  95. # treat as enough time has passed
  96. return 0
  97. fi
  98. # compare midnight today to midnight the day the stamp was updated
  99. stamp_file="$stamp"
  100. stamp=$(date --date=$(date -r $stamp_file --iso-8601) +%s 2>/dev/null)
  101. if [ "$?" != "0" ]; then
  102. # Due to some timezones returning 'invalid date' for midnight on
  103. # certain dates (e.g. America/Sao_Paulo), if date returns with error
  104. # remove the stamp file and return 0. See coreutils bug:
  105. # http://lists.gnu.org/archive/html/bug-coreutils/2007-09/msg00176.html
  106. rm -f "$stamp_file"
  107. return 0
  108. fi
  109. now=$(date --date=$(date --iso-8601) +%s 2>/dev/null)
  110. if [ "$?" != "0" ]; then
  111. # As above, due to some timezones returning 'invalid date' for midnight
  112. # on certain dates (e.g. America/Sao_Paulo), if date returns with error
  113. # return 0.
  114. return 0
  115. fi
  116. delta=$(($now-$stamp))
  117. # interval is in days, convert to sec.
  118. interval=$(($interval*60*60*24))
  119. debug_echo "check_stamp: interval=$interval, now=$now, stamp=$stamp, delta=$delta (sec)"
  120. # remove timestamps a day (or more) in the future and force re-check
  121. if [ $stamp -gt $(($now+86400)) ]; then
  122. echo "WARNING: file $stamp_file has a timestamp in the future: $stamp"
  123. rm -f "$stamp_file"
  124. return 0
  125. fi
  126. if [ $delta -ge $interval ]; then
  127. return 0
  128. fi
  129. return 1
  130. }
  131. update_stamp()
  132. {
  133. stamp="$1"
  134. touch $stamp
  135. }
  136. # we check here if autoclean was enough sizewise
  137. check_size_constraints()
  138. {
  139. MaxAge=0
  140. eval $(apt-config shell MaxAge APT::Archives::MaxAge)
  141. eval $(apt-config shell MaxAge APT::Periodic::MaxAge)
  142. MinAge=2
  143. eval $(apt-config shell MinAge APT::Archives::MinAge)
  144. eval $(apt-config shell MinAge APT::Periodic::MinAge)
  145. MaxSize=0
  146. eval $(apt-config shell MaxSize APT::Archives::MaxSize)
  147. eval $(apt-config shell MaxSize APT::Periodic::MaxSize)
  148. Cache="/var/cache/apt/archives/"
  149. eval $(apt-config shell Cache Dir::Cache::archives/d)
  150. # sanity check
  151. if [ -z "$Cache" ]; then
  152. echo "empty Dir::Cache::archives, exiting"
  153. exit
  154. fi
  155. # check age
  156. if [ ! $MaxAge -eq 0 ] && [ ! $MinAge -eq 0 ]; then
  157. debug_echo "aged: ctime <$MaxAge and mtime <$MaxAge and ctime>$MinAge and mtime>$MinAge"
  158. find $Cache -name "*.deb" \( -mtime +$MaxAge -and -ctime +$MaxAge \) -and -not \( -mtime -$MinAge -or -ctime -$MinAge \) -print0 | xargs -r -0 rm -f
  159. elif [ ! $MaxAge -eq 0 ]; then
  160. debug_echo "aged: ctime <$MaxAge and mtime <$MaxAge only"
  161. find $Cache -name "*.deb" -ctime +$MaxAge -and -mtime +$MaxAge -print0 | xargs -r -0 rm -f
  162. else
  163. debug_echo "skip aging since MaxAge is 0"
  164. fi
  165. # check size
  166. if [ ! $MaxSize -eq 0 ]; then
  167. # maxSize is in MB
  168. MaxSize=$(($MaxSize*1024))
  169. #get current time
  170. now=$(date --date=$(date --iso-8601) +%s)
  171. MinAge=$(($MinAge*24*60*60))
  172. # reverse-sort by mtime
  173. for file in $(ls -rt $Cache/*.deb 2>/dev/null); do
  174. du=$(du -s $Cache)
  175. size=${du%%/*}
  176. # check if the cache is small enough
  177. if [ $size -lt $MaxSize ]; then
  178. debug_echo "end remove by archive size: size=$size < $MaxSize"
  179. break
  180. fi
  181. # check for MinAge of the file
  182. if [ $MinAge -ne 0 ]; then
  183. # check both ctime and mtime
  184. mtime=$(stat -c %Y $file)
  185. ctime=$(stat -c %Z $file)
  186. if [ $mtime -gt $ctime ]; then
  187. delta=$(($now-$mtime))
  188. else
  189. delta=$(($now-$ctime))
  190. fi
  191. if [ $delta -le $MinAge ]; then
  192. debug_echo "skip remove by archive size: $file, delta=$delta < $MinAge"
  193. break
  194. else
  195. # delete oldest file
  196. debug_echo "remove by archive size: $file, delta=$delta >= $MinAge (sec), size=$size >= $MaxSize"
  197. rm -f $file
  198. fi
  199. fi
  200. done
  201. fi
  202. }
  203. # deal with the Apt::Periodic::BackupArchiveInterval
  204. do_cache_backup()
  205. {
  206. BackupArchiveInterval="$1"
  207. if [ $BackupArchiveInterval -eq 0 ]; then
  208. return
  209. fi
  210. # Set default values and normalize
  211. CacheDir="/var/cache/apt"
  212. eval $(apt-config shell CacheDir Dir::Cache/d)
  213. CacheDir=${CacheDir%/}
  214. if [ -z "$CacheDir" ]; then
  215. debug_echo "practically empty Dir::Cache, exiting"
  216. return 0
  217. fi
  218. Cache="${CacheDir}/archives/"
  219. eval $(apt-config shell Cache Dir::Cache::Archives/d)
  220. if [ -z "$Cache" ]; then
  221. debug_echo "practically empty Dir::Cache::archives, exiting"
  222. return 0
  223. fi
  224. BackupLevel=3
  225. eval $(apt-config shell BackupLevel APT::Periodic::BackupLevel)
  226. if [ $BackupLevel -le 1 ]; then
  227. BackupLevel=2 ;
  228. fi
  229. Back="${CacheDir}/backup/"
  230. eval $(apt-config shell Back Dir::Cache::Backup/d)
  231. if [ -z "$Back" ]; then
  232. echo "practically empty Dir::Cache::Backup, exiting" 1>&2
  233. return
  234. fi
  235. CacheArchive="$(basename "${Cache}")"
  236. test -n "${CacheArchive}" || CacheArchive="archives"
  237. BackX="${Back}${CacheArchive}/"
  238. for x in $(seq 0 1 $((${BackupLevel}-1))); do
  239. eval "Back${x}=${Back}${x}/"
  240. done
  241. # backup after n-days if archive contents changed.
  242. # (This uses hardlink to save disk space)
  243. BACKUP_ARCHIVE_STAMP=/var/lib/apt/periodic/backup-archive-stamp
  244. if check_stamp $BACKUP_ARCHIVE_STAMP $BackupArchiveInterval; then
  245. 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
  246. mkdir -p $Back
  247. rm -rf $Back$((${BackupLevel}-1))
  248. for y in $(seq $((${BackupLevel}-1)) -1 1); do
  249. eval BackY=${Back}$y
  250. eval BackZ=${Back}$(($y-1))
  251. if [ -e $BackZ ]; then
  252. mv -f $BackZ $BackY ;
  253. fi
  254. done
  255. cp -la $Cache $Back ; mv -f $BackX $Back0
  256. update_stamp $BACKUP_ARCHIVE_STAMP
  257. debug_echo "backup with hardlinks. (success)"
  258. else
  259. debug_echo "skip backup since same content."
  260. fi
  261. else
  262. debug_echo "skip backup since too new."
  263. fi
  264. }
  265. # sleep for a random interval of time (default 30min)
  266. # (some code taken from cron-apt, thanks)
  267. random_sleep()
  268. {
  269. RandomSleep=1800
  270. eval $(apt-config shell RandomSleep APT::Periodic::RandomSleep)
  271. if [ $RandomSleep -eq 0 ]; then
  272. return
  273. fi
  274. if [ -z "$RANDOM" ] ; then
  275. # A fix for shells that do not have this bash feature.
  276. RANDOM=$(( $(dd if=/dev/urandom bs=2 count=1 2> /dev/null | cksum | cut -d' ' -f1) % 32767 ))
  277. fi
  278. TIME=$(($RANDOM % $RandomSleep))
  279. debug_echo "sleeping for $TIME seconds"
  280. sleep $TIME
  281. }
  282. debug_echo()
  283. {
  284. # Display message if $VERBOSE >= 1
  285. if [ "$VERBOSE" -ge 1 ]; then
  286. echo $1 1>&2
  287. fi
  288. }
  289. check_power(){
  290. # laptop check, on_ac_power returns:
  291. # 0 (true) System is on main power
  292. # 1 (false) System is not on main power
  293. # 255 (false) Power status could not be determined
  294. # Desktop systems always return 255 it seems
  295. if which on_ac_power >/dev/null; then
  296. on_ac_power
  297. POWER=$?
  298. if [ $POWER -eq 1 ]; then
  299. debug_echo "exit: system NOT on main power"
  300. return 1
  301. elif [ $POWER -ne 0 ]; then
  302. debug_echo "power status ($POWER) undetermined, continuing"
  303. fi
  304. debug_echo "system is on main power."
  305. fi
  306. return 0
  307. }
  308. # ------------------------ main ----------------------------
  309. if test -r /var/lib/apt/extended_states; then
  310. # Backup the 7 last versions of APT's extended_states file
  311. # shameless copy from dpkg cron
  312. if cd /var/backups ; then
  313. if ! cmp -s apt.extended_states.0 /var/lib/apt/extended_states; then
  314. cp -p /var/lib/apt/extended_states apt.extended_states
  315. savelog -c 7 apt.extended_states >/dev/null
  316. fi
  317. fi
  318. fi
  319. # check apt-config existence
  320. if ! which apt-config >/dev/null ; then
  321. exit 0
  322. fi
  323. # check if the user really wants to do something
  324. AutoAptEnable=1 # default is yes
  325. eval $(apt-config shell AutoAptEnable APT::Periodic::Enable)
  326. if [ $AutoAptEnable -eq 0 ]; then
  327. exit 0
  328. fi
  329. # Set VERBOSE mode from apt-config (or inherit from environment)
  330. VERBOSE=0
  331. eval $(apt-config shell VERBOSE APT::Periodic::Verbose)
  332. debug_echo "verbose level $VERBOSE"
  333. if [ "$VERBOSE" -le 2 ]; then
  334. # quiet for 0,1,2
  335. XSTDOUT=">/dev/null"
  336. XSTDERR="2>/dev/null"
  337. XAPTOPT="-qq"
  338. XUUPOPT=""
  339. else
  340. XSTDOUT=""
  341. XSTDERR=""
  342. XAPTOPT=""
  343. XUUPOPT="-d"
  344. fi
  345. if [ "$VERBOSE" -ge 3 ]; then
  346. # trace output
  347. set -x
  348. fi
  349. check_power || exit 0
  350. # check if we can lock the cache and if the cache is clean
  351. if which apt-get >/dev/null && ! eval apt-get check $XAPTOPT $XSTDERR ; then
  352. debug_echo "error encountered in cron job with \"apt-get check\"."
  353. exit 0
  354. fi
  355. # Global current time in seconds since 1970-01-01 00:00:00 UTC
  356. now=$(date +%s)
  357. # Support old Archive for compatibility.
  358. # Document only Periodic for all controlling parameters of this script.
  359. UpdateInterval=0
  360. eval $(apt-config shell UpdateInterval APT::Periodic::Update-Package-Lists)
  361. DownloadUpgradeableInterval=0
  362. eval $(apt-config shell DownloadUpgradeableInterval APT::Periodic::Download-Upgradeable-Packages)
  363. UnattendedUpgradeInterval=0
  364. eval $(apt-config shell UnattendedUpgradeInterval APT::Periodic::Unattended-Upgrade)
  365. AutocleanInterval=0
  366. eval $(apt-config shell AutocleanInterval APT::Periodic::AutocleanInterval)
  367. BackupArchiveInterval=0
  368. eval $(apt-config shell BackupArchiveInterval APT::Periodic::BackupArchiveInterval)
  369. Debdelta=1
  370. eval $(apt-config shell Debdelta APT::Periodic::Download-Upgradeable-Packages-Debdelta)
  371. # check if we actually have to do anything that requires locking the cache
  372. if [ $UpdateInterval -eq 0 ] &&
  373. [ $DownloadUpgradeableInterval -eq 0 ] &&
  374. [ $UnattendedUpgradeInterval -eq 0 ] &&
  375. [ $BackupArchiveInterval -eq 0 ] &&
  376. [ $AutocleanInterval -eq 0 ]; then
  377. # check cache size
  378. check_size_constraints
  379. exit 0
  380. fi
  381. # deal with BackupArchiveInterval
  382. do_cache_backup $BackupArchiveInterval
  383. # sleep random amount of time to avoid hitting the
  384. # mirrors at the same time
  385. random_sleep
  386. check_power || exit 0
  387. # include default system language so that "apt-get update" will
  388. # fetch the right translated package descriptions
  389. if [ -r /etc/default/locale ]; then
  390. . /etc/default/locale
  391. export LANG LANGUAGE LC_MESSAGES LC_ALL
  392. fi
  393. # update package lists
  394. UPDATED=0
  395. UPDATE_STAMP=/var/lib/apt/periodic/update-stamp
  396. if check_stamp $UPDATE_STAMP $UpdateInterval; then
  397. if eval apt-get $XAPTOPT -y update $XSTDERR; then
  398. debug_echo "download updated metadata (success)."
  399. if which dbus-send >/dev/null && pidof dbus-daemon >/dev/null; then
  400. if dbus-send --system / app.apt.dbus.updated boolean:true ; then
  401. debug_echo "send dbus signal (success)"
  402. else
  403. debug_echo "send dbus signal (error)"
  404. fi
  405. else
  406. debug_echo "dbus signal not send (command not available)"
  407. fi
  408. update_stamp $UPDATE_STAMP
  409. UPDATED=1
  410. else
  411. debug_echo "download updated metadata (error)"
  412. fi
  413. else
  414. debug_echo "download updated metadata (not run)."
  415. fi
  416. # download all upgradeable packages (if it is requested)
  417. DOWNLOAD_UPGRADEABLE_STAMP=/var/lib/apt/periodic/download-upgradeable-stamp
  418. if [ $UPDATED -eq 1 ] && check_stamp $DOWNLOAD_UPGRADEABLE_STAMP $DownloadUpgradeableInterval; then
  419. if [ $Debdelta -eq 1 ]; then
  420. debdelta-upgrade >/dev/null 2>&1 || true
  421. fi
  422. if eval apt-get $XAPTOPT -y -d dist-upgrade $XSTDERR; then
  423. update_stamp $DOWNLOAD_UPGRADEABLE_STAMP
  424. debug_echo "download upgradable (success)"
  425. else
  426. debug_echo "download upgradable (error)"
  427. fi
  428. else
  429. debug_echo "download upgradable (not run)"
  430. fi
  431. # auto upgrade all upgradeable packages
  432. UPGRADE_STAMP=/var/lib/apt/periodic/upgrade-stamp
  433. if which unattended-upgrade >/dev/null && check_stamp $UPGRADE_STAMP $UnattendedUpgradeInterval; then
  434. if unattended-upgrade $XUUPOPT; then
  435. update_stamp $UPGRADE_STAMP
  436. debug_echo "unattended-upgrade (success)"
  437. else
  438. debug_echo "unattended-upgrade (error)"
  439. fi
  440. else
  441. debug_echo "unattended-upgrade (not run)"
  442. fi
  443. # autoclean package archive
  444. AUTOCLEAN_STAMP=/var/lib/apt/periodic/autoclean-stamp
  445. if check_stamp $AUTOCLEAN_STAMP $AutocleanInterval; then
  446. if eval apt-get $XAPTOPT -y autoclean $XSTDERR; then
  447. debug_echo "autoclean (success)."
  448. update_stamp $AUTOCLEAN_STAMP
  449. else
  450. debug_echo "autoclean (error)"
  451. fi
  452. else
  453. debug_echo "autoclean (not run)"
  454. fi
  455. # check cache size
  456. check_size_constraints
  457. #
  458. # vim: set sts=4 ai :
  459. #