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.
9 # - RootDir for all configuration files
11 # Dir::Cache "var/apt/cache/";
12 # - Set apt package cache directory
14 # Dir::Cache::Archive "archives/";
15 # - Set package archive directory
17 # APT::Periodic::BackupArchiveInterval "0";
18 # - Backup after n-days if archive contents changed.(0=disable)
20 # APT::Periodic::BackupLevel "3";
21 # - Backup level.(0=disable), 1 is invalid.
23 # Dir::Cache::Backup "backup/";
24 # - Set periodic package backup directory
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)
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.
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
44 # APT::Periodic::Update-Package-Lists "0";
45 # - Do "apt-get update" automatically every n-days (0=disable)
47 # APT::Periodic::Download-Upgradeable-Packages "0";
48 # - Do "apt-get upgrade --download-only" every n-days (0=disable)
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
56 # APT::Periodic::AutocleanInterval "0";
57 # - Do "apt-get autoclean" every n-days (0=disable)
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)
71 if [ $interval -eq 0 ]; then
72 debug_echo
"check_stamp: interval=0."
73 # treat as no time has passed
77 if [ ! -f $stamp ]; then
78 debug_echo
"check_stamp: missing time stamp file: $stamp."
79 # treat as enough time has passed
83 # compare midnight today to midnight the day the stamp was updated
84 stamp
=$(date -r $stamp '+%s')
85 delta
=$(($now-$stamp))
87 # intervall is in days, convert to sec.
88 interval
=$(($interval*60*60*24))
89 debug_echo
"check_stamp: interval=$interval, now=$now, stamp=$stamp, delta=$delta (sec)"
91 if [ $delta -ge $interval ]; then
104 # we check here if autoclean was enough sizewise
105 check_size_constraints
()
108 eval $(apt-config shell MaxAge APT::Archives::MaxAge)
109 eval $(apt-config shell MaxAge APT::Periodic::MaxAge)
112 eval $(apt-config shell MinAge APT::Archives::MinAge)
113 eval $(apt-config shell MinAge APT::Periodic::MinAge)
116 eval $(apt-config shell MaxSize APT::Archives::MaxSize)
117 eval $(apt-config shell MaxSize APT::Periodic::MaxSize)
119 CacheDir
="var/cache/apt/"
120 eval $(apt-config shell CacheDir Dir::Cache)
121 CacheDir
=${CacheDir%/}
123 CacheArchive
="archives/"
124 eval $(apt-config shell CacheArchive Dir::Cache::archives)
125 CacheArchive
=${CacheArchive%/}
128 if [ -z "$CacheDir" -o -z "$CacheArchive" ]; then
129 echo "empty Dir::Cache or Dir::Cache::archives, exiting"
133 Cache
="${Dir%/}/${CacheDir%/}/${CacheArchive%/}/"
136 if [ ! $MaxAge -eq 0 ] && [ ! $MinAge -eq 0 ]; then
137 debug_echo
"aged: ctime <$MaxAge and mtime <$MaxAge and ctime>$MinAge and mtime>$MinAge"
138 find $Cache -name "*.deb" \
( -mtime +$MaxAge -and -ctime +$MaxAge \
) -and -not \
( -mtime -$MinAge -or -ctime -$MinAge \
) -print0 | xargs -r -0 rm -f
139 elif [ ! $MaxAge -eq 0 ]; then
140 debug_echo
"aged: ctime <$MaxAge and mtime <$MaxAge only"
141 find $Cache -name "*.deb" -ctime +$MaxAge -and -mtime +$MaxAge -print0 | xargs -r -0 rm -f
143 debug_echo
"skip aging since MaxAge is 0"
147 if [ ! $MaxSize -eq 0 ]; then
149 MaxSize
=$(($MaxSize*1024))
152 now
=$(date --date=$(date --iso-8601) +%s
)
153 MinAge
=$(($MinAge*24*60*60))
155 # reverse-sort by mtime
156 for file in $(ls -rt $Cache/*.deb 2>/dev/null); do
159 # check if the cache is small enough
160 if [ $size -lt $MaxSize ]; then
161 debug_echo
"end remove by archive size: size=$size < $MaxSize"
165 # check for MinAge of the file
166 if [ $MinAge -ne 0 ]; then
167 # check both ctime and mtime
168 mtime
=$(stat -c %Y $file)
169 ctime
=$(stat -c %Z $file)
170 if [ $mtime -gt $ctime ]; then
171 delta
=$(($now-$mtime))
173 delta
=$(($now-$ctime))
175 if [ $delta -le $MinAge ]; then
176 debug_echo
"skip remove by archive size: $file, delta=$delta < $MinAgeSec"
180 debug_echo
"remove by archive size: $file, delta=$delta >= $MinAgeSec (sec), size=$size >= $MaxSize"
188 # deal with the Apt::Periodic::BackupArchiveInterval
191 BackupArchiveInterval
="$1"
192 if [ $BackupArchiveInterval -eq 0 ]; then
196 # Set default values and normalize
198 eval $(apt-config shell Dir Dir)
201 CacheDir
="var/cache/apt/"
202 eval $(apt-config shell CacheDir Dir::Cache)
203 CacheDir
=${CacheDir%/}
204 if [ -z "$CacheDir" ]; then
205 debug_echo
"practically empty Dir::Cache, exiting"
209 CacheArchive
="archives/"
210 eval $(apt-config shell CacheArchive Dir::Cache::Archives)
211 CacheArchive
=${CacheArchive%/}
212 if [ -z "$CacheArchive" ]; then
213 debug_echo
"practically empty Dir::Cache::archives, exiting"
218 eval $(apt-config shell BackupLevel APT::Periodic::BackupLevel)
219 if [ $BackupLevel -le 1 ]; then
223 CacheBackup
="backup/"
224 eval $(apt-config shell CacheBackup Dir::Cache::Backup)
225 CacheBackup
=${CacheBackup%/}
226 if [ -z "$CacheBackup" ]; then
227 echo "practically empty Dir::Cache::Backup, exiting" 1>&2
231 Cache
="${Dir}/${CacheDir}/${CacheArchive}/"
232 Back
="${Dir}/${CacheDir}/${CacheBackup}/"
233 BackX
="${Back}${CacheArchive}/"
234 for x
in $(seq 0 1 $((${BackupLevel}-1))); do
235 eval "Back${x}=${Back}${x}/"
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
244 rm -rf $Back$((${BackupLevel}-1))
245 for y
in $(seq $((${BackupLevel}-1)) -1 1); do
247 eval BackZ
=${Back}$(($y-1))
248 if [ -e $BackZ ]; then
249 mv -f $BackZ $BackY ;
252 cp -la $Cache $Back ; mv -f $BackX $Back0
253 update_stamp
$BACKUP_ARCHIVE_STAMP
254 debug_echo
"backup with hardlinks. (success)"
256 debug_echo
"skip backup since same content."
259 debug_echo
"skip backup since too new."
263 # sleep for a random interval of time (default 30min)
264 # (some code taken from cron-apt, thanks)
268 eval $(apt-config shell RandomSleep APT::Periodic::RandomSleep)
269 if [ $RandomSleep -eq 0 ]; then
272 if [ -z "$RANDOM" ] ; then
273 # A fix for shells that do not have this bash feature.
274 RANDOM
=$(dd if=/dev/urandom count=1 2> /dev/null | cksum | cut -c"1-5")
276 TIME
=$(($RANDOM % $RandomSleep))
277 debug_echo
"sleeping for $TIME seconds"
284 # Display message if $VERBOSE >= 1
285 if [ "$VERBOSE" -ge 1 ]; then
290 # ------------------------ main ----------------------------
292 # check apt-config exstance
293 if ! which apt
-config >/dev
/null
; then
297 # Set VERBOSE mode from apt-config (or inherit from environment)
298 eval $(apt-config shell VERBOSE APT::Periodic::Verbose)
299 debug_echo
"verbose level $VERBOSE"
300 if [ -z "$VERBOSE" ]; then
303 if [ "$VERBOSE" -le 2 ]; then
306 XSTDERR
="2>/dev/null"
315 if [ "$VERBOSE" -ge 3 ]; then
320 # laptop check, on_ac_power returns:
321 # 0 (true) System is on main power
322 # 1 (false) System is not on main power
323 # 255 (false) Power status could not be determined
324 # Desktop systems always return 255 it seems
325 if which on_ac_power
>/dev
/null
; then
328 if [ $POWER -eq 1 ]; then
329 debug_echo
"exit: system NOT on main power"
331 elif [ $POWER -ne 0 ]; then
332 debug_echo
"power status ($POWER) undetermined, continuing"
334 debug_echo
"system is on main power."
337 # check if we can lock the cache and if the cache is clean
338 if which apt
-get >/dev
/null
&& ! eval apt
-get check
$XAPTOPT $XSTDERR ; then
339 debug_echo
"error encountered in cron job with \"apt-get check\"."
343 # Global current time in seconds since 1970-01-01 00:00:00 UTC
346 # Support old Archive for compatibility.
347 # Document only Periodic for all controling parameters of this script.
350 eval $(apt-config shell UpdateInterval APT::Periodic::Update-Package-Lists)
352 DownloadUpgradeableInterval
=0
353 eval $(apt-config shell DownloadUpgradeableInterval APT::Periodic::Download-Upgradeable-Packages)
355 UnattendedUpgradeInterval
=0
356 eval $(apt-config shell UnattendedUpgradeInterval APT::Periodic::Unattended-Upgrade)
359 eval $(apt-config shell AutocleanInterval APT::Periodic::AutocleanInterval)
361 BackupArchiveInterval
=0
362 eval $(apt-config shell BackupArchiveInterval APT::Periodic::BackupArchiveInterval)
364 # check if we actually have to do anything
365 if [ $UpdateInterval -eq 0 ] &&
366 [ $DownloadUpgradeableInterval -eq 0 ] &&
367 [ $UnattendedUpgradeInterval -eq 0 ] &&
368 [ $BackupArchiveInterval -eq 0 ] &&
369 [ $AutocleanInterval -eq 0 ]; then
373 # deal with BackupArchiveInterval
374 do_cache_backup
$BackupArchiveInterval
376 # sleep random amount of time to avoid hitting the
377 # mirrors at the same time
380 # update package lists
381 UPDATE_STAMP
=/var
/lib
/apt
/periodic
/update
-stamp
382 if check_stamp
$UPDATE_STAMP $UpdateInterval; then
383 if eval apt
-get $XAPTOPT -y update
$XSTDERR; then
384 debug_echo
"download updated metadata (success)."
385 if which dbus
-send >/dev
/null
&& pidof dbus
-daemon >/dev
/null
; then
386 if dbus
-send --system / app.apt.dbus.updated boolean
:true
; then
387 debug_echo
"send dbus signal (success)"
389 debug_echo
"send dbus signal (error)"
392 debug_echo
"dbus signal not send (command not available)"
394 update_stamp
$UPDATE_STAMP
396 debug_echo
"download updated metadata (error)"
399 debug_echo
"download updated metadata (not run)."
402 # download all upgradeable packages (if it is requested)
403 DOWNLOAD_UPGRADEABLE_STAMP
=/var
/lib
/apt
/periodic
/download
-upgradeable-stamp
404 if check_stamp
$DOWNLOAD_UPGRADEABLE_STAMP $DownloadUpgradeableInterval; then
405 if eval apt
-get $XAPTOPT -y -d dist
-upgrade $XSTDERR; then
406 update_stamp
$DOWNLOAD_UPGRADEABLE_STAMP
407 debug_echo
"download upgradable (success)"
409 debug_echo
"download upgradable (error)"
412 debug_echo
"download upgradable (not run)"
415 # auto upgrade all upgradeable packages
416 UPGRADE_STAMP
=/var
/lib
/apt
/periodic
/upgrade
-stamp
417 if which unattended
-upgrade >/dev
/null
&& check_stamp
$UPGRADE_STAMP $UnattendedUpgradeInterval; then
418 if unattended
-upgrade $XUUPOPT; then
419 update_stamp
$UPGRADE_STAMP
420 debug_echo
"unattended-upgrade (success)"
422 debug_echo
"unattended-upgrade (error)"
425 debug_echo
"unattended-upgrade (not run)"
428 # autoclean package archive
429 AUTOCLEAN_STAMP
=/var
/lib
/apt
/periodic
/autoclean
-stamp
430 if check_stamp
$AUTOCLEAN_STAMP $AutocleanInterval; then
431 if eval apt
-get $XAPTOPT -y autoclean
$XSTDERR; then
432 debug_echo
"autoclean (success)."
433 update_stamp
$AUTOCLEAN_STAMP
435 debug_echo
"autoclean (error)"
438 debug_echo
"autoclean (not run)"
442 check_size_constraints
445 # vim: set sts=4 ai :