Проект

Общее

Профиль

Archive-rotate » История » Редакция 2

Редакция 1 (Андрей Волков, 2013-03-22 15:06) → Редакция 2/3 (Андрей Волков, 2013-03-22 15:07)

h1. Ротация архивов по расписанию 

 h2. pg_cleanup_backedup_archives.sh pg_cleanup_backedup_archive.sh 

 <pre> 
 <code class="bash"> 
 #!/bin/bash 

 DAYS_BEFORE_LAST_BACKUP=3 

 if [ -z "${2}" ]; then 
   echo "USEAGE: ${0##*/} /path/to/pg_archive backup-report.file" >&2 
   exit 1 
 fi 

 PG_ARCHIVE_DIRECTORY="${1}" 
 BACKUP_REPORT_FILE="${2}" 

 if [ ! -w "${PG_ARCHIVE_DIRECTORY}" ]; then 
   echo "Archive directory '${PG_ARCHIVE_DIRECTORY}' is not writable" >&2 
   exit 1 
 fi 

 if [ ! -e "${BACKUP_REPORT_FILE}" ]; then 
   echo "File '${BACKUP_REPORT_FILE}' not found" >&2 
   exit 1 
 fi 

 SEARCH_DAYS_AGO=$[($(date +'%s')-$(stat -c '%Y' "${BACKUP_REPORT_FILE}"))/24/3600+${DAYS_BEFORE_LAST_BACKUP}] || exit 1 

 OLD_BACKUPS=$(find "${PG_ARCHIVE_DIRECTORY}" -type f -name "*.backup" -mtime +"${SEARCH_DAYS_AGO}" -printf '%f\n') || exit 1 

 if [ -n "${OLD_BACKUPS}" ]; then 
   pg_archivecleanup "${PG_ARCHIVE_DIRECTORY}" $(echo "${OLD_BACKUPS}" |sort | tail -n 1) || exit 1 
  
   for NAME in ${OLD_BACKUPS}; do 
     rm -f "${PG_ARCHIVE_DIRECTORY}/${NAME}" || exit 1 
   done 
 fi 
 </code> 
 </pre>