Archivage de données périmées par script

Archivage de données périmées par script 1

Vous avez tous déjà été confronté à ce problème, j’en suis certain ! Vous avez un serveur de fichiers où les utilisateurs accumulent un grand nombre de document. Arrive le jour où votre espace de stockage est presque plein ! Vous demandez à vos utilisateurs de faire le ménage et rien ne se passe 😉

D’une certaine façon, c’est assez compréhensible car la tâche qui consiste à ouvrir chaque fichier pour en évaluer l’actualité puis décider de l’archiver et tout simplement colossale.

Pourtant, grâce à des utilitaires comme fileage, il est facile de se rendre compte que beaucoup de fichiers ne servent plus !

On peut alors gagner en peu de temps en activant la compression NTFS sur le disque de stockage – au risque de ralentir le fonctionnement du serveur. L’usage de AutoCompress donne un peu d’intelligence à cette approche puisque seuls les fichiers inutilisés sont compressés.

Arrivera néanmoins le jour où il faudra bien faire du ménage et archiver les données non-utilisées.

J’ai finalement adapté un script écrit en KIXTART pour qu’il parcourt une arborescence et déplace les fichiers anciens vers un répertoire de destination. L’arborescence des répertoires source est recrée dans le répertoire d’archivage.

Un journal est créé pour chaque exécution du script. Ce fichier est ensuite compressé avec 7zip puis envoyé par email grâce à bmail/mpack. Le corps de chaque email contient un récapitulatif des informations du dossier source (taille, nombre de fichiers) et des statistiques sur le nombre de fichiers déplacés.

Les paramètres d’exécution sont définis en début de script.

  • $source_dir = “E:Source” – données à archiver
  • $dest_dir = “E:Backup” – stockage des données archivées
  • $old_threshold=30 – les fichiers n’ayant pas été accédées depuis plus de 30 jours seront archivés
  • $debug_mode=”yes” ; – le détail des actions est enregistré
  • $simulation_mode =”no” ; – en mode simulation, les fichiers ne sont pas effectivement déplacés

go.bat

kix32.exe du.kix

pause

Télécharder le script du.kix

 

 

;     NT/95 diskuse + message - Kixtart 3.63, 4.00

;

; (c) scripting@wanadoo.nl 2001

;

;     vs 1.04 - program

;

;     1.00  (20010525)     original version

;     1.01  (20010620)     - MBytes notation changed in KBytes

;                          - contents of "root" wasn't count.

;                          - cleanup $log_file

;     1.02  (20010701)     - fix: no limit to length of variable names

;                            ($number_of_directories => $number_of_dirs)

;     1.03  (20010710)     - standardization of $debug_file value

;     1.04  (20011115)     - fix: possible root problem

;                            (input by Peter Pulfer)

;                          - suppress SetConsole("hide")

;

$prgrm_version="1.04"

;

; ---------------------------------------------------------------------------

; user definable part

; ---------------------------------------------------------------------------

$source_dir = "E:Source" ; the directory we want to clean

$dest_dir = "E:Backup" ; where to archive old files

$old_threshold=30 ; files not used for more than "old_threshold" days are processed

$debug_mode="yes" ; - yes/no - no = no user output will be viewed

$simulation_mode ="no" ; - yes/no - no = no file will be moved

; ---------------------------------------------------------------------------

init_temp_dir() ; init temp var

md $dest_dir ; make dest dir if not exist

init_debug_file() ; init debug file

$number_of_dirs=1 ; init source dir

DIM $dirs[$number_of_dirs+1]

$dirs[1]=$source_dir

; ---------------------------------------------------------------------------

$number_of_files=1 ; <- increase value by new files to check for

DIM $files[$number_of_files+1]

$files[1]="*.*"

$tmp_file=$tmp_directory+"kixtart.tmp"   ; init debug mode

IF (Exist($debug_file) = 1)

del $debug_file

ENDIF

IF ($debug_mode = "yes")

IF (RedirectOutput($debug_file) <> 0)

