Il y a quelques années, j'avais fait un petit script pour gérer nos sauvegardes sur un serveur. Il faudrait certainement changer le parser la date dans le nom du fichier pour que ça fonctionne mais le script permettait de garder toutes les sauvegardes de la semaine, 1 sauvegarde par semaine pour les semaines précédentes la semaine en cours, 1 sauvegarde par mois pour les mois précédents le mois en cours.
Il y a certainement beaucoup à redire et à améliorer sur l'écrire du script mais de mémoire, c'était fonctionnel.
#!/bin/bash# title : smartbackupmanagement.sh# description : This program was designed to smartly manage backup files.# It keeps files of the current week then 1 file per week for# the current month and 1 file per month for the current year.# Backup files must have the date in their file names and not# any other numeric character.# author : Nicolas# date : 20140306 initial release# : 20140312 allow spaces in file/path name# : 20140314 files are deleted with OK/NOK echoed# date is extracted from basename# : 20150203 changed command to get last day of the month# version : 0.31# usage : ./smartbackupmanagement.sh# notes :# bash_version : 4.2.10(1)-release# TODO : - do yearly backup management (keep only last backup of the# year#============================================================================# Directory where backup files are storedBACKUP_DIRECTORY=1ドル# save current date details and delete initial 0 if anyCURRENT_WEEK=`date +%W | sed 's/^0*//'`CURRENT_MONTH=`date +%m | sed 's/^0*//'`CURRENT_YEAR=`date +%Y | sed 's/^0*//'`# Function to check if the date is the last day of the month# 1ドル = year# 2ドル = month# 3ドル = dayfunction is_last_day_month
{# find the last day of the monthlast_day=`cal 2ドル1ドル| egrep "28|29|30|31"| fmt -1 | tail -1`if[3ドル -eq $last_day];then# this is the last day of the monthreturn0else# remove backupreturn1fi}# Function when weeks are different# 1ドル = file name# 2ドル = day of the weekfunction different_weeks
{# check if day of the week is Sunday (day 0)if[2ドル -eq 0];then# keep backup of the week#echo Keep file as weekly backup
keep_backup_file "1ドル"else# remove backup
delete_backup_file "1ドル"fi}# Function when months are different# 1ドル = file name# 2ドル = day of the backup# 3ドル = month of the backupfunction different_months
{# check if it's the last day of the monthif is_last_day_month $CURRENT_YEAR3ドル2ドル;then# this is the last day of the month#echo Keep file as monthly backup
keep_backup_file "1ドル"else# remove backup
delete_backup_file "1ドル"fi}# Function when years are different# TODOfunction different_years
{#echo different years
:
}# Function to keep current filefunction keep_backup_file
{# keep current file#echo Keep file 1ドル
:
}# Function to delete current file# 1ドル = filenamefunction delete_backup_file
{# delete current file#echo "Delete file\t 1ドル"
rm 1ドルif[$? -eq 0];thenecho -e "File deleted\t\t1ドル";elseecho -e "ERROR file NOT deleted\t1ドル";fi}################# Main program #################echo`date`echo Start Smart Backup Management for$BACKUP_DIRECTORY# for all files in the backup directoryfor backup_file_name in "$BACKUP_DIRECTORY"* ;do# extract file name without last extensionbackup_file_name_noext=`basename ${backup_file_name%.*}`# extract date (string) from file name (delete all letters and punctuations)backup_file_date_string=`echo"$backup_file_name_noext"| tr -d [:alpha:],[:punct:]`# convert string into dclearate (not used)#backup_file_date_date=`date -d $backup_file_date_string`# get backup date detailsbackup_file_day=`date -d $backup_file_date_string +%d`backup_file_week=`date -d $backup_file_date_string +%W`backup_file_month=`date -d $backup_file_date_string +%m`backup_file_year=`date -d $backup_file_date_string +%Y`# delete initial 0 if presentbackup_file_day=`echo"$backup_file_day"| sed 's/^0*//'`backup_file_week=`echo"$backup_file_week"| sed 's/^0*//'`backup_file_month=`echo"$backup_file_month"| sed 's/^0*//'`if[$CURRENT_YEAR -eq $backup_file_year];then# same yearif[$CURRENT_MONTH -eq $backup_file_month];then# same monthif[$CURRENT_WEEK -eq $backup_file_week];then# same week#echo Keep this week backups
keep_backup_file "$backup_file_name"else# same month, different weeksbackup_file_day_of_week=`date -d $backup_file_date_string +%w`
different_weeks "$backup_file_name"$backup_file_day_of_weekfielse# same year, different monthsif[$CURRENT_WEEK -eq $backup_file_week];then# different months BUT same week!!!#echo Keep this week backups
keep_backup_file "$backup_file_name"else# different months, different weeks
different_months "$backup_file_name"$backup_file_day$backup_file_monthfifielse# different years
different_years
fidoneecho Smart Backup Management finished for$BACKUP_DIRECTORYecho`date`echo"* * * * * * * * * *"exit0
# Script
Posté par nico4nicolas . En réponse au message Script shell pour purge automatique d'un dossier. Évalué à 3.
Il y a quelques années, j'avais fait un petit script pour gérer nos sauvegardes sur un serveur. Il faudrait certainement changer le parser la date dans le nom du fichier pour que ça fonctionne mais le script permettait de garder toutes les sauvegardes de la semaine, 1 sauvegarde par semaine pour les semaines précédentes la semaine en cours, 1 sauvegarde par mois pour les mois précédents le mois en cours.
Il y a certainement beaucoup à redire et à améliorer sur l'écrire du script mais de mémoire, c'était fonctionnel.