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