ENDIF

ENDIF

? "KIX-DU  "+@kix+" (vs "+$prgrm_version+")"+"                             "+@date+" "+@time

?

IF (Exist($tmp_file) = 1)

del $tmp_file

ENDIF

$eof="~~~ The command completed succesfully ~~~"

;

$i=1

WHILE ($i <= $number_of_dirs)

$k=1

IF (substr($dirs[$i],len($dirs[$i]),1) <> "")

$dirs[$i]=$dirs[$i]+""

ENDIF

IF ($debug_mode = "yes")

? "check directory: "+$dirs[$i]

ENDIF

WHILE ($k <= $number_of_files)

$entry=$dirs[$i]+$files[$k]

IF (exist($dirs[$i]) = 1)

SHELL "%comspec% /c echo "+substr($dirs[$i],1,len($dirs[$i])-1)+"   >>"+$tmp_file

SHELL "%comspec% /c dir  "+CHR(34)+$entry+CHR(34)+" /a:d /b /o:n /s >>"+$tmp_file

ENDIF

$k=$k+1

LOOP

$i=$i+1

LOOP

SHELL "%comspec% /c echo "+$eof+">>"+$tmp_file

? "check files:     "+$files[1]

$i=2

WHILE ($i <= $number_of_files)

? "                 "+$files[$i]

$i=$i+1

LOOP

?

$total_size=0.0

$total_dirs=0.0

$total_files=0.0

;----------------------------------------------

$moved_total_size=0.0

$moved_total_files=0.0

;----------------------------------------------

IF (Exist($tmp_file) = 1)

IF (Open(1,$tmp_file,2) = 0)

$completed="no"

WHILE ($completed <> "yes")

$result=LTRIM(RTRIM(Readline(1)))

IF (@error <> 0) or (instr($result,$eof) <> 0)

$completed="yes"

ELSE

IF (exist($result) = 1)

$total_dirs=$total_dirs+1

GOSUB show_file_info

ENDIF

ENDIF

LOOP

IF Close(1)

ENDIF

ENDIF

ENDIF

$total_size=$total_size/1024/1024

IF (Exist($tmp_file) = 1)

del $tmp_file

ENDIF

?

IF ($total_dirs+$total_files = 0)

? "completed. found no entries."

ELSE

? "completed. found " $total_dirs+$total_files " entries with total size of "+$total_size+" KBytes."

ENDIF

?

IF (RedirectOutput("") <> 0)

ENDIF

$moved_total_size=$moved_total_size/1024/1024 ; convert to Mb

; display stats  to screen

IF ($debug_mode = "yes")

? "Output written to file '"+$debug_file+"'."

?

? " #         dirs  "+$total_dirs

? " #         files "+$total_files

? " #         size  "+$total_size+" Mbytes"

? " #"

IF ($simulation_mode = "yes")

? " # == Simulation mode enabled =="

? " #"

ENDIF

? " #   moved files "+$moved_total_files

? " #   moved size  "+$moved_total_size+" Mbytes"

? " #"

? " # % moved files "+ $moved_total_files*100/$total_files

? " # % moved size  "+ $moved_total_size*100/$total_size

?

ENDIF

; display stats  to logfile i.e email body

$log_file="logfile.log"

del $log_file /h /c

IF Open( 3 , $log_file , 5 ) = 0

$x = WriteLine( 3 , "Output written to file '"+$debug_file+"'."+@CRLF)

$x = WriteLine( 3 , @CRLF)

$x = WriteLine( 3 ,  "dirs  "+$total_dirs+@CRLF)

$x = WriteLine( 3 ,  "files "+$total_files+@CRLF)

$x = WriteLine( 3 ,  "size  "+$total_size+" Mbytes"+@CRLF)

$x = WriteLine( 3 , @CRLF)

IF ($simulation_mode = "yes")

$x = WriteLine( 3 ,  "== Simulation mode enabled =="+@CRLF)

$x = WriteLine( 3 ,  @CRLF)

ENDIF

