Free Pascal 调用bash命令,见:如何使用free pascal 调用bash命令-CSDN博客
BorgBackup软件备份:学习使用备份软件BorgBackup-CSDN博客
那么怎么在 Free Pascal 程序中调用BorgBackup进行备份操作呢?
为了简化操作,只针对备份使用FreePascal代码,前期的初始化,我们还是老老实实在bash里执行
先初始化BorgBackup
borg init --encryption=none skywalk@192.168.1.5:/home/skywalk/macbak
执行备份
borg create --stats skywalk@192.168.1.5:/home/skywalk/macbak::obbak /Users/skywalk/obsidian/ /usr/local/etc
FreePascal代码,写在borgbak.fpc文件里:
program LargeOutputDemo;
{$mode objfpc}{$H+}
uses
Classes, SysUtils, Process; // Process is the unit that holds TProcess
const
BUF_SIZE = 2048; // Buffer size for reading the output in chunks
var
AProcess : TProcess;
OutputStream : TStream;
BytesRead : longint;
Buffer : array[1..BUF_SIZE] of byte;
begin
// Set up the process; as an example a recursive directory search is used
// because that will usually result in a lot of data.
AProcess := TProcess.Create(nil);
// The commands for Windows and *nix are different hence the $IFDEFs
{$IFDEF Windows}
// In Windows the dir command cannot be used directly because it's a build-in
// shell command. Therefore cmd.exe and the extra parameters are needed.
AProcess.Executable := 'c:\windows\system32\cmd.exe';
AProcess.Parameters.Add('/c');
AProcess.Parameters.Add('dir /s c:\windows');
{$ENDIF Windows}
{$IFDEF Unix}
// AProcess.Executable := '/bin/ls';
AProcess.Executable := '/usr/local/bin/borg';
AProcess.Parameters.Add('create --stats skywalk@192.168.1.5:/home/skywalk/macbak::obbak /Users/skywalk/obsidian/ /usr/local/etc < pass.txt');
// AProcess.Executable := 'ls';
{$IFDEF Darwin}
// AProcess.Parameters.Add('-recursive');
// AProcess.Parameters.Add('-all');
{$ENDIF Darwin}
{$IFDEF Linux}
AProcess.Parameters.Add('--recursive');
AProcess.Parameters.Add('--all');
{$ENDIF Linux}
{$IFDEF FreeBSD}
// AProcess.Parameters.Add('-R');
// AProcess.Parameters.Add('-a');
{$ENDIF FreeBSD}
// AProcess.Parameters.Add('-l');
{$ENDIF Unix}
// Process option poUsePipes has to be used so the output can be captured.
// Process option poWaitOnExit can not be used because that would block
// this program, preventing it from reading the output data of the process.
AProcess.Options := [poUsePipes];
// Start the process (run the dir/ls command)
AProcess.Execute;
// Create a stream object to store the generated output in. This could
// also be a file stream to directly save the output to disk.
OutputStream := TMemoryStream.Create;
// All generated output from AProcess is read in a loop until no more data is available
repeat
// Get the new data from the process to a maximum of the buffer size that was allocated.
// Note that all read(...) calls will block except for the last one, which returns 0 (zero).
BytesRead := AProcess.Output.Read(Buffer, BUF_SIZE);
// Add the bytes that were read to the stream for later usage
OutputStream.Write(Buffer, BytesRead)
until BytesRead = 0; // Stop if no more data is available
// The process has finished so it can be cleaned up
AProcess.Free;
// Now that all data has been read it can be used; for example to save it to a file on disk
with TFileStream.Create('output.txt', fmCreate) do
begin
OutputStream.Position := 0; // Required to make sure all data is copied from the start
CopyFrom(OutputStream, OutputStream.Size);
Free
end;
// Or the data can be shown on screen
with TStringList.Create do
begin
OutputStream.Position := 0; // Required to make sure all data is copied from the start
LoadFromStream(OutputStream);
writeln(Text);
writeln('--- Number of lines = ', Count, '----');
Free
end;
// Clean up
OutputStream.Free;
end.
已经将用户的密码写到pass.txt文件中。
现在还没调通,主要是命令需要交互的信息比较多,比如scp的密码啊,还有一些“yes”确认等。
不过后面调通问题不大。
调试
执行备份时报错
borg create: error: argument ARCHIVE: Invalid location format: "skywalk@192.168.1.5:/home/skywalk/macbak::obbak /Users/skywalk/obsidian/"
发现时命令里的空格有问题,改成半角空格,搞定
sudo borg create --stats skywalk@192.168.1.5:/home/skywalk/macbak::obbak /Users/skywalk/obsidian/ /usr/local/etc