Bacula: освобождаем место

Оказалось как всегда все в opensource не просто. Благо наткнулся на скрипт для linux. Оставалось адаптировать его под freebsd. Данный скрипт будет удалять бекапы старее двух месяцев.

Для начала установим coreutils поскольку в нем есть gnu date. Вам же надо будет подправить пути в скрипте и срок бекапов под свои реалии.

#!/usr/bin/env bash
#set -x

# original script https://gist.github.com/starkers/5cfa83dfaeba2da9fe7c

# Prune and volumes older than 2 months:
PAST=`gdate --date='-2 months 0 day ago' +%Y-%m-%d`

#EG a hard coded date
#PAST="2012-11-29"
#PAST="2013-11-29"

DB=bacula

#tmpfiles
TMPF1=`mktemp`
TMPF2=`mktemp`

#extract stuff from postgresq
psql -U postgres $DB -c  "select volumename from media where lastwritten < '$PAST' " > "$TMPF1"

#an ugly attempt to clean the output.. I'm sure this can be fixed with a flag on the above cmd sometime
VOLS=`awk '{print $1}' "$TMPF1" | tail -n+3  | grep -v "(" `

#output a clean list of volumes into TMPF2
cp /dev/null $TMPF2
for a in $VOLS ; do
    echo $a >> $TMPF2
done

#how many volumes did we match btw?
COUNT=`wc -l $TMPF2 | awk '{print $1}'`

if [ "$COUNT" -gt 0 ]; then
    echo "Found $COUNT volumes"

    for a in `cat "$TMPF2"` ; do
        echo $TMPF2
        echo "### processing $a ###"
        cp /dev/null $TMPF1
        printf "prune yes volume=$a\n" >> $TMPF1
        printf "delete yes volume=$a\n" >> $TMPF1

        bconsole <$TMPF1 1>/dev/null
        # ^ uncomment the above to go live, check this works first!

        #check if bconsole exited with an error
        if [ "$?" -gt 0 ]; then
            echo "+[$a]: WARNING: something went wrong with bconsole: $?"
            echo "See: TMPF1=$TMPF1 / TMPF2=$TMPF2"
            exit
        fi

        # The above takes care of the bacula side.. now delete files
        # as I store my files in a per-host directory (and keep catalog files elsewhere I just split this into two:

        NAME="$(cut -f1 -d"_" <<<"$a")"
        #Firstly lets remove the Catalog files
        if grep -q ^Catalog_File <<< "$a"  ; then
            FPATH=/backups
            echo "+[$a] removing file: $FPATH/$NAME/volume/$a"
            echo rm -f "$FPATH/$NAME/volume/$a"
            rm -f "$FPATH/$NAME/volume/$a"
            # ^ uncomment the above to go live

            # else its a client..
          else
            # get the clients name from $a
            #NAME="$(cut -d "-" -f 1 <<<"$a")"
            FPATH=/backups
            echo "+[$a]  removing file: $FPATH/$NAME/volume/$a"
            echo rm -f "$FPATH/$NAME/volume/$a"
            rm -f "$FPATH/$NAME/volume/$a"
            # ^ uncomment the above to go live
        fi
        echo
    done
  else
    echo "No volumes found older than $PAST"
fi

#cleanup
rm "$TMPF1" "$TMPF2"


## Now as you may note the log table can get quite large.
# bacula does not (internally) rely on this in any form.. rather the "file" table is the important one
#
#
#  bacula=# SELECT
#bacula-#    relname AS objectname,
#bacula-#    relkind AS objecttype,
#bacula-#    reltuples AS "#entries", pg_size_pretty(relpages::bigint*8*1024) AS size
#bacula-#    FROM pg_class
#bacula-#    WHERE relpages >= 8
#bacula-#    ORDER BY relpages DESC;
#           objectname            | objecttype |  #entries   |  size
#---------------------------------+------------+-------------+---------
# log                             | r          | 2.18772e+06 | 78 GB
# file                            | r          | 3.49857e+08 | 51 GB
# file_jpfid_idx                  | i          | 3.49857e+08 | 24 GB
# file_jobid_idx                  | i          | 3.49857e+08 | 13 GB
# file_pkey                       | i          | 3.49857e+08 | 11 GB
# log_name_idx                    | i          | 2.18772e+06 | 7681 MB

# I suggest you truncate the bastard
# psql $DB -c "TRUNCATE log;"

В скрипте после shebang указана ссылка на оригинальный скрипт.

Показать комментарии