我以前写的备份模块,可惜后来推翻了,因为功能太次了,不过还是有借鉴意义的哦:[
]
private
{ Private declarations }
procedure initpack(ss:string); //初始化打包文件
procedure packfile(ss:string); //把数据文件打入包
procedure depackfile(ss:string); //对打包文件解包
procedure backupto(ss:string); //备份到指定的路径
function chkdisk(drive:byte;bbsize:longint):boolean;//检查磁盘空间
function chkfile(ss:string):boolean;//检查文件
public
{ Public declarations }
end;
var
backupmain: Tbackupmain;
implementation
uses helpspiritu, datamodulemainu;
{$R *.DFM}
//文件打包解包准备
procedure tbackupmain.initpack(ss:string);
var
vf:file;
begin
assignfile(vf,ss);
rewrite(vf,1);
closefile(vf);
end;
procedure tbackupmain.packfile(ss:string);
var
rf,pf:file;
fsize:longint;
numread,numwrite:integer;
ss1,ss2,ss3:string[100];
buf:array [1..2048] of char;
begin
assignfile(pf,extractfilepath(application.exename)+'hmpack.pak');
assignfile(rf,ss);
reset(pf,1); //打开打包文件
reset(rf,1); //打开数据文件
fsize:=filesize(pf);
seek(pf,fsize);//从打包文件尾部开始插入
fsize:=filesize(rf);
seek(rf,0);
ss1:='change';//打包文件标志
ss2:=ss;
str(fsize,ss3);//数据文件长度转化为字符串
blockwrite(pf,ss1,10);
blockwrite(pf,ss2,60);
blockwrite(pf,ss3,20);//写数据文件参数标志入打包文件
repeat //开始打包
blockread(rf,buf,sizeof(buf),numread);
blockwrite(pf,buf,numread,numwrite);
until (numread=0) or(numwrite<>numread);
closefile(pf);
closefile(rf);
end;
procedure tbackupmain.backupto(ss:string);
var
sf,df:file;
nnn,nn,basesize,srcfilesize:longint;
dnum,vi,bn:integer;
ss1:string[200];
ssdnum,sste,ssattr:string[20];
// vbyte:char;
buf:array [1..2048] of char;
myreg:tregistry;
begin
assignfile(sf,ss);
reset(sf,1);
srcfilesize:=filesize(sf);
basesize:=1430000;//设置a盘大小为1430000
dnum:=ceil(srcfilesize/basesize);//需要几张软盘
str(dnum,ssdnum);
randomize;
vi:=random(1000);//随机数,为了证明是同一次备份
str(vi,ssattr);
for vi:=1 to dnum do
begin
str(vi,sste);
sste:=trim(sste);
ss1:='共需'+ssdnum+'张软磁盘,'+'请插入第'+sste+'张盘';
bn:=messagedlg(ss1,mtinformation,[mbok,mbcancel],0);
if bn<>mrok then
begin
messagebox(getactivewindow,pchar('备份被终止!'),pchar('人事管理系统v0.0.2'),mb_ok);
backupmain.ProgressBar1.Position :=0;
break;
end;
// 暂时只提供a:备份,直接用a:
// ss1:=uppercase(trim(backupmain.edit1.text));
// vbyte:=ss[1];
// bn:=ord(vbyte)-ord('A')+1;
bn:=1;
//检查a磁盘空间
if chkdisk(bn,basesize)=false then break;
//复制打包文件为备份文件
// 暂时只提供a:备份,直接用a:
// ss1:=trim(backupmain.edit1.text)+'/__'+sste+'disk.bak';
ss1:='A:/__'+sste+'disk.bak';
assignfile(df,ss1);
rewrite(df,1);
ss1:='hillman001';
blockwrite(df,ss1,16);
blockwrite(df,ssdnum,16);
blockwrite(df,ssattr,16);//项背分文件写标志
srcfilesize:=basesize;
repeat//开始备份
if srcfilesize>2048 then
begin
nn:=2048;
srcfilesize:=srcfilesize-2048;
end
else
begin
nn:=srcfilesize;
srcfilesize:=0;
end;
blockread(sf,buf,nn,nnn);
blockwrite(df,buf,nnn);
until srcfilesize<=0;
closefile(df);
backupmain.ProgressBar1.StepBy(floor(60/dnum));
end;
closefile(sf);
//
myreg:=tregistry.Create ;
try
myreg.RootKey :=HKEY_CURRENT_USER;
myreg.OpenKey('/software/hillman/ems/backup',false);
myreg.WriteBool('took',true);
finally
myreg.CloseKey;
myreg.Free;
end;
backupmain.ProgressBar1.Position :=100;
messagebox(getactivewindow,pchar('备份操作结束!'),pchar('人事管理系统v0.0.2'),mb_ok);
end;
function tbackupmain.chkfile(ss:string):boolean;
var
bn:integer;
begin
result:=false;
while fileexists(ss)=false do
begin
bn:=messagebox(getactivewindow,pchar('备份文件不存在!继续吗?'),pchar('人事管理系统v0.0.2'),mb_yesno);
if bn<>idyes then
begin
result:=false;
messagebox(getactivewindow,pchar('恢复备份操作被终止!'),pchar('人事管理系统v0.0.2'),mb_ok);
backupmain.ProgressBar1.Position :=0;
break;
end;
end;
end;
function tbackupmain.chkdisk(drive:byte;bbsize:longint):boolean;
var
bn,chk:integer;
mysize:longint;
begin
result:=true;
chk:=0 ;
while chk=0 do
begin
mysize:=diskfree(drive);
if mysize=-1 then
begin
bn:=messagebox(getactivewindow,pchar('奇怪,怎么没有发现指定的磁盘,请检查!'),pchar('人事管理系统v0.0.2'),mb_okcancel);
if bn=idok then continue
else
begin
result:=false;
messagebox(getactivewindow,pchar('备份或恢复操作被终止!'),pchar('人事管理系统v0.0.2'),mb_ok);
backupmain.ProgressBar1.Position :=0;
break;
end;
end
else
begin
if mysize<bbsize then
begin
bn:=messagebox(getactivewindow,pchar('可惜,磁盘怎么会空间不足,请换磁盘!'),pchar('人事管理系统v0.0.2'),mb_okcancel);
if bn=idok then continue
else
begin
result:=false;
messagebox(getactivewindow,pchar('备份或恢复操作被终止!'),pchar('人事管理系统v0.0.2'),mb_ok);
backupmain.ProgressBar1.Position :=0;
break;
end;
end
else
break;
end;
end;
end;
procedure tbackupmain.depackfile(ss:string);
var
sf,df:file;
ss1,ss2,ss3:string[100];
fsize:longint;
numread,numwrite,nn:integer;
buf:array [1..2048] of char;
myreg:tregistry;
begin
assignfile(sf,ss);
reset(sf,1);//打开包文件
while not eof(sf) do
begin
blockread(sf,ss1,10,nn);
blockread(sf,ss2,60,nn);
blockread(sf,ss3,20,nn);
if nn<20 then //实际读如果是备份文件,一定能读出20,而如果不是备份则可能没有足够20而且文件已经到了尾部
begin
messagebox(getactivewindow,pchar('文件尺寸不匹配!'),pchar('人事管理系统v0.0.2'),mb_ok);
closefile(sf);
exit;
end;
fsize:=strtoint(ss3);
if ss1<>'change' then
begin
messagebox(getactivewindow,pchar('文件内部标志不匹配!'),pchar('人事管理系统v0.0.2'),mb_ok);
closefile(sf);
exit;
end;
assignfile(df,trim(ss2));//解开第一个文件
rewrite(df,1);
repeat
if fsize>2048 then
begin
nn:=2048;
fsize:=fsize-2048;
end
else
begin
nn:=fsize;
fsize:=0;
end;
blockread(sf,buf,nn,numread);
blockwrite(df,buf,numread,numwrite);
until (fsize=0)or (numwrite<>numread);
closefile(df);
end;
closefile(sf);
//
myreg:=tregistry.Create ;
try
myreg.RootKey :=HKEY_CURRENT_USER;
myreg.OpenKey('/software/hillman/ems/backup',false);
myreg.WriteBool('fromok',true);
finally
myreg.CloseKey;
myreg.Free;
end;
backupmain.ProgressBar1.Stepby(40);
messagebox(getactivewindow,pchar('恢复操作结束!'),pchar('人事管理系统v0.0.2'),mb_ok);
end;
//其他
procedure Tbackupmain.FormKeyDown(Sender: TObject; var Key: Word;
Shift: TShiftState);
begin
//调出帮助精灵
if key=$70 then
begin
if helpspirit.Visible=true then
helpspirit.Hide;
helpspirit.puthelpout('数据备份');
helpspirit.show;
end;
end;
procedure Tbackupmain.Button1Click(Sender: TObject);
//var
// path:string;
begin
//选择备份或恢复的路径
// path:=getcurrentdir;
// if selectdirectory(path,[sdAllowCreate,sdPrompt],0) then
// backupmain.Edit1.Text :=path;
end;
procedure Tbackupmain.BitBtn1Click(Sender: TObject);
var
myreg:tregistry;
dbpath:string;
//恢复备份的变量
sf,df:file;
ss:string;
ss1,ssattr:string[30];
numread,numwrite,bn,nn,i:integer;
buf:array [1..2048] of char;
begin
//察看条件是否成立
// if (not backupmain.RadioButton1.Checked)and(not backupmain.RadioButton2.Checked) then
// begin
// messagebox(getactivewindow,pchar('需要选择备份或恢复备份的路径。'),pchar('人事管理系统v0.0.2'),mb_ok);
// exit;
// end
// else if backupmain.RadioButton2.Checked and (backupmain.Edit1.Text='') then
// begin
// messagebox(getactivewindow,pchar('需要选择备份或恢复备份的路径。'),pchar('人事管理系统v0.0.2'),mb_ok);
// backupmain.Button1.SetFocus;
// exit;
// end;
//纪录备份路径
myreg:=tregistry.Create;
try
myreg.RootKey :=HKEY_CURRENT_USER;
myreg.OpenKey('/software/hillman/db/alias',false);
dbpath:=myreg.ReadString('path');
myreg.CloseKey;
myreg.OpenKey('/software/hillman/ems/backup',false);
if backupmain.RadioButton3.Checked then
begin
// 暂时只提供a:备份,直接用a:
// if backupmain.RadioButton2.Checked then
// myreg.WriteString('todir',backupmain.edit1.text)
// else if backupmain.RadioButton1.Checked then
myreg.WriteString('todir','A:');
myreg.WriteString('totime',datetimetostr(now));
myreg.WriteBool('took',false);
end
else if backupmain.RadioButton4.Checked then
begin
// if backupmain.RadioButton2.Checked then
// myreg.WriteString('fromdir',backupmain.edit1.text)
// else if backupmain.RadioButton1.Checked then
myreg.WriteString('fromdir','A:');
myreg.WriteString('fromtime',datetimetostr(now));
myreg.WriteBool('fromok',false);
end;
finally
myreg.CloseKey;
myreg.Free;
end;
//开始备份或恢复
backupmain.BitBtn1.Enabled :=false;
//
if backupmain.RadioButton3.Checked then
begin
backupmain.initpack(extractfilepath(application.exename)+'hmpack.pak');
for i:=0 to 3 do
begin
backupmain.packfile(dbpath+'/'+datamodulemain.tables
+'.db');
backupmain.ProgressBar1.StepBy(5);
end;
backupmain.backupto(extractfilepath(application.exename)+'hmpack.pak');
//
backupmain.progressbar1.position:=0;
end
else if backupmain.RadioButton4.Checked then
begin
//度备份文件
// 暂时只提供a:备份,直接用a:
//ss:=trim(backupmain.edit1.text)+'/__1disk.bak';
ss:='A:/__1disk.bak';
if not fileexists(ss) then
begin
messagebox(getactivewindow,pchar('备份文件不存在,恢复操作被终止!'),pchar('人事管理系统v0.0.2'),mb_ok);
exit;
end;
assignfile(df,extractfilepath(application.exename)+'hmpack.pak');
rewrite(df,1);
assignfile(sf,ss);
reset(sf,1);
blockread(sf,ss1,16);
if ss1<>'hillman001' then
begin
messagebox(getactivewindow,pchar('备份被终止!'),pchar('人事管理系统v0.0.2'),mb_ok);
closefile(sf);
exit;
end;
blockread(sf,ss1,16);
nn:=strtoint(ss1);
blockread(sf,ssattr,16);
//复制第一张盘
repeat
blockread(sf,buf,sizeof(buf),numread);
blockwrite(df,buf,numread,numwrite);
until (numread=0)or(numwrite<>numread);
closefile(sf);
backupmain.ProgressBar1.StepBy(floor(60/nn));
//度去备份初始化结束
//将备份读出临时包
if nn>1 then
begin
for bn:=2 to nn do
begin
messagebox(getactivewindow,pchar('请插入第'+inttostr(bn)+'张磁盘!'),pchar('人事管理系统v0.0.2'),mb_ok);
// 暂时只提供a:备份,直接用a:
// ss:=trim(backupmain.edit1.text)+'/__'+trim(inttostr(bn))+'disk.bak';
ss:='A:/__'+trim(inttostr(bn))+'disk.bak';
if chkfile(ss)=false then break;
assignfile(sf,ss);
reset(sf,1);
blockread(sf,ss1,16);
if ss1<>'hillman001' then
begin
messagebox(getactivewindow,pchar('不是该系统的备份文件,恢复操作被终止!'),pchar('人事管理系统v0.0.2'),mb_ok);
closefile(sf);
closefile(df);
exit;
end;
blockread(sf,ss1,16);
blockread(sf,ss1,16);
if ss1<>ssattr then
begin
messagebox(getactivewindow,pchar('不是同一次的备份文件,恢复操作被终止!'),pchar('人事管理系统v0.0.2'),mb_ok);
closefile(sf);
closefile(df);
exit;
end;
repeat
blockread(sf,buf,sizeof(buf),numread);
blockwrite(df,buf,numread,numwrite);
until (numread=0)or(numwrite<>numread);
closefile(sf);
backupmain.ProgressBar1.StepBy(floor(60/nn));
end;
end;
closefile(df);
//临时包复制完毕
//对临时包解包
backupmain.depackfile(extractfilepath(application.exename)+'hmpack.pak');
backupmain.ProgressBar1.Position :=0;
end;
backupmain.BitBtn1.Enabled :=true;
end;
procedure Tbackupmain.RadioButton1Click(Sender: TObject);
begin
//显示路径
// if sender=backupmain.RadioButton1 then
// backupmain.Edit1.Text :='A:';
// else if sender=backupmain.RadioButton2 then
// begin
// backupmain.Edit1.Clear;
// backupmain.Button1.SetFocus;
// end;
end;