aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/obs-mirror/rm-old-nightly-archives.sh
blob: 485bf01165ee579a91038844d39761ddada06859 (plain)
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
#!/bin/sh -e
# Remove nightly archives older than one month (OS#4862)
echo "Redirecting all output to: /home/pkgmirror/rm-old-nightly-archives.log"
exec >"/home/pkgmirror/rm-old-nightly-archives.log" 2>&1

DRY=0

# Get removal date in seconds since epoch and display it
DATE_RM_SEC=$(expr $(date +%s) - 3600 \* 24 \* 32)
DATE_RM_STR=$(date -d "@$DATE_RM_SEC" +"%Y-%m-%d")
echo "Removing nightly archives from $DATE_RM_STR and older (DRY=$DRY)"

cd /downloads/obs-mirror

for i in */nightly; do
        # "Last modified" isn't set to the date of the dir name for some
        # archives, so parse the date from the dir name instead
        DATE_DIR="$(basename "$(dirname "$i")")"  # e.g. "20210604-002301"
        DATE_DIR_SEC="$(date -d "$(echo "$DATE_DIR" | cut -d "-" -f 1)" +%s)"
        if [ -z "$DATE_DIR_SEC" ]; then
                echo "ERROR: $i: failed to parse date from dir name"
                continue
        fi

        if [ "$DATE_DIR_SEC" -lt "$DATE_RM_SEC" ]; then
                DATE_DIR_STR="$(date -d "@$DATE_DIR_SEC" +"%Y-%m-%d")"
                echo "Removing $i ($DATE_DIR_STR)..."
                if [ "$DRY" = 0 ]; then
                        rm -r "$i"
                fi
        fi
done

echo "Done"