apt.cron.daily 16 KB

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