]> git.saurik.com Git - apt.git/blame - debian/apt.cron.daily
* apt-pkg/pkgcachegen.cc:
[apt.git] / debian / apt.cron.daily
CommitLineData
0c132682 1#!/bin/sh
0c132682 2#set -e
8a139d4d
MV
3#
4# This file understands the following apt configuration variables:
ff7c76f8
OA
5# Values here are the default.
6# Create /etc/apt/apt.conf.d/02periodic file to set your preference.
8a139d4d 7#
ff7c76f8
OA
8# Dir "/";
9# - RootDir for all configuration files
8a139d4d 10#
ff7c76f8
OA
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::BackupArchiveInterval "0";
18# - Backup after n-days if archive contents changed.(0=disable)
19#
20# APT::Periodic::BackupLevel "3";
21# - Backup level.(0=disable), 1 is invalid.
22#
23# Dir::Cache::Backup "backup/";
24# - Set periodic package backup directory
25#
26# APT::Archives::MaxAge "0"; (old, deprecated)
27# APT::Periodic::MaxAge "0"; (new)
8a139d4d
MV
28# - Set maximum allowed age of a cache package file. If a cache
29# package file is older it is deleted (0=disable)
30#
ff7c76f8
OA
31# APT::Archives::MinAge "2"; (old, deprecated)
32# APT::Periodic::MinAge "2"; (new)
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# APT::Archives::MaxSize "0"; (old, deprecated)
38# APT::Periodic::MaxSize "0"; (new)
8a139d4d
MV
39# - Set maximum size of the cache in MB (0=disable). If the cache
40# is bigger, cached package files are deleted until the size
41# requirement is met (the biggest packages will be deleted
42# first).
43#
ff7c76f8
OA
44# APT::Periodic::Update-Package-Lists "0";
45# - Do "apt-get update" automatically every n-days (0=disable)
46#
47# APT::Periodic::Download-Upgradeable-Packages "0";
48# - Do "apt-get upgrade --download-only" every n-days (0=disable)
49#
50# APT::Periodic::Unattended-Upgrade "0";
51# - Run the "unattended-upgrade" security upgrade script
52# every n-days (0=disabled)
53# Requires the package "unattended-upgrades" and will write
54# a log in /var/log/unattended-upgrades
8a139d4d 55#
ff7c76f8
OA
56# APT::Periodic::AutocleanInterval "0";
57# - Do "apt-get autoclean" every n-days (0=disable)
58#
59# APT::Periodic::Verbose "0";
60# - Send report mail to root
61# 0: no report (or null string)
62# 1: progress report (actually any string)
63# 2: + command outputs (remove -qq, remove 2>/dev/null, add -d)
64# 3: + trace on
0c132682 65
05f6a46a 66check_stamp()
0c132682 67{
05f6a46a
MZ
68 stamp="$1"
69 interval="$2"
70
10946ddc 71 if [ $interval -eq 0 ]; then
ff7c76f8
OA
72 debug_echo "check_stamp: interval=0."
73 # treat as no time has passed
10946ddc
MZ
74 return 1
75 fi
76
05f6a46a 77 if [ ! -f $stamp ]; then
ff7c76f8
OA
78 update_stamp $stamp
79 debug_echo "check_stamp: missing time stamp file: $stamp."
80 # treat as enough time has passed
05f6a46a 81 return 0
0c132682 82 fi
05f6a46a
MZ
83
84 # compare midnight today to midnight the day the stamp was updated
ff7c76f8 85 stamp=$(date -r $stamp '+%s')
05f6a46a 86 delta=$(($now-$stamp))
05f6a46a 87
ff7c76f8 88 # intervall is in days, convert to sec.
8a139d4d 89 interval=$(($interval*60*60*24))
ff7c76f8 90 debug_echo "check_stamp: interval=$interval, now=$now, stamp=$stamp, delta=$delta (sec)"
8a139d4d 91
05f6a46a
MZ
92 if [ $delta -ge $interval ]; then
93 return 0
94 fi
95
96 return 1
97}
98
99update_stamp()
100{
101 stamp="$1"
05f6a46a 102 touch $stamp
0c132682
MZ
103}
104
ff7c76f8 105debug_echo()
6cce801a 106{
ff7c76f8
OA
107 # Display message if $VERBOSE >= 1
108 if [ "$VERBOSE" -ge 1 ]; then
109 echo $1 1>&2
6cce801a 110 fi
ff7c76f8
OA
111}
112
113# check apt-config exstance
114if ! which apt-config >/dev/null ; then
115 exit 0
116fi
6cce801a 117
ff7c76f8
OA
118# Set VERBOSE mode from apt-config (or inherit from environment)
119eval $(apt-config shell VERBOSE APT::Periodic::Verbose)
120if [ -z "$VERBOSE" ]; then
121 VERBOSE="0"
122fi
123if [ "$VERBOSE" -le 2 ]; then
124 # quiet for 0,1,2
125 XSTDOUT=">/dev/null"
126 XSTDERR="2>/dev/null"
127 XAPTOPT="-qq"
128 XUUPOPT=""
129else
130 XSTDOUT=""
131 XSTDERR=""
132 XAPTOPT=""
133 XUUPOPT="-d"
134fi
135if [ "$VERBOSE" -ge 3 ]; then
136 # trace output
137 set -x
138fi
8a139d4d 139
ff7c76f8
OA
140# laptop check, on_ac_power returns:
141# 0 (true) System is on main power
142# 1 (false) System is not on main power
143# 255 (false) Power status could not be determined
144# Desktop systems always return 255 it seems
145if which on_ac_power >/dev/null; then
146 on_ac_power
147 POWER=$?
148 if [ $POWER -eq 1 ]; then
149 debug_echo "exit: system on main power."
150 exit 0
151 elif [ $POWER -ne 0 ]; then
152 debug_echo "exit: power status ($POWER) undetermined."
153 exit 0
6cce801a 154 fi
ff7c76f8
OA
155 debug_echo "system is on main power."
156fi
8a139d4d 157
ff7c76f8
OA
158# check if we can lock the cache and if the cache is clean
159if which apt-get >/dev/null && ! eval apt-get check $XAPTOPT $XSTDERR ; then
160 debug_echo "error encountered in cron job with \"apt-get check\"."
161 exit 0
162fi
163# No need to check for apt-get below
8a139d4d 164
ff7c76f8
OA
165# Global current time in seconds since 1970-01-01 00:00:00 UTC
166now=$(date +%s)
6cce801a 167
ff7c76f8
OA
168# Set default values and normalize
169Dir="/"
170eval $(apt-config shell Dir Dir)
171Dir=${Dir%/}
172
173CacheDir="var/cache/apt/"
174eval $(apt-config shell CacheDir Dir::Cache)
175CacheDir=${CacheDir%/}
176if [ -z "$CacheDir" ]; then
177 debug_echo "practically empty Dir::Cache, exiting"
178 exit 0
179fi
69c28efc 180
ff7c76f8
OA
181CacheArchive="archives/"
182eval $(apt-config shell CacheArchive Dir::Cache::Archives)
183CacheArchive=${CacheArchive%/}
184if [ -z "$CacheArchive" ]; then
185 debug_echo "practically empty Dir::Cache::archives, exiting"
186 exit 0
187fi
69c28efc 188
ff7c76f8
OA
189BackupArchiveInterval=0
190eval $(apt-config shell BackupArchiveInterval APT::Periodic::BackupArchiveInterval)
191
c7560b77
MV
192BackupLevel=3
193eval $(apt-config shell BackupLevel APT::Periodic::BackupLevel)
194if [ $BackupLevel -le 1 ]; then BackupLevel=2 ; fi
ff7c76f8
OA
195
196CacheBackup="backup/"
197eval $(apt-config shell CacheBackup Dir::Cache::Backup)
198CacheBackup=${CacheBackup%/}
199if [ -z "$CacheBackup" ]; then
200 echo "practically empty Dir::Cache::Backup, exiting" 1>&2
201 exit 0
18d38975 202fi
87ddfb96 203
ff7c76f8
OA
204# Support old Archive for compatibility.
205# Document only Periodic for all controling parameters of this script.
206MaxAge=0
207eval $(apt-config shell MaxAge APT::Archives::MaxAge)
208eval $(apt-config shell MaxAge APT::Periodic::MaxAge)
209
210MinAge=2
211eval $(apt-config shell MinAge APT::Archives::MinAge)
212eval $(apt-config shell MinAge APT::Periodic::MinAge)
213
214MaxSize=0
215eval $(apt-config shell MaxSize APT::Archives::MaxSize)
216eval $(apt-config shell MaxSize APT::Periodic::MaxSize)
217
0c132682 218UpdateInterval=0
ff7c76f8
OA
219eval $(apt-config shell UpdateInterval APT::Periodic::Update-Package-Lists)
220
05f6a46a 221DownloadUpgradeableInterval=0
ff7c76f8 222eval $(apt-config shell DownloadUpgradeableInterval APT::Periodic::Download-Upgradeable-Packages)
0c132682 223
fdd15654
MV
224UnattendedUpgradeInterval=0
225eval $(apt-config shell UnattendedUpgradeInterval APT::Periodic::Unattended-Upgrade)
226
ff7c76f8
OA
227AutocleanInterval=0
228eval $(apt-config shell AutocleanInterval APT::Periodic::AutocleanInterval)
229
230Cache="${Dir}/${CacheDir}/${CacheArchive}/"
231Back="${Dir}/${CacheDir}/${CacheBackup}/"
232BackX="${Back}${CacheArchive}/"
233for x in $(seq 0 1 $((${BackupLevel}-1))); do
234 eval "Back${x}=${Back}${x}/"
235done
236
4c2dcaa1
MV
237# check if we actually have to do anything
238if [ $UpdateInterval -eq 0 ] &&
239 [ $DownloadUpgradeableInterval -eq 0 ] &&
240 [ $UnattendedUpgradeInterval -eq 0 ] &&
2783b261 241 [ $BackupArchiveInterval -eq 0 ] &&
4c2dcaa1
MV
242 [ $AutocleanInterval -eq 0 ]; then
243 exit 0
244fi
fdd15654 245
0c132682 246
ff7c76f8
OA
247# backup after n-days if archive contents changed.
248# (This uses hardlink to save disk space)
249BACKUP_ARCHIVE_STAMP=/var/lib/apt/periodic/backup-archive-stamp
250if check_stamp $BACKUP_ARCHIVE_STAMP $BackupArchiveInterval; then
251 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
252 mkdir -p $Back
253 rm -rf $Back$((${BackupLevel}-1))
254 for y in $(seq $((${BackupLevel}-1)) -1 1); do
255 eval BackY=${Back}$y
256 eval BackZ=${Back}$(($y-1))
257 if [ -e $BackZ ]; then mv -f $BackZ $BackY ; fi
258 done
259 cp -la $Cache $Back ; mv -f $BackX $Back0
260 update_stamp $BACKUP_ARCHIVE_STAMP
261 debug_echo "backup with hardlinks. (success)"
262 else
fdd15654 263
ff7c76f8 264 debug_echo "skip backup since same content."
0c132682 265 fi
ff7c76f8
OA
266else
267 debug_echo "skip backup since too new."
0c132682
MZ
268fi
269
ff7c76f8
OA
270# package archive contnts removal by package age
271if [ $MaxAge -ne 0 ] && [ $MinAge -ne 0 ]; then
272 find $Cache -name "*.deb" \( -mtime +$MaxAge -and -ctime +$MaxAge \) -and -not \( -mtime -$MinAge -or -ctime -$MinAge \) -print0 | xargs -r -0 rm -f
273 debug_echo "aged: ctime <$MaxAge and mtime <$MaxAge and ctime>$MinAge and mtime>$MinAge"
274elif [ $MaxAge -ne 0 ]; then
275 find $Cache -name "*.deb" -ctime +$MaxAge -and -mtime +$MaxAge -print0 | xargs -r -0 rm -f
276 debug_echo "aged: ctime <$MaxAge and mtime <$MaxAge only"
277else
278 debug_echo "skip aging since MaxAge is 0"
e15dcd38 279fi
ff7c76f8
OA
280
281# package archive contnts removal down to $MaxSize
282if [ $MaxSize -ne 0 ]; then
283
284 MinAgeSec=$(($MinAge*24*60*60))
285
286 # reverse-sort by mtime
287 for file in $(ls -rt $Cache/*.deb 2>/dev/null); do
288 du=$(du -m -s $Cache)
289 size=${du%%/*}
290 # check if the cache is small enough
291 if [ $size -lt $MaxSize ]; then
292 debug_echo "end remove by archive size: size=$size < $MaxSize"
293 break
294 fi
e15dcd38 295
ff7c76f8
OA
296 # check for MinAge in second of the file
297 if [ $MinAgeSec -ne 0 ]; then
298 # check both ctime and mtime
299 mtime=$(stat -c %Y $file)
300 ctime=$(stat -c %Z $file)
301 if [ $mtime -gt $ctime ]; then
302 delta=$(($now-$mtime))
303 else
304 delta=$(($now-$ctime))
305 fi
306 if [ $delta -le $MinAgeSec ]; then
307 debug_echo "skip remove by archive size: $file, delta=$delta < $MinAgeSec"
308 else
309 # delete oldest file
310 debug_echo "remove by archive size: $file, delta=$delta >= $MinAgeSec (sec), size=$size >= $MaxSize"
311 rm -f $file
312 fi
313 fi
69c28efc 314
ff7c76f8 315 done
69c28efc 316fi
e15dcd38 317
ff7c76f8 318# update package lists
05f6a46a
MZ
319UPDATE_STAMP=/var/lib/apt/periodic/update-stamp
320if check_stamp $UPDATE_STAMP $UpdateInterval; then
ff7c76f8
OA
321 if eval apt-get $XAPTOPT -y update $XSTDERR; then
322 debug_echo "download updated metadata (success)."
be993931 323 if which dbus-send >/dev/null && pidof dbus-daemon >/dev/null; then
ff7c76f8
OA
324 if dbus-send --system / app.apt.dbus.updated boolean:true ; then
325 debug_echo "send dbus signal (success)"
326 else
327 debug_echo "send dbus signal (error)"
328 fi
329 else
330 debug_echo "dbus signal not send (command not available)"
05f6a46a 331 fi
ff7c76f8
OA
332 update_stamp $UPDATE_STAMP
333 # download all upgradeable packages if it is requested
334 DOWNLOAD_UPGRADEABLE_STAMP=/var/lib/apt/periodic/download-upgradeable-stamp
335 if check_stamp $DOWNLOAD_UPGRADEABLE_STAMP $DownloadUpgradeableInterval; then
336 if eval apt-get $XAPTOPT -y -d dist-upgrade $XSTDERR; then
337 update_stamp $DOWNLOAD_UPGRADEABLE_STAMP
338 debug_echo "download upgradable (success)."
339 # auto upgrade all upgradeable packages
340 UPGRADE_STAMP=/var/lib/apt/periodic/upgrade-stamp
341 if which unattended-upgrade >/dev/null && check_stamp $UPGRADE_STAMP $UnattendedUpgradeInterval; then
342 if unattended-upgrade $XUUPOPT; then
343 update_stamp $UPGRADE_STAMP
344 debug_echo "unattended-upgrade (success)."
345 else
346 debug_echo "unattended-upgrade (error)."
347 fi
348 else
349 debug_echo "unattended-upgrade (not run)."
350 fi
351 else
352 debug_echo "download upgradable (error)."
353 fi
354 else
355 debug_echo "download upgradable (not run)."
356 fi
357 else
358 debug_echo "download updated metadata (error)."
05f6a46a 359 fi
ff7c76f8
OA
360else
361 debug_echo "download updated metadata (not run)."
0c132682
MZ
362fi
363
ff7c76f8 364# autoclean package archive
de15fbae
MV
365AUTOCLEAN_STAMP=/var/lib/apt/periodic/autoclean-stamp
366if check_stamp $AUTOCLEAN_STAMP $AutocleanInterval; then
ff7c76f8
OA
367 if apt-get $XAPTOPT -y autoclean $XSTDERR; then
368 debug_echo "autoclean (success)."
369 update_stamp $AUTOCLEAN_STAMP
370 else
371 debug_echo "autoclean (error)."
372 fi
373else
374 debug_echo "autoclean (not run)."
de15fbae
MV
375fi
376
ff7c76f8
OA
377#
378# vim: set sts=4 ai :
379#
380