]> git.saurik.com Git - apt.git/blob - debian/apt.cron.daily
merged from apt--mvo
[apt.git] / debian / apt.cron.daily
1 #!/bin/sh
2 #
3
4 #set -e
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 #
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 #
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 #
38
39 check_stamp()
40 {
41 stamp="$1"
42 interval="$2"
43
44 if [ $interval -eq 0 ]; then
45 return 1
46 fi
47
48 if [ ! -f $stamp ]; then
49 return 0
50 fi
51
52 # compare midnight today to midnight the day the stamp was updated
53 stamp=$(date --date=$(date -r $stamp --iso-8601) +%s)
54 now=$(date --date=$(date --iso-8601) +%s)
55 delta=$(($now-$stamp))
56
57 # intervall is in days,
58 interval=$(($interval*60*60*24))
59 #echo "stampfile: $1"
60 #echo "interval=$interval, now=$now, stamp=$stamp, delta=$delta"
61
62 if [ $delta -ge $interval ]; then
63 return 0
64 fi
65
66 return 1
67 }
68
69 update_stamp()
70 {
71 stamp="$1"
72
73 touch $stamp
74 }
75
76
77
78 # we check here if autoclean was enough sizewise
79 check_size_constraints()
80 {
81 # min-age in days
82 MaxAge=0
83 MinAge=2
84 MaxSize=0
85 CacheDir="var/cache/apt"
86 CacheArchive="archives/"
87 eval $(apt-config shell MaxAge APT::Archives::MaxAge)
88 eval $(apt-config shell MinAge APT::Archives::MinAge)
89 eval $(apt-config shell MaxSize APT::Archives::MaxSize)
90 eval $(apt-config shell Dir Dir)
91 eval $(apt-config shell CacheDir Dir::Cache)
92 eval $(apt-config shell CacheArchive Dir::Cache::archives)
93
94 # sanity check
95 if [ -z "$CacheDir" -o -z "$CacheArchive" ]; then
96 echo "empty Dir::Cache or Dir::Cache::archives, exiting"
97 exit
98 fi
99
100 Cache="${Dir%/}/${CacheDir%/}/${CacheArchive%/}/"
101
102 # check age
103 if [ ! $MaxAge -eq 0 ] && [ ! $MinAge -eq 0 ]; then
104 find $Cache -name "*.deb" \( -mtime +$MaxAge -and -ctime +$MaxAge \) -and -not \( -mtime -$MinAge -or -ctime -$MinAge \) -print0 | xargs -r -0 rm -f
105 elif [ ! $MaxAge -eq 0 ]; then
106 find $Cache -name "*.deb" -ctime +$MaxAge -and -mtime +$MaxAge -print0 | xargs -r -0 rm -f
107 fi
108
109 # check size
110 if [ ! $MaxSize -eq 0 ]; then
111 # maxSize is in MB
112 MaxSize=$(($MaxSize*1024))
113
114 #get current time
115 now=$(date --date=$(date --iso-8601) +%s)
116 MinAge=$(($MinAge*24*60*60))
117
118 # reverse-sort by mtime
119 for file in $(ls -rt $Cache/*.deb 2>/dev/null); do
120 du=$(du -s $Cache)
121 size=${du%%/*}
122 # check if the cache is small enough
123 if [ $size -lt $MaxSize ]; then
124 break
125 fi
126
127 # check for MinAge of the file
128 if [ ! $MinAge -eq 0 ]; then
129 # check both ctime and mtime
130 mtime=$(stat -c %Y $file)
131 ctime=$(stat -c %Z $file)
132 if [ $mtime -gt $ctime ]; then
133 delta=$(($now-$mtime))
134 else
135 delta=$(($now-$ctime))
136 fi
137 #echo "$file ($delta), $MinAge"
138 if [ $delta -le $MinAge ]; then
139 #echo "Skiping $file (delta=$delta)"
140 break
141 fi
142 fi
143
144 # delete oldest file
145 rm -f $file
146 done
147 fi
148 }
149
150 if ! which apt-config >/dev/null; then
151 exit 0
152 fi
153
154 UpdateInterval=0
155 DownloadUpgradeableInterval=0
156 eval $(apt-config shell UpdateInterval APT::Periodic::Update-Package-Lists DownloadUpgradeableInterval APT::Periodic::Download-Upgradeable-Packages)
157 AutocleanInterval=$DownloadUpgradeableInterval
158 eval $(apt-config shell AutocleanInterval APT::Periodic::AutocleanInterval)
159
160 UnattendedUpgradeInterval=0
161 eval $(apt-config shell UnattendedUpgradeInterval APT::Periodic::Unattended-Upgrade)
162
163
164 # laptop check, on_ac_power returns:
165 # 0 (true) System is on mains power
166 # 1 (false) System is not on mains power
167 # 255 (false) Power status could not be determined
168 # Desktop systems always return 255 it seems
169 if which on_ac_power >/dev/null; then
170 on_ac_power
171 if [ $? -eq 1 ]; then
172 exit 0
173 fi
174 fi
175
176 # check if we can lock the cache and if the cache is clean
177 if ! apt-get check -q -q 2>/dev/null; then
178 echo "$0: could not lock the APT cache"
179 exit 1
180 fi
181
182
183 UPDATE_STAMP=/var/lib/apt/periodic/update-stamp
184 if check_stamp $UPDATE_STAMP $UpdateInterval; then
185 if apt-get -qq update 2>/dev/null; then
186 # Could possible test access to '/var/run/dbus/system_bus_socket' has well,
187 # but I'm not sure how stable the internal pipe location is defined as
188 # being; so for the moment just 2>/dev/null . --sladen 2007-09-27
189 if which dbus-send >/dev/null; then
190 dbus-send --system / app.apt.dbus.updated boolean:true 2>/dev/null || true
191 fi
192 update_stamp $UPDATE_STAMP
193 fi
194 fi
195
196 DOWNLOAD_UPGRADEABLE_STAMP=/var/lib/apt/periodic/download-upgradeable-stamp
197 if check_stamp $DOWNLOAD_UPGRADEABLE_STAMP $DownloadUpgradeableInterval; then
198 apt-get -qq -d dist-upgrade 2>/dev/null
199 update_stamp $DOWNLOAD_UPGRADEABLE_STAMP
200 fi
201
202 UPGRADE_STAMP=/var/lib/apt/periodic/upgrade-stamp
203 if check_stamp $UPGRADE_STAMP $UnattendedUpgradeInterval; then
204 unattended-upgrade
205 update_stamp $UPGRADE_STAMP
206 fi
207
208 AUTOCLEAN_STAMP=/var/lib/apt/periodic/autoclean-stamp
209 if check_stamp $AUTOCLEAN_STAMP $AutocleanInterval; then
210 apt-get -qq autoclean
211 update_stamp $AUTOCLEAN_STAMP
212 fi
213
214 # check cache size
215 check_size_constraints