File: backup-manager.backup-manager-clean-upload-db.cron.monthly

package info (click to toggle)
backup-manager 0.7.12-4
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 1,564 kB
  • sloc: sh: 3,957; perl: 1,188; makefile: 207
file content (118 lines) | stat: -rwxr-xr-x 3,432 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#!/bin/bash
# Cron script to cleanup the uploaded archives database from old entries.

# Do not run if backup-manager is not installed, or
# the conf file does not exist or is not readable.

[ -x /usr/sbin/backup-manager ] || exit 0
[ -r /etc/backup-manager.conf ] || exit 0

. /etc/backup-manager.conf

# Do not run if the site admin has not already activated
# the uploaded database or it does not (yet) exist.

[ "X${BM_UPLOADED_ARCHIVES}" != "X" ] || exit 0
[ -f ${BM_UPLOADED_ARCHIVES} ] || exit 0

# Guard against improper configuration values; give sensible defaults.

MINPER=7        # minimum days back of entries to keep
OTHPER=30       # days back of entries to keep for other upload methods

PERIOD=$(expr ${BM_ARCHIVE_TTL} \+ 0 2>/dev/null)

if [ "X${PERIOD}" = "X" ] || [ ${PERIOD} -lt 1 ]; then
    PERIOD=${MINPER}
fi

case ${BM_UPLOAD_METHOD} in
    scp|ssh-gpg)
        if [ "X${BM_UPLOAD_SSH_TTL}" != "X" ]; then
            PDIFF=$(expr ${BM_UPLOAD_SSH_TTL} - ${PERIOD} 2>/dev/null)
        fi
        ;;
    ftp)
        if [ "X${BM_UPLOAD_FTP_TTL}" != "X" ]; then
            PDIFF=$(expr ${BM_UPLOAD_FTP_TTL} - ${PERIOD} 2>/dev/null)
        fi
        ;;
    s3)
        # For S3 we choose a conservative value because there
        # is no way to know the time-to-live on the remote host.

        PDIFF=$(expr ${OTHPER} - ${PERIOD} 2>/dev/null)
        ;;
    *)
        # The upload database is not used in any other method
        # than those listed above. Exit silently.

        exit 0
        ;;
esac

# A bad configuration may result in either a null or a negative PDIFF.
# Correct this here.

if [ "X${PDIFF}" = "X" ] || [ ${PDIFF} -lt 0 ]; then
    PDIFF=0
fi

PERIOD=$(expr ${PERIOD} \+ ${PDIFF} 2>/dev/null)
PURGE_DATE=$(date "+%Y%m%d" --date ${PERIOD}" days ago")

NEW_UPLOAD_DB=$(mktemp)
if [ $? -ne 0 ]; then
    >&2 echo "Error: cannot create temporary file; aborting."
    exit 2
fi

# Use backup-manager configuration values for permissions
# removing the executable bits (database is a file).

trap "rm -f ${NEW_UPLOAD_DB}" EXIT
set -e
chown ${BM_REPOSITORY_USER}:${BM_REPOSITORY_GROUP} ${NEW_UPLOAD_DB}
chmod ${BM_REPOSITORY_CHMOD} ${NEW_UPLOAD_DB}
chmod a-x ${NEW_UPLOAD_DB}
set +e

# Now that we have ensured that source and destination exist,
# read in the lines and remove any older than $PURGE_DATE.

while read line; do

    archive=$(echo "${line}" | cut -d' ' -f1)
    arch_date=$(echo "${archive}" \
      | sed -e 's/.*\([0-9][0-9][0-9][0-9][01][0-9][0-3][0-9]\).*/\1/')

    # Both days are pure numbers of the form YYYYMMDD. Thus comparison
    # can be reduced to a simple numeric comparison (newer is bigger).

    if [ ${arch_date} -gt ${PURGE_DATE} ]; then
        echo "${line}"

    # We do not remove hashes of existing archives, even if older than
    # $PURGE_DATE (if it stayed there, it has to be a reason).

    elif [ -f ${BM_REPOSITORY_ROOT}/${archive} ]; then
        echo "${line}"
    fi

done < ${BM_UPLOADED_ARCHIVES} >> ${NEW_UPLOAD_DB}

if [ $? -ne 0 ]; then
    >&2 echo "Error parsing BM_UPLOADED_ARCHIVES database; file was not updated"
    exit 3
fi
trap - EXIT

# If all went well, replace the database.

if mv ${NEW_UPLOAD_DB} ${BM_UPLOADED_ARCHIVES} ; then
    :
else
    >&2 echo "Error replacing backup-manager BM_UPLOADED_ARCHIVES with updated values;"
    >&2 echo "please check its status (updated file was at: $NEW_UPLOAD_DB)"
    exit 4
fi