⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 adobackup.pas

📁 数据备份与恢复源码,给需要的一个参考,可以直接运行
💻 PAS
📖 第 1 页 / 共 3 页
字号:
  RemoveDir(FTempPath);
  Result := True;
end;

function TADOBackup.CopytoTemp(SourcePath: string): Boolean;
var
  FindResult: Integer;
  mSearchRec: TSearchRec;
  tmpSource, tmpTarget, TempFileName: string;
  mReadFile: file of TFloppyFileHead;
  mFloppyFileHead: TFloppyFileHead;
begin
  Result := False;
  FindResult := FindFirst(SourcePath + '*.*', faAnyFile + faHidden + faSysFile, mSearchRec);
  try
    while FindResult = 0 do
    begin
      TempFileName := mSearchRec.Name;
      if Copy(mSearchRec.Name, 1, Pos('.', FBackUpFileName) - 1) <>
        Copy(FBackUpFileName, 1, Pos('.', FBackUpFileName) - 1) then
      begin
        FindResult := FindNext(mSearChRec);
        Continue;
      end;

      tmpSource := SourcePath + TempFileName; // 从工作路径里提出要拷贝的分割后的文件
      Assignfile(mReadFile, tmpSource);
      ReSet(mReadFile);
      try
        if not Eof(mReadFile) then
          Read(mReadFile, mFloppyFileHead);
        if mFloppyFileHead.ID = 'JSFB' then
          if mFloppyFileHead.SerialNo = FFloppyFileHead.SerialNo then
            FFileHeadList[mFloppyFileHead.CurFloppyNo - 1] := mFloppyFileHead;
      finally
        CloseFile(mReadFile);
      end;

      tmpTarget := FTempPath + TempFileName;
      if not CopyFile(PChar(tmpSource), PChar(tmpTarget), False) then
        raise Exception.Create('拷贝失败!');
      FindResult := FindNext(mSearChRec);
    end;
  finally
    findclose(mSearchRec);
  end;
end;

function TADOBackup.CopyfromAtoTemp: Boolean; // 将文件从A转移至临时目录
var
  FloppyCount, CurFloppy: integer;
  SerialNos: Double;
  SFile: string;
  tmpS: string;
  tempFileName: string;
  Excursion: Byte;
  ErrorFlag: Boolean;
 // ErrorCodes: DWord;
  FHandle: Integer;
