• # Script

    Posté par . 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.

    #!/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 stored
    BACKUP_DIRECTORY=1ドル
    # save current date details and delete initial 0 if any
    CURRENT_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ドル = day
    function is_last_day_month
    {
     # find the last day of the month
     last_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 month
     return 0
     else
     # remove backup
     return 1
     fi
    }
    # Function when weeks are different
    # 1ドル = file name
    # 2ドル = day of the week
    function 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 backup
    function different_months
    {
     # check if it's the last day of the month
     if is_last_day_month $CURRENT_YEAR 3ドル 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
    # TODO
    function different_years
    {
     #echo different years
     :
    }
    # Function to keep current file
    function keep_backup_file
    {
     # keep current file
     #echo Keep file 1ドル
     :
    }
    # Function to delete current file
    # 1ドル = filename
    function delete_backup_file
    {
     # delete current file
     #echo "Delete file\t 1ドル"
     rm 1ドル
     if [ $? -eq 0 ]; then echo -e "File deleted\t\t1ドル"; else echo -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 directory
    for backup_file_name in "$BACKUP_DIRECTORY"* ; do
     # extract file name without last extension
     backup_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 details
     backup_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 present
     backup_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 year
     if [ $CURRENT_MONTH -eq $backup_file_month ] ; then
     # same month
     if [ $CURRENT_WEEK -eq $backup_file_week ] ; then
     # same week
     #echo Keep this week backups
     keep_backup_file "$backup_file_name"
     else
     # same month, different weeks
     backup_file_day_of_week=`date -d $backup_file_date_string +%w`
     different_weeks "$backup_file_name" $backup_file_day_of_week
     fi
     else
     # same year, different months
     if [ $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_month
     fi
     fi
     else
     # different years
     different_years
     fi
    done
    echo Smart Backup Management finished for $BACKUP_DIRECTORY
    echo `date`
    echo "* * * * * * * * * *"
    exit 0