Archive-rotate » История » Версия 1
Андрей Волков, 2013-03-22 15:06
1 | 1 | Андрей Волков | h1. Ротация архивов по расписанию |
---|---|---|---|
2 | |||
3 | h2. pg_cleanup_backedup_archive.sh |
||
4 | |||
5 | <pre> |
||
6 | <code class="bash"> |
||
7 | #!/bin/bash |
||
8 | |||
9 | DAYS_BEFORE_LAST_BACKUP=3 |
||
10 | |||
11 | if [ -z "${2}" ]; then |
||
12 | echo "USEAGE: ${0##*/} /path/to/pg_archive backup-report.file" >&2 |
||
13 | exit 1 |
||
14 | fi |
||
15 | |||
16 | PG_ARCHIVE_DIRECTORY="${1}" |
||
17 | BACKUP_REPORT_FILE="${2}" |
||
18 | |||
19 | if [ ! -w "${PG_ARCHIVE_DIRECTORY}" ]; then |
||
20 | echo "Archive directory '${PG_ARCHIVE_DIRECTORY}' is not writable" >&2 |
||
21 | exit 1 |
||
22 | fi |
||
23 | |||
24 | if [ ! -e "${BACKUP_REPORT_FILE}" ]; then |
||
25 | echo "File '${BACKUP_REPORT_FILE}' not found" >&2 |
||
26 | exit 1 |
||
27 | fi |
||
28 | |||
29 | SEARCH_DAYS_AGO=$[($(date +'%s')-$(stat -c '%Y' "${BACKUP_REPORT_FILE}"))/24/3600+${DAYS_BEFORE_LAST_BACKUP}] || exit 1 |
||
30 | |||
31 | OLD_BACKUPS=$(find "${PG_ARCHIVE_DIRECTORY}" -type f -name "*.backup" -mtime +"${SEARCH_DAYS_AGO}" -printf '%f\n') || exit 1 |
||
32 | |||
33 | if [ -n "${OLD_BACKUPS}" ]; then |
||
34 | pg_archivecleanup "${PG_ARCHIVE_DIRECTORY}" $(echo "${OLD_BACKUPS}" |sort | tail -n 1) || exit 1 |
||
35 | |||
36 | for NAME in ${OLD_BACKUPS}; do |
||
37 | rm -f "${PG_ARCHIVE_DIRECTORY}/${NAME}" || exit 1 |
||
38 | done |
||
39 | fi |
||
40 | </code> |
||
41 | </pre> |