begin
  Result := True;
  ErrorFlag := False;
    // 初始化包信息
  FloppyCount := 1; // 包个数
  CurFloppy := 1; // 当前包
  SerialNos := -1; // 包序列号
  SetLength(FFileHeadList, 1);
  SFile := StringReplace(FBackUpFileName, '.', InttoStr(1) + '.', [rfReplaceAll, rfIgnoreCase]);
  SFile := extractFilePath(FSourceFile) + SFile;
  while True do
  begin
    if UpperCase(Copy(SFile, 1, 1)) = 'A' then // 提示插入磁盘
    begin
      if CurFloppy <> 1 then
      begin
        if Application.MessageBox(PChar('请在 A:\ 驱中放入 ' + IntToStr(CurFloppy) +
          ' 号数据盘。'), '恢复数据', MB_YESNO + MB_ICONWARNING) = ID_NO then
        begin
          tmpS := '取消操作';
          FErrorType := etCancel;
          ErrorFlag := False;
          Break;
        end;
      end;
    end;

    TempFileName := ExtractFileName(SFile);
    Excursion := Length(inttostr(CurFloppy)) + 1; // 计算.偏移位数

    if CurFloppy = 1 then
      SFile := ExtractFilePath(SFile) + Copy(TempFileName, 1, pos('.', TempFileName) - Excursion)
        + ExtractFileExt(SFile)
    else if CurFloppy = 2 then
      SFile := ExtractFilePath(SFile) + Copy(TempFileName, 1, pos('.', TempFileName) - 1)
        + Inttostr(CurFloppy) + ExtractFileExt(SFile)
    else
      SFile := ExtractFilePath(SFile) + Copy(TempFileName, 1, pos('.', TempFileName) - Excursion)
        + Inttostr(CurFloppy) + ExtractFileExt(SFile);

    if FileExistS(SFile) then
    begin
      FHandle := FileOpen(SFile, fmOpenRead or fmShareDenyNone);
      if FHandle = -1 then
      begin
        tmpS := '读备份数据 ' + IntToStr(CurFloppy) + ' 号盘错误, ';
        ErrorFlag := True;
      end;
       //将文件抬头读入 FFileHeadList
      if FileRead(FHandle, FFileHeadList[CurFloppy - 1], Sizeof(FFileHeadList[CurFloppy - 1])) = -1 then
      begin
        tmpS := '读备份数据 ' + IntToStr(CurFloppy) + ' 号盘错误, ';
        ErrorFlag := True;
      end;
      FileClose(FHandle);

      if not ErrorFlag then
      begin
        if (SerialNos <> FFileHeadList[CurFloppy - 1].SerialNo) and
          (SerialNos <> -1) then
        begin
          tmpS := '备份数据 ' + IntToStr(CurFloppy) + ' 号盘序列号错误, ';
          ErrorFlag := True;
        end;
        if not ErrorFlag then
        begin
          if CurFloppy <> FFileHeadList[CurFloppy - 1].CurFloppyNo then
          begin
            tmpS := '不是备份数据 ' + IntToStr(CurFloppy) + ' 号盘, ';
            ErrorFlag := True;
          end
          else
          begin
            // 读取备份包文件个数,并赋给 FFileHeadList 个数
            FloppyCount := FFileHeadList[CurFloppy - 1].FloppyCount;
            if CurFloppy = 1 then // 只赋值一次即可
              SetLength(FFileHeadList, FloppyCount);
            if SerialNos = -1 then // 只赋值一次即可,作为全局参照
            begin
              SerialNos := FFileHeadList[CurFloppy - 1].SerialNo;
            end;
          end;
        end;
      end;
    end
    else
    begin
      tmpS := '没有发现备份数据 ' + IntToStr(CurFloppy) + ' 号盘, ';
      ErrorFlag := True;
    end;

    if not ErrorFlag then
    begin
      if not CopyFile(PChar(SFile), PChar(fTempPath + ExtractFileName(SFile)), False) then
        ErrorFlag := True;
    end;

    if ErrorFlag then
    begin
      if Application.MessageBox(PChar('拷贝失败: ' + tmpS + '重新拷贝吗?'),
        '错误', MB_OKCANCEL + MB_ICONWARNING) = mrCancel then
      begin
        FErrorType := etCancel;
        Break;
      end
      else
      begin
        ErrorFlag := False; // 仍对当前包进行循环操作
      end;
    end
    else // 可进行下个包的拷贝
    begin
      Inc(CurFloppy);
      if CurFloppy > FloppyCount then
      begin
        tmpS := '拷贝完毕';
        Break;
      end;
    end;

  end;
  if ErrorFlag then
    Result := False;
end;

function TADOBackup.Backup(SavePath: string): Boolean;
var
  mADODataSet: TADODataSet;
  i, InfoLen, TotalCount: integer;
  TableStrings: TStrings;
  BackUpStream: TFileStream;
  fn, InfoText: string;
