]> git.saurik.com Git - apt.git/blame - debian/apt.cron.daily
merge with lp:~mvo/apt/debian-sid
[apt.git] / debian / apt.cron.daily
CommitLineData
0c132682
MZ
1#!/bin/sh
2#
0c132682
MZ
3
4#set -e
8a139d4d
MV
5#
6# This file understands the following apt configuration variables:
7#
8# "APT::Periodic::Update-Package-Lists=1"
9# - Do "apt-get update" automatically every n-days (0=disable)
10#
11# "APT::Periodic::Download-Upgradeable-Packages=0",
12# - Do "apt-get upgrade --download-only" every n-days (0=disable)
13#
14# "APT::Periodic::AutocleanInterval"
15# - Do "apt-get autoclean" every n-days (0=disable)
16#
fdd15654
MV
17# "APT::Periodic::Unattended-Upgrade"
18# - Run the "unattended-upgrade" security upgrade script
19# every n-days (0=disabled)
20# Requires the package "unattended-upgrades" and will write
21# a log in /var/log/unattended-upgrades
22#
8a139d4d
MV
23# "APT::Archives::MaxAge",
24# - Set maximum allowed age of a cache package file. If a cache
25# package file is older it is deleted (0=disable)
26#
27# "APT::Archives::MaxSize",
28# - Set maximum size of the cache in MB (0=disable). If the cache
29# is bigger, cached package files are deleted until the size
30# requirement is met (the biggest packages will be deleted
31# first).
32#
33# "APT::Archives::MinAge"
34# - Set minimum age of a package file. If a file is younger it
35# will not be deleted (0=disable). Usefull to prevent races
36# and to keep backups of the packages for emergency.
37#
0c132682 38
05f6a46a 39check_stamp()
0c132682 40{
05f6a46a
MZ
41 stamp="$1"
42 interval="$2"
43
10946ddc
MZ
44 if [ $interval -eq 0 ]; then
45 return 1
46 fi
47
05f6a46a
MZ
48 if [ ! -f $stamp ]; then
49 return 0
0c132682 50 fi
05f6a46a
MZ
51
52 # compare midnight today to midnight the day the stamp was updated
a7c526b6
MV
53 stamp_file="$stamp"
54 stamp=$(date --date=$(date -r $stamp_file --iso-8601) +%s 2>/dev/null)
55 if [ "$?" != "0" ]; then
56 # Due to some timezones returning 'invalid date' for midnight on
57 # certain dates (eg America/Sao_Paulo), if date returns with error
58 # remove the stamp file and return 0. See coreutils bug:
59 # http://lists.gnu.org/archive/html/bug-coreutils/2007-09/msg00176.html
60 rm -f "$stamp_file"
61 return 0
62 fi
63
64 now=$(date --date=$(date --iso-8601) +%s 2>/dev/null)
65 if [ "$?" != "0" ]; then
66 # As above, due to some timezones returning 'invalid date' for midnight
67 # on certain dates (eg America/Sao_Paulo), if date returns with error
68 # return 0.
69 return 0
70 fi
71
05f6a46a 72 delta=$(($now-$stamp))
05f6a46a 73
8a139d4d
MV
74 # intervall is in days,
75 interval=$(($interval*60*60*24))
76 #echo "stampfile: $1"
77 #echo "interval=$interval, now=$now, stamp=$stamp, delta=$delta"
78
6e7c6c3f
MV
79 # remove timestamps a day (or more) in the future and force re-check
80 if [ $stamp -gt $(($now+86400)) ]; then
81 echo "WARNING: file $stamp_file has a timestamp in the future: $stamp"
82 rm -f "$stamp_file"
83 return 0
84 fi
85
05f6a46a
MZ
86 if [ $delta -ge $interval ]; then
87 return 0
88 fi
89
90 return 1
91}
92
93update_stamp()
94{
95 stamp="$1"
96
97 touch $stamp
0c132682
MZ
98}
99
6cce801a
MV
100
101
102# we check here if autoclean was enough sizewise
2e8a92e5 103check_size_constraints()
6cce801a
MV
104{
105 # min-age in days
106 MaxAge=0
89eaeb44 107 MinAge=2
6cce801a
MV
108 MaxSize=0
109 CacheDir="var/cache/apt"
110 CacheArchive="archives/"
111 eval $(apt-config shell MaxAge APT::Archives::MaxAge)
8a139d4d 112 eval $(apt-config shell MinAge APT::Archives::MinAge)
6cce801a 113 eval $(apt-config shell MaxSize APT::Archives::MaxSize)
01717245 114 eval $(apt-config shell Dir Dir)
6cce801a
MV
115 eval $(apt-config shell CacheDir Dir::Cache)
116 eval $(apt-config shell CacheArchive Dir::Cache::archives)
117
118 # sanity check
119 if [ -z "$CacheDir" -o -z "$CacheArchive" ]; then
120 echo "empty Dir::Cache or Dir::Cache::archives, exiting"
121 exit
122 fi
01717245
MV
123
124 Cache="${Dir%/}/${CacheDir%/}/${CacheArchive%/}/"
6cce801a
MV
125
126 # check age
8a139d4d 127 if [ ! $MaxAge -eq 0 ] && [ ! $MinAge -eq 0 ]; then
8e29e348 128 find $Cache -name "*.deb" \( -mtime +$MaxAge -and -ctime +$MaxAge \) -and -not \( -mtime -$MinAge -or -ctime -$MinAge \) -print0 | xargs -r -0 rm -f
8a139d4d 129 elif [ ! $MaxAge -eq 0 ]; then
8e29e348 130 find $Cache -name "*.deb" -ctime +$MaxAge -and -mtime +$MaxAge -print0 | xargs -r -0 rm -f
6cce801a
MV
131 fi
132
133 # check size
134 if [ ! $MaxSize -eq 0 ]; then
8a139d4d
MV
135 # maxSize is in MB
136 MaxSize=$(($MaxSize*1024))
137
138 #get current time
139 now=$(date --date=$(date --iso-8601) +%s)
140 MinAge=$(($MinAge*24*60*60))
141
6cce801a 142 # reverse-sort by mtime
3408b58c 143 for file in $(ls -rt $Cache/*.deb 2>/dev/null); do
6cce801a
MV
144 du=$(du -s $Cache)
145 size=${du%%/*}
146 # check if the cache is small enough
147 if [ $size -lt $MaxSize ]; then
148 break
149 fi
8a139d4d
MV
150
151 # check for MinAge of the file
152 if [ ! $MinAge -eq 0 ]; then
8e29e348 153 # check both ctime and mtime
3971f8e8 154 mtime=$(stat -c %Y $file)
8e29e348
MV
155 ctime=$(stat -c %Z $file)
156 if [ $mtime -gt $ctime ]; then
157 delta=$(($now-$mtime))
158 else
159 delta=$(($now-$ctime))
160 fi
8a139d4d
MV
161 #echo "$file ($delta), $MinAge"
162 if [ $delta -le $MinAge ]; then
163 #echo "Skiping $file (delta=$delta)"
3408b58c 164 break
8a139d4d
MV
165 fi
166 fi
167
6cce801a
MV
168 # delete oldest file
169 rm -f $file
170 done
171 fi
172}
173
d047c6da 174# sleep for a random interval of time (default 30min)
69c28efc
MV
175# (some code taken from cron-apt, thanks)
176random_sleep()
177{
178 RandomSleep=1800
179 eval $(apt-config shell RandomSleep APT::Periodic::RandomSleep)
180 if [ $RandomSleep -eq 0 ]; then
181 return
182 fi
183 if [ -z "$RANDOM" ] ; then
184 # A fix for shells that do not have this bash feature.
185 RANDOM=$(dd if=/dev/urandom count=1 2> /dev/null | cksum | cut -c"1-5")
186 fi
187 TIME=$(($RANDOM % $RandomSleep))
188 sleep $TIME
189}
190
191# main
192
18d38975
OS
193if ! which apt-config >/dev/null; then
194 exit 0
195fi
87ddfb96 196
0c132682 197UpdateInterval=0
05f6a46a 198DownloadUpgradeableInterval=0
9bd1cf87
MZ
199eval $(apt-config shell UpdateInterval APT::Periodic::Update-Package-Lists DownloadUpgradeableInterval APT::Periodic::Download-Upgradeable-Packages)
200AutocleanInterval=$DownloadUpgradeableInterval
0eb7056b 201eval $(apt-config shell AutocleanInterval APT::Periodic::AutocleanInterval)
fdd15654
MV
202UnattendedUpgradeInterval=0
203eval $(apt-config shell UnattendedUpgradeInterval APT::Periodic::Unattended-Upgrade)
204
4c2dcaa1
MV
205# check if we actually have to do anything
206if [ $UpdateInterval -eq 0 ] &&
207 [ $DownloadUpgradeableInterval -eq 0 ] &&
208 [ $UnattendedUpgradeInterval -eq 0 ] &&
209 [ $AutocleanInterval -eq 0 ]; then
210 exit 0
211fi
fdd15654 212
0c132682
MZ
213# laptop check, on_ac_power returns:
214# 0 (true) System is on mains power
215# 1 (false) System is not on mains power
216# 255 (false) Power status could not be determined
217# Desktop systems always return 255 it seems
2288cf77 218if which on_ac_power >/dev/null; then
05f6a46a 219 on_ac_power
0c132682
MZ
220 if [ $? -eq 1 ]; then
221 exit 0
222 fi
223fi
224
4c2dcaa1
MV
225# sleep random amount of time to avoid hitting the
226# mirrors at the same time
69c28efc
MV
227random_sleep
228
4c2dcaa1 229# check if we can access the cache
69c28efc 230if ! apt-get check -q -q 2>/dev/null; then
4c2dcaa1
MV
231 # wait random amount of time before retrying
232 random_sleep
233 # check again
234 if ! apt-get check -q -q 2>/dev/null; then
235 echo "$0: could not lock the APT cache while performing daily cron job. "
236 echo "Is another package manager working?"
237 exit 1
238 fi
69c28efc 239fi
e15dcd38 240
05f6a46a
MZ
241UPDATE_STAMP=/var/lib/apt/periodic/update-stamp
242if check_stamp $UPDATE_STAMP $UpdateInterval; then
243 if apt-get -qq update 2>/dev/null; then
f9b4616d 244 if which dbus-send >/dev/null && pidof dbus-daemon >/dev/null; then
7822f141
OS
245 dbus-send --system / app.apt.dbus.updated boolean:true
246 fi
05f6a46a
MZ
247 update_stamp $UPDATE_STAMP
248 fi
0c132682
MZ
249fi
250
05f6a46a
MZ
251DOWNLOAD_UPGRADEABLE_STAMP=/var/lib/apt/periodic/download-upgradeable-stamp
252if check_stamp $DOWNLOAD_UPGRADEABLE_STAMP $DownloadUpgradeableInterval; then
253 apt-get -qq -d dist-upgrade 2>/dev/null
254 update_stamp $DOWNLOAD_UPGRADEABLE_STAMP
0c132682 255fi
9bd1cf87 256
fdd15654
MV
257UPGRADE_STAMP=/var/lib/apt/periodic/upgrade-stamp
258if check_stamp $UPGRADE_STAMP $UnattendedUpgradeInterval; then
259 unattended-upgrade
260 update_stamp $UPGRADE_STAMP
261fi
262
de15fbae
MV
263AUTOCLEAN_STAMP=/var/lib/apt/periodic/autoclean-stamp
264if check_stamp $AUTOCLEAN_STAMP $AutocleanInterval; then
265 apt-get -qq autoclean
266 update_stamp $AUTOCLEAN_STAMP
267fi
268
47536509
MV
269# check cache size
270check_size_constraints