$x = WriteLine( 3 , "moved files "+$moved_total_files+@CRLF)

$x = WriteLine( 3 , "moved size  "+$moved_total_size+" Mbytes"+@CRLF)

$x = WriteLine( 3 , @CRLF)

$x = WriteLine( 3 , "% moved files "+ $moved_total_files*100/$total_files+@CRLF)

$x = WriteLine( 3 , "% moved size  "+ $moved_total_size*100/$total_size+@CRLF)

$x = WriteLine( 3 , @CRLF)

IF Close(3)

Beep

? "Error closing file "+ $log_file+" !"

ENDIF

Else

BEEP

? "Failed to Open "+ $log_file+": [" + @ERROR + "]"

ENDIF

; compress logfile with 7z

del "logfile1.7z" /h /c

$command = "7za u -r -t7z logfile1.7z "+$debug_file+" -mx9 -mmt"

? $command

SHELL $command

; create email with $log_file as body and logfile1.7z as attachment

$command  = "mpack -s purge_ftp_public -d "+$log_file+" -o body.msg logfile1.7z"

? $command

SHELL  $command

; email result

$command = "bmail -d -h -s smtp.monserver.fr -t  yyy@@xxx.fr -f zzz@@xxx.fr -m body.msg"

? $command

SHELL $command

EXIT

:show_file_info

$count=0

$size=0

IF (substr($result,len($result),1) = "")

$result=substr($result,1,len($result)-1)

ENDIF

$filename=Dir($result+"*.*")

WHILE ($filename <> "") AND (@error = 0)

IF GetFileAttr($result+""+$filename) & 16

; - directory -

; --------------------------------------------

md Replace($result+""+$filename, $dirs[1], $dest_dir+"")

; --------------------------------------------

ELSE

IF ($filename <> ".") AND ($filename <> "..")

$count=$count+1

ENDIF

$size=$size+GetFileSize($result+""+$filename)

; ---------------------------------------------------------

$last_access_time = GetFileTime($result+""+$filename,2)

$old = DateCalc(@date,substr($last_access_time,1,10))

? $result+""+$filename + " last_write_time="+ $last_write_time + " creation_time="+ $creation_time + " last_access_time="+ $last_access_time + " Last accessed= " + $old+" days ago"

$move="";

$move_to="";

if ($old > $old_threshold)

$move = " move it to "

$move_to = Replace($result+""+$filename, $dirs[1], $dest_dir+"")

$moved_total_size=  $moved_total_size+GetFileSize($result+""+$filename)

$moved_total_files= $moved_total_files+1

IF ($simulation_mode = "no")

MOVE $result+""+$filename $move_to /c /h /r

? "moved"

else

COPY $result+""+$filename $move_to /c /h /r

? "copied " + $result+""+$filename + " to " + $move_to

ENDIF

endif

; ---------------------------------------------------------

ENDIF

$filename=Dir()

LOOP

;

$total_files=$total_files+$count

$total_size=$total_size+$size

IF ($debug_mode = "yes")

? substr("       ",1,7-len("$count")) $count " " substr("         ",1,9-len("$size")) $size " ==> "+$result

ENDIF

RETURN

; ---------------------------------------------------------

;FUNCTION         DateCalc()

;

;AUTHOR           Jochen Polster (jochenDOTpolsterATgmxDOTnet)

;                 based on date algorithms by Peter Baum to be found here :

;                 http://www.capecod.net/~pbaum/date/date0.htm

;

;VERSION          1.12

;

;VERSION HISTORY  1.0  2001/12/10 Initial release

;

;                 1.1  2004/02/18 Added support for single digit month/day input

;                                 and optional single digit month/day date return

;

;                 1.11 2004/02/20 Minor Variable handling fix

;

;                 1.12 2005/03/31 Finally supports "NoVarsInStrings" and "Explicit" set to "ON" in

;                                 all possible variations

;

;ACTION           Calculates days between 2 dates or returns a date string calculated from

;                 a given date and a given amount of days ( Addition of positive or negative

;                 integer value )