begin
  Result := False;
  if StartWrong then
    Exit;
  if SavePath = '' then
  begin
    Application.MessageBox('路径无效,请检查!', '系统信息', 0 + mb_Iconinformation);
    exit;
  end;
  FSavepath := SavePath;
  if FSavepath[Length(FSavepath)] <> '\' then
    FSavepath := FSavepath + '\';

  if DirectoryExists(FTempPath) then
    RemoveDir(FTempPath);
  Forcedirectories(FTempPath); // 建立临时路径
  Forcedirectories(FSavePath); // 建立备份路径

  FSqlStrings.SaveToFile(FTempPath + fn_SQLFile); // 储存SQL脚本
  TableStrings := GetTablename(FSqlStrings, rtTable); // 提取表名用以建立文件

  mADODataSet := TADODataSet.Create(nil);
  mADODataSet.Connection := FADOConnection;
  TableMaps.Clear;
  TotalCount := FSqLStrings.Count - 1;
  try
    for i := 0 to FSQLStrings.Count - 1 do
    begin
      mADODataSet.Close;
      mADODataSet.CommandText := FSQLStrings[i];
      mADODataSet.Open;
      fn := GenerateTempName(FTempPath);
      TableMaps.Add(fn);
      mADODataSet.SaveToFile(FTempPath + fn, pfxml);
      if TotalCount = 0 then
        FRadio := 100
      else
        FRadio := (100 * i div TotalCount);
      if Assigned(FRatioChanged) then
        FRatioChanged(Self, fRadio);
    end;
    TableMaps.SaveToFile(FTempPath + fn_TableMap);
    TableStrings.Clear;
    TableStrings.Add(FTempPath + '*.*');

    if UpperCase(Copy(FSavePath, 1, 1)) = 'A' then
    begin
      FIsSavetoA := True;
      FWorkPath := FTempPath;
      FIsDevide := True; // 如是A盘,自动分卷
    end
    else
    begin
      FIsSavetoA := False;
      FWorkPath := FSavePath;
    end;

    //!!!!!!!!!!!!!!! ForigPath????????????????
    if FIsDevide then
      fn := FOrigPath + FBackUpFileName
    else
      fn := FSavePath + FBackUpFileName;
    BackUpStream := TFileStream.Create(fn, fmCreate);
    BackUpStream.Size := 0;
    // 首先写入备份标志,表明本文件为备份文件
    BackUpStream.Write(BackUpSign[1], length(BackUpSign));
    InfoText := FInfo.Text;
    InfoLen := Length(InfoText);
    // 写入附加信息长度
    BackUpStream.write(InfoLen, sizeof(Integer));
    // 写入附加信息
    BackUpStream.write(InfoText[1], InfoLen);
    try
      BackUpFile.BackupToStream(TableStrings, BackUpStream); // 将多文件压缩整理为单一文件
    finally
      BackUpStream.free;
    end;
    if FIsDevide then
    begin
      CopyDevide;
      DeleteFile(fn);
    end
  finally
    mADODataSet.free;
    RemoveDir(FTempPath);
  end;
  Result := True;
end;

procedure TADOBackup.PreviewFile(Sender: TOpenPreviewDialog; FileName: string);
begin
  if GetInfo(FileName) then
  begin
    Sender.PreviewText.Lines.Assign(FInfo);
    Sender.PreviewText.Font.Color := clBlack;
    Sender.Hint := '备份数据内容:';
  end
  else
  begin
    Sender.Hint := '文件格式错误';
    Sender.PreviewText.Lines.Clear;
    Sender.PreviewText.Lines.Add('无法识别的文件格式:' + #13#10 + '  此文件不是备份文件');
    Sender.PreviewText.Font.Color := clRed;
  end;
end;

function TADOBackup.BrowseBackFile: string;
var
  OpenDlg: TOpenPreviewDialog;
begin
  OpenDlg := TOpenPreviewDialog.Create(self);
  OpenDlg.Title := '指定需恢复的备份文件';
  OpenDlg.Filter := '数据备份文件(*.*)|*.*';
  OpenDlg.OnPreview := PreviewFile;
  Result := '';
  try
    if OpenDlg.Execute then
      Result := OpenDlg.FileName;
  finally
    OpenDlg.Free;
  end;
end;

constructor TADOBackup.Create(AOwner: TComponent);
var
  TempDir: array[0..255] of Char;
begin
  inherited Create(AOwner);
  PackSize := 1456000;
  FBackupFileName := 'Save.dat';

  GetTempPath(255, @TempDir);
  FTempPath := StrPas(TempDir) + Extractfilename(FBackUpFileName) + '\';
  FOrigPath := StrPas(TempDir) + 'TempBack\';
  Forcedirectories(FOrigPath);
  FIsSavetoA := False;
  BackupFile := TBackupFile.Create(nil);
  FSQLStrings := TStringList.Create;
  FDelSQLStrings := TStringlist.Create;
  FInfo := TStringlist.Create;
  TableMaps := TStringlist.Create;
  FIsDevide := True;
  FAutoDel := True;
  FPackSize := 1456000;
end;

destructor TADOBackup.Destroy;
begin
  RemoveDir(FOrigPath);
  BackupFile.free;
  FDelSQLStrings.Free;
  FSQLStrings.free;
  FInfo.Free;
  TableMaps.Free;
  inherited Destroy;
end;

initialization
  Randomize;
end.

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -