apt.cron.daily 15 KB

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