;

;SYNTAX           DateCalc( Date1, Date2|Modifier, [SingleDigit] )

;

;PARAMETERS       Date1 (Required)

;                  -  (Gregorian) Date string in Format : YYYY/M[M]/D[D]

;

;                 Date2|Modifier (Required)

;                  - either a second (Gregorian) date string (YYYY/M[M]/D[D]) to calculate days between

;                    or a positive/negative amount of days to calculate with

;

;                 SingleDigit (Optional)

;                  - if not zero date will be returned unpadded, eg. 2004/2/9

;

;REMARKS          Date format must be KiX friendly : YYYY/M[M]/D[D] (2001/11/20)

;                 To calculate a date less than given assign a negative integer (ie. -45 )

;

;RETURNS          Either a positive integer value of days between two given dates,

;                 or a (Gregorian) date string.

;

;DEPENDENCIES     None !

;

;EXAMPLES

;                 break on

;                 call "[path]DateCalc.udf"

;

;                 "boot.ini last modified : " + DateCalc(@date,substr(getfiletime("c:boot.ini"),1,10))

;                  + " days ago ..." ? ?

;

;                 $mod = 60

;                 "in/before $mod day(s) it was/will be " + DateCalc(@date,$mod) ? ?

;

;                 get $

function DateCalc($date1, $DateOrMod, optional $SingleDigit)

dim $_intDate1, $_intYear1, $_intMonth1, $_intDay1

dim $_intDate2, $_intYear2, $_intMonth2, $_intDay2

$date1 = split($date1,'/')

if ubound($date1) <> 2

exit 1

endif

$_intYear1  = val($date1[0])

$_intMonth1 = val($date1[1])

$_intDay1   = val($date1[2])

if $_intMonth1 < 3

$_intMonth1 = $_intMonth1 + 12

$_intYear1  = $_intYear1  - 1

endif

$_intDate1 = $_intDay1 + ( 153 * $_intMonth1 - 457 ) / 5 + 365 * $_intYear1 +

$_intYear1 / 4 - $_intYear1 / 100 + $_intYear1 / 400 - 306

select

case vartype($DateOrMod) = 3

$_intDate2  = $_intDate1 + $DateOrMod

if instr($_intDate2,'-') $_intDate2 = val(substr($_intDate2,2,len($_intDate2)-1)) endif

$_intYear2  = ( 100 * ( ( ( 100*($_intDate2+306)-25)/3652425)

- ( ((100*($_intDate2+306)-25)/3652425)/4)

) + (100*($_intDate2+306)-25)

) / 36525

$_intMonth2 = (   5 * ( ( ( 100*($_intDate2+306)-25)/3652425)

- ( ((100*($_intDate2+306)-25)/3652425)/4)

+ ($_intDate2+306) - 365 * $_intYear2 - $_intYear2 / 4

) + 456

) / 153

$_intDay2   = ( ( ( 100*($_intDate2+306)-25)/3652425)

- ( ((100*($_intDate2+306)-25)/3652425)/4)

+ ($_intDate2+306) - 365 * $_intYear2 - $_intYear2 / 4

)         - ( 153 * $_intMonth2 - 457

) / 5

if $_intMonth2 > 12 $_intYear2 = $_intYear2 + 1 $_intMonth2 = $_intMonth2 - 12 endif

if not $SingleDigit

if len($_intYear2 ) < 4

$_ = execute("for $i=1 to 4-len($$_intYear2) $$_intYear2 = '0' + $$_intYear2 next")

endif

$_intMonth2 = right("0" + $_intMonth2,2)

$_intDay2   = right("0" + $_intDay2,2)

endif

$DateCalc = '' + $_intYear2 + '/' + $_intMonth2 + '/' + $_intDay2

case vartype($DateOrMod) = 8

$DateOrMod = split($DateOrMod,'/')

if ubound($DateOrMod) <> 2

exit 1

endif

$_intYear2  = val($DateOrMod[0])

$_intMonth2 = val($DateOrMod[1])

