Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate
Photo

CopyIfNewer + incremental backup


  • Please log in to reply
7 replies to this topic
enrica
  • Members
  • 117 posts
  • Last active: Feb 26 2016 11:15 PM
  • Joined: 21 Mar 2005
Hi, everybody, I try without success to modify this backup script (help file)
what I want is: when the fileA.txt exist in copydest not overwritten, but copy with a incremental number
and leave intact the FileA.txt
eg.
C:\H\FileA.txt >> C:\H\BACKUPPP\FileA00001.txt (OR C:\H\BACKUPPP\00001FileA.txt)
after to modify C:\H\FileA.txt and make another backup,
C:\H\FileA.txt >> C:\H\BACKUPPP\FileA00002.txt

count = 10000
CopyDest = C:\H\BACKUPPP\
CopySourcePattern = C:\H\*.txt
CopyIfNewer:
Loop, %CopySourcePattern%
{
	copy_it = n
	IfNotExist, %CopyDest%\%A_LoopFileName% 
		copy_it = y
	else
	{
		FileGetTime, time, %CopyDest%\%A_LoopFileName%
		EnvSub, time, %A_LoopFileTimeModified%, seconds  
		if time < 0  ; Source file is newer than destination file.
			copy_it = y
	}
	if copy_it = y
	{
FileCopy, %A_LoopFileFullPath%, %CopyDest%\%A_LoopFileName%, 1  

;------------------------ MY ------------------------------
{
  IfExist, %CopyDest%\%A_LoopFileName%
    count++
  Else
    break
}
FileCopy, %A_LoopFileFullPath%, %CopyDest%\%count%...%A_LoopFileName%, 1  
count++
;------------------------ MY ------------------------------

	if ErrorLevel <> 0
MsgBox, Could not copy "%A_LoopFileFullPath%" 
	}
}
run, C:\H\BACKUPPP\
exitapp

Is there a way?
Thanks in advance.
Ciao!
65.6E.72.69.63.61. (My hovercraft is full of eels)

Laszlo
  • Moderators
  • 4713 posts
  • Last active: Mar 31 2012 03:17 AM
  • Joined: 14 Feb 2005
There is a way, of course, but it might not be that good. It gets complicated if also the renamed file already exists. It could be a renamed file or an original, which has been incidentally named with numbers. I use another scheme: if a file already exists with the same name, move the old file to another directory. For example, from c:\backup to c:\backup0. If there is a copy there, too, try c:\backup1, c:\backup2, ..., until a safe place is found. Here you have options: keep only a certain number of versions or keep creating these c:\backupXXX directories indefinitely. In the former case you can have again different strategies: replace the oldest file, replace the one, which makes the versions the most evenly paced in time or in version number, etc.

shimanov
  • Members
  • 610 posts
  • Last active: Jul 18 2006 08:35 PM
  • Joined: 25 Sep 2005
Here is an alternative. It creates a directory (e.g., 2005.10.07 - 13:10), then copies all files from the source to that directory. It is not what you requested, but it does have its merits. For example:

* easy sorting and identification
* simple handling and validation of processing
* leaves file names intact for clear identification and use

Try it. You may like it.

dir_source = c:\lab 
dir_target = c:\lab\archive

pattern = *

suffix := a_YYYY "." a_MM "." a_DD " - " a_Hour "." a_Min "." a_Sec

FileCreateDir, %dir_target%\%suffix%

loop, %dir_source%\%pattern%
	FileCopy, %A_LoopFileLongPath%, %dir_target%\%suffix%\%a_LoopFileName%
return


enrica
  • Members
  • 117 posts
  • Last active: Feb 26 2016 11:15 PM
  • Joined: 21 Mar 2005
Hi Laszlo, thank you.
I'm interesting about your method, can you give me an running example?

Shimanov, thank you.
until today, I was used this method:
FileCopyDir, C:\H\, E:\BACKUP\H.Backup.%A_YYYY%.%A_MM%.%A_DD%
sleep, 60000
FileCopyDir, C:\Arquivos de programas\NoteTab Light\Libraries\, E:\BACKUP\Libraries.Backup.%A_YYYY%.%A_MM%.%A_DD%
sleep, 60000
FileCopyDir, ...............................%A_YYYY%.%A_MM%.%A_DD%
sleep, 60000
FileCopyDir, ...............................%A_YYYY%.%A_MM%.%A_DD%
exitapp
But now I have a HD space problem and I can't lost older files...
Ciao!
65.6E.72.69.63.61. (My hovercraft is full of eels)

