+# deal with the Apt::Periodic::BackupArchiveInterval
+do_cache_backup()
+{
+ BackupArchiveInterval="$1"
+ if [ $BackupArchiveInterval -eq 0 ]; then
+ return
+ fi
+
+ # Set default values and normalize
+ CacheDir="/var/cache/apt"
+ eval $(apt-config shell CacheDir Dir::Cache/d)
+ CacheDir=${CacheDir%/}
+ if [ -z "$CacheDir" ]; then
+ debug_echo "practically empty Dir::Cache, exiting"
+ return 0
+ fi
+
+ Cache="${CacheDir}/archives/"
+ eval $(apt-config shell Cache Dir::Cache::Archives/d)
+ if [ -z "$Cache" ]; then
+ debug_echo "practically empty Dir::Cache::archives, exiting"
+ return 0
+ fi
+
+ BackupLevel=3
+ eval $(apt-config shell BackupLevel APT::Periodic::BackupLevel)
+ if [ $BackupLevel -le 1 ]; then
+ BackupLevel=2 ;
+ fi
+
+ Back="${CacheDir}/backup/"
+ eval $(apt-config shell Back Dir::Cache::Backup/d)
+ if [ -z "$Back" ]; then
+ echo "practically empty Dir::Cache::Backup, exiting" 1>&2
+ return
+ fi
+
+ CacheArchive="$(basename "${Cache}")"
+ test -n "${CacheArchive}" || CacheArchive="archives"
+ BackX="${Back}${CacheArchive}/"
+ for x in $(seq 0 1 $((${BackupLevel}-1))); do
+ eval "Back${x}=${Back}${x}/"
+ done
+
+ # backup after n-days if archive contents changed.
+ # (This uses hardlink to save disk space)
+ BACKUP_ARCHIVE_STAMP=/var/lib/apt/periodic/backup-archive-stamp
+ if check_stamp $BACKUP_ARCHIVE_STAMP $BackupArchiveInterval; then
+ 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
+ mkdir -p $Back
+ rm -rf $Back$((${BackupLevel}-1))
+ for y in $(seq $((${BackupLevel}-1)) -1 1); do
+ eval BackY=${Back}$y
+ eval BackZ=${Back}$(($y-1))
+ if [ -e $BackZ ]; then
+ mv -f $BackZ $BackY ;
+ fi
+ done
+ cp -la $Cache $Back ; mv -f $BackX $Back0
+ update_stamp $BACKUP_ARCHIVE_STAMP
+ debug_echo "backup with hardlinks. (success)"
+ else
+ debug_echo "skip backup since same content."
+ fi
+ else
+ debug_echo "skip backup since too new."
+ fi
+}
+
+# sleep for a random interval of time (default 30min)
+# (some code taken from cron-apt, thanks)
+random_sleep()
+{
+ RandomSleep=1800
+ eval $(apt-config shell RandomSleep APT::Periodic::RandomSleep)
+ if [ $RandomSleep -eq 0 ]; then
+ return
+ fi
+ if [ -z "$RANDOM" ] ; then
+ # A fix for shells that do not have this bash feature.
+ RANDOM=$(( $(dd if=/dev/urandom bs=2 count=1 2> /dev/null | cksum | cut -d' ' -f1) % 32767 ))
+ fi
+ TIME=$(($RANDOM % $RandomSleep))
+ debug_echo "sleeping for $TIME seconds"
+ sleep $TIME
+}
+
+
+debug_echo()
+{
+ # Display message if $VERBOSE >= 1
+ if [ "$VERBOSE" -ge 1 ]; then
+ echo $1 1>&2
+ fi
+}
+
+check_power(){
+ # laptop check, on_ac_power returns:
+ # 0 (true) System is on main power
+ # 1 (false) System is not on main power
+ # 255 (false) Power status could not be determined
+ # Desktop systems always return 255 it seems
+ if which on_ac_power >/dev/null; then
+ on_ac_power
+ POWER=$?
+ if [ $POWER -eq 1 ]; then
+ debug_echo "exit: system NOT on main power"
+ return 1
+ elif [ $POWER -ne 0 ]; then
+ debug_echo "power status ($POWER) undetermined, continuing"
+ fi
+ debug_echo "system is on main power."
+ fi
+ return 0
+}
+
+# ------------------------ main ----------------------------
+
+if test -r /var/lib/apt/extended_states; then
+ # Backup the 7 last versions of APT's extended_states file
+ # shameless copy from dpkg cron
+ if cd /var/backups ; then
+ if ! cmp -s apt.extended_states.0 /var/lib/apt/extended_states; then
+ cp -p /var/lib/apt/extended_states apt.extended_states
+ savelog -c 7 apt.extended_states >/dev/null
+ fi
+ fi
+fi
+
+# check apt-config existence
+if ! which apt-config >/dev/null ; then
+ exit 0
+fi
+
+# check if the user really wants to do something
+AutoAptEnable=1 # default is yes
+eval $(apt-config shell AutoAptEnable APT::Periodic::Enable)
+
+if [ $AutoAptEnable -eq 0 ]; then
+ exit 0
+fi
+
+# Set VERBOSE mode from apt-config (or inherit from environment)
+VERBOSE=0
+eval $(apt-config shell VERBOSE APT::Periodic::Verbose)
+debug_echo "verbose level $VERBOSE"
+if [ "$VERBOSE" -le 2 ]; then
+ # quiet for 0,1,2
+ XSTDOUT=">/dev/null"
+ XSTDERR="2>/dev/null"
+ XAPTOPT="-qq"
+ XUUPOPT=""
+else
+ XSTDOUT=""
+ XSTDERR=""
+ XAPTOPT=""
+ XUUPOPT="-d"
+fi
+if [ "$VERBOSE" -ge 3 ]; then
+ # trace output
+ set -x
+fi
+
+check_power || exit 0
+
+# check if we can lock the cache and if the cache is clean
+if which apt-get >/dev/null && ! eval apt-get check $XAPTOPT $XSTDERR ; then
+ debug_echo "error encountered in cron job with \"apt-get check\"."
+ exit 0
+fi
+
+# Global current time in seconds since 1970-01-01 00:00:00 UTC
+now=$(date +%s)
+
+# Support old Archive for compatibility.
+# Document only Periodic for all controlling parameters of this script.