apt.cron.daily 14 KB

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