apt.systemd.daily 16 KB

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