$_intDay2   = val($DateOrMod[2])

if $_intMonth2 < 3

$_intMonth2 = $_intMonth2 + 12

$_intYear2  = $_intYear2  - 1

endif

$_intDate2 = $_intDay2 + ( 153 * $_intMonth2 - 457 ) / 5 + 365 * $_intYear2 +

$_intYear2 / 4 - $_intYear2 / 100 + $_intYear2 / 400 - 306

$DateCalc  = $_intDate1 - $_intDate2

;comment the next line if you wish to return negative results also !!!

if instr($DateCalc,'-') $DateCalc = val(substr($DateCalc,2,len($DateCalc)-1)) endif

case 1

exit 1

endselect

endfunction

;-------------------------------------------------------------------------------------------

Function Replace($SourceString, $SearchString, $ReplaceString, Optional $First, Optional $CaseSensitive)

; Effectue le remplacement d'une chaine par une autre.

Dim $String1

Dim $String2

Dim $Finished

$Finished = 0

$Counter = 0

$String1 = $SourceString

If $CaseSensitive

$PreviousState = SetOption("CaseSensitivity", "On")

EndIf

While Not $Finished

$String2 = $String1

$Location = InStr($String1, $SearchString)

If $Location > 0

$String1 = Substr($String1, 1, $Location - 1) + $ReplaceString + Substr($String1, $Location + Len($SearchString), Len($SourceString) - $Location + Len($SearchString) + 1)

Else

$Finished = 1

EndIf

If $First

$Finished = 1

EndIf

If $String1 = $String2

$Finished = 1

EndIf

Loop

$Replace = $String1

$barul = SetOption("CaseSensitivity", $PreviousState)

EndFunction

;-------------------------------------------------------------------------------------------

; initialize environement variables

Function init_temp_dir()

$tmp_directory=ExpandEnvironmentVars("%tmp%")

IF (substr($tmp_directory,len($tmp_directory),1) <> "")

$tmp_directory=$tmp_directory+""

ENDIF

EndFunction

;-------------------------------------------------------------------------------------------

; init debug file based on date

Function init_debug_file()

$TomorD = @MDAYNO

$TomorM = @MONTHNO

$TomorY = @YEAR

If $TomorD < 10

$TomorD = "0" + $TomorD

EndIf

If $TomorM < 10

$TomorM = "0" + $TomorM

EndIf

$debug_file="traceinfo-"+$TomorD+$TomorM+$TomorY+"-"+@TICKS+".log"

EndFunction

 

A propos Olivier Olejniczak

Cofondateur de SynerGeek.fr. Passionné d'informatique, je m'intéresse plus particulièrement aux technologies de virtualisation et de stockage. J'apprécie la liberté et la quasi-absence de frontières à mon imagination qu'offre l'OpenSource et Linux. Professionnellement, je travaille exclusivement avec les outils Microsoft. Les pieds bien sur terre et ancrés dans le quotidien de l'entreprise, j'aime faire partager mes découvertes et contribuer à un meilleur usage des technologies. Vous aussi, rejoignez-nous sur Synergeek et partagez votre expérience!

Je vous propose également...

JSON dans les scripts Bash avec Ticktick 2

JSON dans les scripts Bash avec Ticktick

Je vais vous présenter l’ami du WGET par excellence. Imaginez le contexte suivant : sur …

Compiler un script PowerShell avec PS2EXE 3

Compiler un script PowerShell avec PS2EXE

J’utilise au quotidien le langage PowerShell pour gérer des serveurs Windows. Ce langage est à …

5 commentaires

  1. Je suis nouveau ici j’aime se site ya beaucoup d’information jai besoin de conseil aussi je suis débutant

  2. Bonjour, tres utile ce programme , je n’arrive pas à le telecharger , serait il possible de reactiver le lien ? merci et bonne fete de fin d’année !

  3. olivier olejniczak

    Un autre script de backup fort intéressant:

    http://fds.mvps.org/fiches/Fiche-09.htm