1

How to take backup of older version files while the older version is uninstalled and new version is installed using Inno Setup compiler?

[Files] 
Source: "RecordUpdateProcessor\*"; DestDir: "{app}\RecordUpdateProcessor"; \
    Flags: ignoreversion recursesubdirs createallsubdirs; \
    Source: "setup\nssm.exe*"; DestDir: "{app}\"; Flags: ignoreversion

and in run section using nssm service has been installed.

Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992
Saranya
  • 11
  • 3

1 Answers1

1

There's no built-in support for this.

You can implement it using Pascal Script.

Using BeforeInstall function is probably the best way to go.

[Files]
Source: "RecordUpdateProcessor*"; DestDir: "{app}\RecordUpdateProcessor"; \
    Flags: ignoreversion recursesubdirs createallsubdirs; \
    Source: "setup\nssm.exe*"; DestDir: "{app}\"; \
    BeforeInstall: BackupBeforeInstall

[Code]

function BackupFolderPath: string;
begin
  Result := AddBackslash(ExpandConstant('{app}')) + 'Backup';
end;

procedure BackupBeforeInstall;
var
  SourcePath: string;
  DestPath: string;
begin
  if not DirExists(BackupFolderPath) then
  begin
    Log('Creating ' + BackupFolderPath);
    CreateDir(BackupFolderPath);
  end;

  SourcePath := ExpandConstant(CurrentFileName);
  DestPath := BackupFolderPath + '\' + ExtractFileName(SourcePath);
  Log(Format('Backing up %s to %s', [SourcePath, DestPath]));
  if not FileCopy(SourcePath, BackupFolderPath + '\' + ExtractFileName(CurrentFileName), False) then
  begin
    Log('Backup failed');
  end;
end;

procedure CurUninstallStepChanged(CurUninstallStep: TUninstallStep);
var
  FindRec: TFindRec;
  SourceDir: string;
  SourcePath: string;
  DestDir: string;
  DestPath: string;
begin
  if CurUninstallStep = usPostUninstall then
  begin
    SourceDir := BackupFolderPath;
    DestDir := RemoveBackslash(ExpandConstant('{app}'));
    if FindFirst(SourceDir + '\*.*', FindRec) then
    begin
      repeat
        if (FindRec.Name <> '.') and (FindRec.Name <> '..') then
        begin
          SourcePath := SourceDir + '\' + FindRec.Name;
          DestPath := DestDir + '\' + FindRec.Name
          Log(Format('Restoring %s to %s', [SourcePath, DestPath]));
          if not RenameFile(SourcePath, DestPath) then
          begin
            Log('Restore failed');
          end;
        end;
      until not FindNext(FindRec);
    end;  
    FindClose(FindRec);

    RemoveDir(SourceDir);
  end;
end;

Note the the code needs improving, if you need a recursive backup. The recursesubdirs suggests that. On the contrary setup\nssm.exe* does not look like it could match folders.

Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992