Laszlo
  • Moderators
  • 4713 posts
  • Last active: Mar 31 2012 03:17 AM
  • Joined: 14 Feb 2005
Here is a very simple version. You probably want to use FileSelectFolder to choose the source directory, which is hard coded together with the backup directory in the sample script below.

First a BB subdirectory is created with the name of the current date and time, as shimanov proposed. (UTC is used, so there will never be a name conflict, not even at change of daylight saving time.) Next, in a loop each file is taken from the source directory. If a file exists with the same name in the backup directory, it is moved to the BB subdirectory, then the source file is copied to the backup directory. Finally, the BB directory is removed, but only if it is empty.
dir_source = c:\btest
dir_backup = c:\backup
FormatTime dir_bb,%A_NowUTC%,yy.MM.dd-HH.mm.ss
FileCreateDir %dir_backup%\%dir_bb%

Loop %dir_source%\*.*
{
   IfExist %dir_backup%\%A_LoopFileName%
      FileMove %dir_backup%\%A_LoopFileName%, %dir_backup%\%dir_bb%\%A_LoopFileName%
   FileCopy %dir_source%\%A_LoopFileName%, %dir_backup%\%a_LoopFileName%
}

FileRemoveDir %dir_backup%\%dir_bb%


enrica
  • Members
  • 117 posts
  • Last active: Feb 26 2016 11:15 PM
  • Joined: 21 Mar 2005
Thank you Laszlo!
But always copy all the file and fill my Hd!

is there a way to compare the files of the daily backup directorys and delete files with same name and timestamp?
Thank you.
Ciao!
65.6E.72.69.63.61. (My hovercraft is full of eels)

Laszlo
  • Moderators
  • 4713 posts
  • Last active: Mar 31 2012 03:17 AM
  • Joined: 14 Feb 2005
This version compares the time the files were last modified. (For simplicity local time is used, which can fool you when daylight saving is changed.)
If there is no backup copy, the source file is copied to the backup directory.
If there is a backup copy and it is the same old, no action is taken.
If there is a backup copy and it is older than the source, the backup file is moved to a new subdirectory and the source file is copied to the backup directory.
dir_source = c:\btest 
dir_backup = c:\backup 
FormatTime dir_bb,%A_NowUTC%,yy.MM.dd-HH.mm.ss 
FileCreateDir %dir_backup%\%dir_bb% 

Loop %dir_source%\*.* 
{ 
   IfExist %dir_backup%\%A_LoopFileName%
   {
      FileGetTime sourceTime, %dir_source%\%A_LoopFileName%
      FileGetTime backupTime, %dir_backup%\%A_LoopFileName%
      IfEqual sourceTime,%backupTime%, Continue
      FileMove %dir_backup%\%A_LoopFileName%, %dir_backup%\%dir_bb%\%A_LoopFileName% 
   }
   FileCopy %dir_source%\%A_LoopFileName%, %dir_backup%\%a_LoopFileName% 
} 

FileRemoveDir %dir_backup%\%dir_bb%


wilcofan
  • Members
  • 10 posts
  • Last active: Mar 22 2016 03:37 PM
  • Joined: 17 Jan 2014

This version compares the time the files were last modified. (For simplicity local time is used, which can fool you when daylight saving is changed.)
If there is no backup copy, the source file is copied to the backup directory.
If there is a backup copy and it is the same old, no action is taken.
If there is a backup copy and it is older than the source, the backup file is moved to a new subdirectory and the source file is copied to the backup directory.

dir_source = c:\btest 
dir_backup = c:\backup 
FormatTime dir_bb,%A_NowUTC%,yy.MM.dd-HH.mm.ss 
FileCreateDir %dir_backup%\%dir_bb% 

Loop %dir_source%\*.* 
{ 
   IfExist %dir_backup%\%A_LoopFileName%
   {
      FileGetTime sourceTime, %dir_source%\%A_LoopFileName%
      FileGetTime backupTime, %dir_backup%\%A_LoopFileName%
      IfEqual sourceTime,%backupTime%, Continue
      FileMove %dir_backup%\%A_LoopFileName%, %dir_backup%\%dir_bb%\%A_LoopFileName% 
   }
   FileCopy %dir_source%\%A_LoopFileName%, %dir_backup%\%a_LoopFileName% 
} 

FileRemoveDir %dir_backup%\%dir_bb%

Thanks so much for this I have included this in a script I have working.  Lost some data this morning so this was a nice improvement.