apt.cron.daily 14 KB

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