Bareos 25.1 常用 CLI 命令详解

Bareos 25.1 常用 CLI 命令详解:bconsole 交互全套(status/list/llist/show/messages/run/estimate/cancel/卷生命周期管理/sqlquery)逐条实测,restore 全交互闭环恢复到自定义目录并以 sha256 校验,bls/bextract/bscan 独立工具、配置校验与 systemd 服务管理,含 25.1 命令变化与踩坑实录。

BareosBareos数据备份2026/10/22

Bareos 25.1 常用 CLI 命令详解

目标:系统梳理 Bareos 25.1 的日常命令行操作——以 bconsole 交互式控制台为主线(状态查看、目录库查询、任务运行与取消、备份量估算、卷生命周期管理、全交互恢复流程),配合 bls/bextract/bscan 等独立工具与三守护进程配置校验、systemd 服务管理,并完整实测一次「恢复到临时目录 + sha256 校验」的闭环。全部命令与回显在 Ubuntu Server 22.04.5 LTS + Bareos 25.1.2~pre49.d5742f640 实测通过;安装过程见《Ubuntu 22.04 安装 Bareos 25.1 指南》,配置体系见《Bareos 25.1 配置参数详解》,组件定位与选型背景见《Bareos 介绍与选型指南》。

一、环境信息

本篇所有命令在以下环境实测,回显均为逐行照抄的真实输出:

  • 操作系统:Ubuntu Server 22.04.5 LTS(主机名 bareos-server)
  • Bareos 版本:25.1.2~pre49.d5742f640(2026-10-16 构建,Bareos community build,官方 APT 仓库安装)
  • 组件:Director(bareos-dir,9101)+ Storage Daemon(bareos-sd,9103)+ File Daemon(bareos-fd,9102),本机同时充当备份服务器与客户端
  • 目录数据库:PostgreSQL,库名 bareos、用户 bareos,Catalog 资源名 MyCatalog
  • 配置目录:/etc/bareos/(bareos-dir.d/、bareos-sd.d/、bareos-fd.d/ 三套 + bconsole.conf)
  • 存储介质:File 类型,设备资源 FileStorage,归档目录 /var/lib/bareos/storage
  • 示例环境既有备份任务:backup-bareos-fd(FileSet SelfTest,备份 /usr/sbin,202 个文件约 19.08 MB)、BackupCatalog(备份目录库转储与 /etc/bareos);卷 Full-0001 等存于 Full 池

💡 说明:Bareos 的命令体系分三层——bconsole(连接 Director 的统一管理入口)、独立工具(bls/bextract 等,直接操作卷文件或目录库,不依赖 Director)、以及 systemd 服务管理命令。日常 90% 的操作在 bconsole 内完成,独立工具主要在 Director 不可用或需要底层排障时使用。

二、工具概览

2.1 随包 CLI 工具全景

通过 dpkg -L 与 ls -l 实查各工具的落盘路径与所属 deb 包:

ls -l /usr/bin/bconsole /usr/sbin/b* /usr/bin/bregex /usr/bin/bwild | grep -v btraceback
dpkg -S /usr/bin/bconsole /usr/sbin/bls /usr/sbin/bextract /usr/sbin/bscan /usr/sbin/bareos-dbcheck

实测回显:

-rwxr-xr-x 1 root root 281328 Oct 16 09:19 /usr/bin/bconsole
-rwxr-xr-x 1 root root 211296 Oct 16 09:19 /usr/bin/bregex
-rwxr-xr-x 1 root root 211296 Oct 16 09:19 /usr/bin/bwild
-rwxr-xr-x 1 root root 516368 Oct 16 09:19 /usr/sbin/bareos-dbcheck
-rwxr-xr-x 1 root root 223576 Oct 16 09:19 /usr/sbin/bcopy
-rwxr-xr-x 1 root root 305536 Oct 16 09:19 /usr/sbin/bdedupestimate
-rwxr-xr-x 1 root root 235920 Oct 16 09:19 /usr/sbin/bextract
-rwxr-xr-x 1 root root 235872 Oct 16 09:19 /usr/sbin/bls
-rwxr-xr-x 1 root root 280952 Oct 16 09:19 /usr/sbin/bpluginfo
-rwxr-xr-x 1 root root 265384 Oct 16 09:19 /usr/sbin/bscan
-rwxr-xr-x 1 root root 626072 Oct 16 09:19 /usr/sbin/testfind
bareos-bconsole: /usr/bin/bconsole
bareos-tools: /usr/sbin/bls
bareos-tools: /usr/sbin/bextract
bareos-tools: /usr/sbin/bcopy
bareos-tools: /usr/sbin/bdedupestimate
bareos-tools: /usr/sbin/bpluginfo
bareos-tools: /usr/sbin/testfind
bareos-database-tools: /usr/sbin/bscan
bareos-database-tools: /usr/sbin/bareos-dbcheck

各工具定位一览(关键差异:是否依赖 Director):

工具用途是否需要 Director 运行
bconsole连接 Director 的统一管理控制台,覆盖状态/任务/卷/恢复全部日常操作是(绝大部分命令经 Director 下发)
bls列出卷内 Job 会话/块/文件记录否,直接读卷文件
bextract从卷中直接抽取文件到指定目录(不写目录库)否, Director 宕机时的裸恢复手段
bscan扫描卷内容并重建/校准目录库记录否(直连数据库)
bcopy卷到卷复制数据否,直接读卷
bareos-dbcheck目录库一致性与孤儿记录检查/修复否,直连数据库
bregex / bwild正则/通配符匹配测试(配合 FileSet 编写)否
bdedupestimate估算去重存储(dedup 后端)节省空间否
bpluginfo查看 Bareos 插件信息否
testfind测试 FileSet 匹配哪些文件否

💡 技术拓展:备份类工具的「绕过控制端直接操作介质」能力并非 Bareos 独有设计,同类方案对比——

方案恢复路径特点
Bareos(bconsole restore)Director 编排 → FD 写回走目录库选文件,体验完整、可恢复任意历史版本
Bareos(bextract)直接读卷抽取Director/数据库全挂时仍能取数,但无目录库辅助、只能凭文件名匹配
Bacula(bextract/bscan)与 Bareos 同源同构Bareos 分支继承,命令基本兼容
rsnapshot/rsync 类目录即备份无独立卷格式,ls 即恢复;无增量去重与目录库

Bareos 的卷是带自描述块结构的私有格式,因此即使脱离整套平台也能用独立工具读出数据——本篇 6.2 节将实测这条裸恢复路径。

2.2 命令执行路径

运维人员 ── bconsole ──(TLS-PSK, 9101)──> bareos-dir ──> 目录库(PostgreSQL)
                                            │
                                            ├──> bareos-sd (9103, 读写卷)
                                            └──> bareos-fd (9102, 客户端文件)
独立工具 bls/bextract/bscan ──────────────────────────> 直接读 /var/lib/bareos/storage/<卷>

bconsole 只与 Director 通信(/etc/bareos/bconsole.conf 中定义连接目标),所有查询结果来自目录库、所有任务指令由 Director 分发给 SD/FD。独立工具则绕过这套编排直接读卷或连库——理解这条分界线,后面每个命令「为什么能/不能这么做」就都清楚了。

三、前置条件

3.1 三守护进程运行正常

命令行操作的前提是三个守护进程都在运行。先确认服务状态:

systemctl is-active bareos-director
systemctl is-active bareos-storage
systemctl is-active bareos-filedaemon

实测回显:

active
active
active

⚠️ 注意:Ubuntu 包提供的真实单元名是 bareos-director.service/bareos-storage.service/bareos-filedaemon.service,bareos-dir/bareos-sd/bareos-fd 只是别名(alias)。systemctl is-enabled bareos-dir 会返回 alias 而非 enabled,属正常现象,详见第八章。

3.2 bconsole 能连上 Director

printf "version\nquit\n" | bconsole

实测回显:

Connecting to Director localhost:9101
 Encryption: TLS_CHACHA20_POLY1305_SHA256 TLSv1.3
1000 OK: bareos-dir Version: 25.1.2~pre49.d5742f640 (16 October 2026)
Bareos community build (UNSUPPORTED).
Get professional support from https://www.bareos.com
You are connected using the default console

Enter a period (.) to cancel a command.
version
bareos-dir Version: 25.1.2~pre49.d5742f640 (16 October 2026) Ubuntu 22.04.5 LTS ubuntu Ubuntu 22.04.5 LTS 
You have messages.
quit

看到 1000 OK 即连接成功。You have messages. 表示 Director 里有未读任务消息,进入控制台后用 messages 命令查看(见 4.5 节)。

3.3 目录库中已有可用备份数据

本篇恢复演练依赖已有的备份记录,确认方式(回显为实测环境当前状态):

printf "list jobs\nquit\n" | bconsole

实测回显(执行时已有 JobId 1–12):

list jobs
Automatically selected Catalog: MyCatalog
Using Catalog "MyCatalog"
+-------+------------------+-----------+---------------------+----------+------+-------+----------+------------+-----------+
| jobid | name             | client    | starttime           | duration | type | level | jobfiles | jobbytes   | jobstatus |
+-------+------------------+-----------+---------------------+----------+------+-------+----------+------------+-----------+
|     1 | backup-bareos-fd | bareos-fd | 2026-10-21 17:38:26 | 00:00:01 | B    | F     |      202 | 19,081,236 | T         |
|     2 | RestoreFiles     | bareos-fd | 2026-10-21 17:38:50 | 00:00:02 | R    | F     |      202 | 19,081,236 | T         |
|     3 | backup-bareos-fd | bareos-fd | 2026-10-21 18:15:23 | 00:00:01 | B    | F     |      202 | 19,081,236 | T         |
|     4 | RestoreFiles     | bareos-fd | 2026-10-21 18:22:30 | 00:00:02 | R    | F     |      202 | 19,081,236 | T         |
|     5 | backup-bareos-fd | bareos-fd | 2026-10-21 18:28:19 | 00:00:01 | B    | F     |      202 | 19,081,236 | T         |
|     6 | RestoreFiles     | bareos-fd | 2026-10-21 18:28:59 | 00:00:02 | R    | F     |      202 | 19,081,236 | T         |
|     7 | RestoreFiles     | bareos-fd | 2026-10-21 18:29:32 | 00:00:02 | R    | F     |      202 | 19,081,236 | T         |
|     8 | backup-bareos-fd | bareos-fd | 2026-10-21 21:00:02 | 00:00:01 | B    | I     |        0 |          0 | T         |
|     9 | BackupCatalog    | bareos-fd | 2026-10-21 21:10:02 | 00:00:00 | B    | F     |       69 |    161,670 | T         |
|    10 | backup-bareos-fd | bareos-fd | 2026-10-22 00:48:27 | 00:00:01 | B    | F     |      202 | 19,081,236 | T         |
|    11 | RestoreFiles     | bareos-fd | 2026-10-22 00:50:53 | 00:00:02 | R    | F     |      202 | 19,081,236 | T         |
|    12 | backup-bareos-fd | bareos-fd | 2026-10-22 00:58:31 | 00:00:00 | B    | F     |        0 |          0 | A         |
+-------+------------------+-----------+---------------------+----------+------+-------+----------+------------+-----------+
You have messages.
quit

要点:type 列 B=Backup、R=Restore;level 列 F=Full、I=Incremental;jobstatus 列 T=正常结束、A=被取消。全新环境若 list jobs 无记录,先按《Ubuntu 22.04 安装 Bareos 25.1 指南》跑一次首次备份再演练恢复。

四、bconsole 交互式控制台

4.1 进入、退出与帮助

交互方式直接运行 bconsole(root 或 bareos 用户均可,本环境以 root 执行):

bconsole

进入后提示符为星号 *,退出用 quit 或 exit。help 列出全部命令(实测共 45 条):

*help
  Command            Description
  =======            ===========
  add                Add media to a pool
  autodisplay        Autodisplay console messages
  automount          Automount after label
  cancel             Cancel a job
  configure          Configure director resources
  create             Create DB Pool from resource
  delete             Delete volume, pool or job
  disable            Disable a job/client/schedule
  enable             Enable a job/client/schedule
  estimate           Performs FileSet estimate, listing gives full listing
  exit               Terminate Bconsole session
  export             Export volumes from normal slots to import/export slots
  gui                Switch between interactive (gui off) and non-interactive (gui on) mode
  help               Print help on specific command
  import             Import volumes from import/export slots to normal slots
  label              Label a tape
  list               List objects from catalog
  llist              Full or long list like list command
  messages           Display pending messages
  memory             Print current memory usage
  mount              Mount storage
  move               Move slots in an autochanger
  prune              Prune records from catalog
  purge              Purge records from catalog
  quit               Terminate Bconsole session
  query              Query catalog
  restore            Restore files
  relabel            Relabel a tape
  release            Release storage
  reload             Reload conf file
  rerun              Rerun a job
  resolve            Resolve a hostname
  run                Run a job
  status             Report status
  setbandwidth       Sets bandwidth
  setdebug           Sets debug level
  setdevice          Sets device parameter
  setip              Sets new client address -- if authorized
  show               Show resource records
  sqlquery           Use SQL to query catalog
  time               Print current time
  trace              Turn on/off trace to file
  truncate           Truncate purged volumes
  unmount            Unmount storage
  umount             Umount - for old-time Unix guys, see unmount
  update             Update volume, pool, slots, job or statistics
  use                Use specific catalog
  var                Does variable expansion
  version            Print Director version
  wait               Wait until no jobs are running
  whoami             Print the user name associated with this console

When at a prompt, entering a period (.) cancels the command.

任意命令加参数可查其用法,如 help run:

*help run
  Command            Description
  =======            ===========
  run                Run a job

Arguments:
       job=<job-name> [ jobid=<jobid> ] [ level=<level-name> ] [ since=<time-spec> ]
                       [ client=<client-name> ] [ pool=<pool-name> ] [ fileset=<fileset-name> ]
                       [ storage=<storage-name> ] [ spooldata=<yes|no> ] [ verifyjob=<job-name> ]
                       [ verifylist=<jobid list> ] [ when=<time-spec> ] [ yes ]

When at a prompt, entering a period (.) cancels the command.

除普通命令外还有一组以点号开头的「点命令」(dot commands),输出为机器友好格式,供 WebUI 与脚本调用,.help 可列出全部:

*.help
. no op -- 
.actiononpurge List possible actions on purge -- 
.api Switch between different api modes -- [ 0 | 1 | 2 | off | on | json ] [compact=<yes|no>]
.authorized Check for authorization -- job=<job-name> | client=<client-name> | storage=<storage-name> | schedule=<schedule-name> | pool=<pool-name> | cmd=<command> | fileset=<fileset-name> | catalog=<catalog>
.catalogs List all catalog resources -- 
.clients List all client resources -- [enabled | disabled]
.consoles List all console resources -- 
.users List all user resources -- 
.defaults Get default settings -- job=<job-name> | client=<client-name> | storage=<storage-name> | pool=<pool-name>
.filesets List all filesets -- 
.help Print parsable information about a command -- [ all ] [ item=cmd ] [ full ]
.jobdefs List all job defaults resources -- 
.jobs List all job resources -- [type=<jobtype>] | [enabled | disabled]
.jobstatus List jobstatus information -- [ =<jobstatus> ]
.levels List all backup levels -- 
.locations  -- 
.messages Display pending messages -- 
.media List all medias -- 
.mediatypes List all media types -- 
.msgs List all message resources -- 
.pools List all pool resources -- type=<pooltype>
.profiles List all profile resources -- 
.quit Close connection -- 
.sql Send an arbitrary SQL command -- query=<sqlquery>
.schedule List all schedule resources -- [enabled | disabled]
.status Report status -- dir ( current | last | header | scheduled | running | terminated ) | storage=<storage> [ header | waitreservation | devices | volumes | spooling | running | terminated ] | client=<client> [ header | terminated | running ]
.storages List all storage resources -- [enabled | disabled]
.types List all job types -- 
.volstatus List all volume status -- 
.bvfs_lsdirs List directories using BVFS -- jobid=<jobid> path=<path> | pathid=<pathid> [limit=<limit>] [offset=<offset>]
.bvfs_lsfiles List files using BVFS -- jobid=<jobid> path=<path> | pathid=<pathid> [limit=<limit>] [offset=<offset>]
.bvfs_update Update BVFS cache -- [jobid=<jobid>]
.bvfs_get_jobids Get jobids required for a restore -- jobid=<jobid> | ujobid=<unique-jobid> [all]
.bvfs_versions List versions of a file -- jobid=0 client=<client-name> pathid=<path-id> filename=<file-name> [copies] [versions]
.bvfs_restore Mark BVFS files/directories for restore. Stored in handle. -- path=<handle> jobid=<jobid> [fileid=<file-id>] [dirid=<dirid>] [hardlink=<hardlink>]
.bvfs_cleanup Cleanup BVFS cache for a certain handle -- path=<handle>

(输出节选:.help 还包含 bvfs_getattr 等 BVFS 系列点命令,供 bareos-webui 浏览备份内容使用。)

三个最常用的小命令实测:

*time
Thu 22-Oct-2026 03:48:48
*whoami
root
*version
bareos-dir Version: 25.1.2~pre49.d5742f640 (16 October 2026) Ubuntu 22.04.5 LTS ubuntu Ubuntu 22.04.5 LTS 

💡 说明:命令参数一律用 key=value 形式书写(如 job=backup-bareos-fd);在任何交互提示符下输入英文句点 . 可取消当前命令——help 输出末尾的 "When at a prompt, entering a period (.) cancels the command." 即此约定。

4.2 status:查看守护进程与任务状态

status 是日常巡检第一命令,help status 给出参数:

*help status
  Command            Description
  =======            ===========
  status             Report status

Arguments:
        dir [ header | scheduled | running | terminated | current | last ] |
        client=<client-name> [ header | scheduled | running | terminated | current | last ] |
        storage=<storage-name> [ header | running | terminated | current | last ] |
        all

When at a prompt, entering a period (.) cancels the command.

status dir(Director 自身:计划任务、运行中任务、已结束任务):

*status dir
bareos-dir Version: 25.1.2~pre49.d5742f640 (16 October 2026) Ubuntu 22.04.5 LTS
Daemon started 21-Oct-26 17:40. Jobs: run=10, running=0 db:postgresql, Bareos community binary

Scheduled Jobs:
Level          Type     Pri  Scheduled          Name               Volume
===================================================================================
Incremental    Backup    10  22-Oct-26 21:00    backup-bareos-fd   *unknown*
Full           Backup    11  22-Oct-26 21:10    BackupCatalog      *unknown*
====

Running Jobs:
Console connected at 22-Oct-26 03:49
No Jobs running.
====

Terminated Jobs:
 JobId  Level    Files      Bytes   Status   Finished        Name 
====================================================================
     3  Full        202    19.08 M  OK       21-Oct-26 18:15 backup-bareos-fd
     4              202    19.08 M  OK       21-Oct-26 18:22 RestoreFiles
     5  Full        202    19.08 M  OK       21-Oct-26 18:28 backup-bareos-fd
     6              202    19.08 M  OK       21-Oct-26 18:29 RestoreFiles
     7              202    19.08 M  OK       21-Oct-26 18:29 RestoreFiles
     8  Incr          0         0   OK       21-Oct-26 21:00 backup-bareos-fd
     9  Full         69    161.6 K  OK       21-Oct-26 21:10 BackupCatalog
    10  Full        202    19.08 M  OK       22-Oct-26 00:48 backup-bareos-fd
    11              202    19.08 M  OK       22-Oct-26 00:50 RestoreFiles
    12  Full          0         0   Cancel   22-Oct-26 00:58 backup-bareos-fd


Client Initiated Connections (waiting for jobs):
Connect time        Protocol            Authenticated       Name                                    
====================================================================================================
====
You have messages.
quit

status client(连接客户端 FD,看其版本与任务执行历史):

*status client
Automatically selected Client: bareos-fd
Connecting to Client bareos-fd at localhost:9102
Probing client protocol... (result will be saved until config reload) Handshake: Immediate TLS, Encryption: TLS_CHACHA20_POLY1305_SHA256 TLSv1.3

bareos-server-fd Version: 25.1.2~pre49.d5742f640 (16 October 2026) Ubuntu 22.04.5 LTS
Daemon started 21-Oct-26 17:36. Jobs: run=11 running=0, Bareos community binary
 Sizeof: boffset_t=8 size_t=8 debug=0 trace=0 bwlimit=0kB/s

Running Jobs:
bareos-dir (director) connected at: 22-Oct-26 03:49
No Jobs running.
====

Terminated Jobs:
 JobId  Level    Files      Bytes   Status   Finished        Name 
======================================================================
     2              202    19.08 M  OK       21-Oct-26 17:38 RestoreFiles
     3  Full        202    19.08 M  OK       21-Oct-26 18:15 backup-bareos-fd
     4              202    19.08 M  OK       21-Oct-26 18:22 RestoreFiles
     5  Full        202    19.08 M  OK       21-Oct-26 18:28 backup-bareos-fd
     6              202    19.08 M  OK       21-Oct-26 18:29 RestoreFiles
     7              202    19.08 M  OK       21-Oct-26 18:29 RestoreFiles
     8  Incr          0         0   OK       21-Oct-26 21:00 backup-bareos-fd
     9  Full         69    161.6 K  OK       21-Oct-26 21:10 BackupCatalog
    10  Full        202    19.08 M  OK       22-Oct-26 00:48 backup-bareos-fd
    11              202    19.08 M  OK       22-Oct-26 00:50 RestoreFiles
====
You have messages.
quit

status storage(连接 SD,看驱动器与卷占用;输出较长,此处为首尾节选):

*status storage
Automatically selected Storage: File
Connecting to Storage daemon File at bareos-server:9103
 Encryption: TLS_CHACHA20_POLY1305_SHA256 TLSv1.3

bareos-sd Version: 25.1.2~pre49.d5742f640 (16 October 2026) Ubuntu 22.04.5 LTS 
Daemon started 21-Oct-26 17:36. Jobs: run=11, running=0, Bareos community binary
 Sizes: boffset_t=8 size_t=8 int32_t=4 int64_t=8 bwlimit=0kB/s

Job inventory:

No Jobs running.
====
...(中间为 Terminated Jobs 列表,格式同上)...

Device status:
Autochanger "FileStorage" with devices:
   "FileStorage0000" (/var/lib/bareos/storage)
   "FileStorage0001" (/var/lib/bareos/storage)
   ...(共 11 个设备条目 FileStorage0000–0010)...
   "FileStorage0010" (/var/lib/bareos/storage)

Device "FileStorage0000" (/var/lib/bareos/storage) is not open.
==
...(每个设备两行,状态均 is not open)...

Used Volume status:
====
====
You have messages.
quit

(输出节选说明:File 型存储每池按需自动生成设备资源,此处 11 个 FileStorageNNNN 都指向 /var/lib/bareos/storage;is not open 表示当前没有任务占用驱动器。)

status all 等价于依次执行 dir、storage、全部 client 三段输出,适合一次性巡检。三个子命令覆盖的验证点:status dir 看调度与 Director 侧任务、status storage 看介质占用、status client 看客户端连通性与版本——三者都无 running 任务、TLS 握手正常,即为健康态。

4.3 list 与 llist:查询目录库

list 输出精简表格,llist(long list)输出全字段长格式。help list 的参数清单(实测):

*help list
  Command            Description
  =======            ===========
  list               List objects from catalog

Arguments:
        jobs [type=<type>] [jobstatus=<jobstatus> [since=<time>] ] |
        jobid=<jobid> | ujobid=<unique-jobid> |
        joblog [jobid=<jobid>] [limit=<number>] [offset=<number>] |
        jobmedia [jobid=<jobid>] [volume=<volume>] |
        jobtotals | media [pool=<pool>] [enable|all] [locate=<text>] |
        volume [<volume>] [jobid=<jobid>] [limit=<number>] [enable|all] |
        pools | clients [jobs run] | files jobid=<jobid> |
        copies jobid=<jobid> | field=<number> jobid=<jobid> |
        basefiles jobid=<jobid> | client=<client> |
        nextvol job=<job-name> | pools | all

When at a prompt, entering a period (.) cancels the command.

逐项实测(环境含 JobId 1–12、卷 Full-0001 等时的真实输出):

list clients(客户端及其保留期,秒):

*list clients
+----------+-----------+---------------+--------------+
| clientid | name      | fileretention | jobretention |
+----------+-----------+---------------+--------------+
|        1 | bareos-fd |     5,184,000 |   15,552,000 |
+----------+-----------+---------------+--------------+

list volumes(按池分组列卷):

*list volumes
Pool: Incremental
No results to list.
Pool: Full
+---------+------------+-----------+---------+------------+----------+--------------+---------+------+-----------+-----------+---------------------+---------+------------------+
| mediaid | volumename | volstatus | enabled | volbytes   | volfiles | volretention | recycle | slot | inchanger | mediatype | lastwritten         | storage | hasencryptionkey |
+---------+------------+-----------+---------+------------+----------+--------------+---------+------+-----------+-----------+---------------------+---------+------------------+
|       1 | Full-0001  | Append    |       1 | 76,612,558 |        0 |   31,536,000 |       1 |    0 |         0 | File      | 2026-10-22 00:48:28 | File    |                0 |
|       2 | Full-0002  | Append    |       1 |        215 |        0 |   31,536,000 |       1 |    0 |         0 | File      |                     | File    |                0 |
|       3 | Full-0003  | Append    |       1 |        215 |        0 |   31,536,000 |       1 |    0 |         0 | File      |                     | File    |                0 |
+---------+------------+-----------+---------+------------+----------+--------------+---------+------+-----------+-----------+---------------------+---------+------------------+
Pool: Differential
No results to list.
Pool: Scratch
No results to list.

list pools(池定义):

*list pools
+--------+--------------+---------+---------+----------+---------------+
| poolid | name         | numvols | maxvols | pooltype | labelformat   |
+--------+--------------+---------+---------+----------+---------------+
|      1 | Scratch      |       0 |       0 | Scratch  | *             |
|      2 | Incremental  |       0 |     100 | Backup   | Incremental-  |
|      3 | Full         |       3 |     100 | Backup   | Full-         |
|      4 | Differential |       0 |     100 | Backup   | Differential- |
+--------+--------------+---------+---------+----------+---------------+

list filesets(目录库中登记过的 FileSet 版本,filesettext 列为完整定义;输出节选):

*list filesets
+-----------+----------+------------------------+---------------------+----------------------------------------------------------------------------------------------------+
| filesetid | fileset  | md5                    | createtime          | filesettext                                                                                          |
+-----------+----------+------------------------+---------------------+----------------------------------------------------------------------------------------------------+
|         1 | SelfTest | 35wO25lkbw+icQACU80RdB | 2026-10-21 17:38:24 | FileSet {
  Name = "SelfTest"
  Description = "fileset just to backup some files for selftest"
  Include {
    Options {
      Signature = "XXH128"
      ...
    }
    File = "/usr/sbin"
  }
}

 |
|         2 | Catalog  | Y9gUE3Mi1UIy95+cHC/mUC | 2026-10-21 21:10:00 | FileSet {
  Name = "Catalog"
  Description = "Backup the catalog dump and Bareos configuration files."
  ...
}

 |
+-----------+----------+------------------------+---------------------+----------------------------------------------------------------------------------------------------+

list jobmedia(任务与卷的对应关系——恢复时判断「数据在哪个卷上」就查它):

*list jobmedia
+-------+------------+------------+-----------+
| jobid | volumename | firstindex | lastindex |
+-------+------------+------------+-----------+
|     1 | Full-0001  |          1 |       202 |
|     3 | Full-0001  |          1 |       202 |
|     5 | Full-0001  |          1 |       202 |
|     9 | Full-0001  |          1 |        69 |
|    10 | Full-0001  |          1 |       202 |
+-------+------------+------------+-----------+

list jobtotals(按任务名汇总):

*list jobtotals
+------+-------+------------+------------------+
| jobs | files | bytes      | job              |
+------+-------+------------+------------------+
|    1 |    69 |    161,670 | BackupCatalog    |
|    6 |   808 | 76,324,944 | backup-bareos-fd |
+------+-------+------------+------------------+
+------+-------+------------+
| jobs | files | bytes      |
+------+-------+------------+
|    7 |   877 | 76,486,614 |
+------+-------+------------+

list joblog jobid=9(读取指定任务的完整运行日志,排障利器;输出节选首尾):

*list joblog jobid=9
Automatically selected Catalog: MyCatalog
Using Catalog "MyCatalog"
 2026-10-21 21:10:02 bareos-dir JobId 9: Version: 25.1.2~pre49.d5742f640 (16 October 2026) Ubuntu 22.04.5 LTS
 2026-10-21 21:10:02 bareos-dir JobId 9: shell command: run BeforeJob "/usr/lib/bareos/scripts/make_catalog_backup MyCatalog"
 2026-10-21 21:10:02 bareos-dir JobId 9: Start Backup JobId 9, Job=BackupCatalog.2026-10-21_21.10.00_39
 2026-10-21 21:10:02 bareos-dir JobId 9: Connected Storage daemon at bareos-server:9103, encryption: TLS_CHACHA20_POLY1305_SHA256 TLSv1.3
 ...(中段为 SD/FD 连接、卷追加写过程日志)...
 2026-10-21 21:10:02 bareos-dir JobId 9: shell command: run AfterJob "/usr/lib/bareos/scripts/delete_catalog_backup MyCatalog"
You have messages.
quit

(输出节选:末段还有完整的任务统计块——FD Files Written: 69、Termination: Backup OK 等,格式与 4.6 节 messages 输出一致。)

llist:同样对象的长格式输出,一卷 30+ 个字段全部展开。实测 llist volume=TestVol001(当时新建的测试卷):

*llist volume=TestVol001
Using Catalog "MyCatalog"
          mediaid: 5
       volumename: TestVol001
             slot: 0
           poolid: 3
             pool: Full
        mediatype: File
     firstwritten:
      lastwritten:
        labeldate: 2026-10-22 03:52:34
          voljobs: 0
         volfiles: 0
        volblocks: 0
        volmounts: 0
         volbytes: 216
        volerrors: 0
        volwrites: 0
 volcapacitybytes: 0
        volstatus: Append
          enabled: 1
          recycle: 1
     volretention: 31,536,000
   voluseduration: 0
       maxvoljobs: 0
      maxvolfiles: 0
      maxvolbytes: 53,687,091,200
        inchanger: 0
          endfile: 0
         endblock: 0
        labeltype: 0
        storageid: 1
         deviceid: 0
       locationid: 0
     recyclecount: 0
     initialwrite:
    scratchpoolid: 0
      scratchpool:
    recyclepoolid: 0
      recyclepool:
 hasencryptionkey: 0
          comment:
          storage: File

💡 说明:list 与 llist 的数据全部来自目录库(PostgreSQL),Director 未重启也会实时反映已完成的任务;但正在运行的任务行要等结束时才入库,运行态请用 status 查看。

4.4 show:查看运行时资源配置

show 读的是 Director 内存中已加载的资源配置(区别于 list 的目录库数据),常用于验证配置是否按预期生效。实测 show filesets(输出节选,完整输出含 4 个 FileSet):

*show filesets
FileSet {
  Name = "SelfTest"
  Description = "fileset just to backup some files for selftest"
  Include {
    Options {
      Signature = "XXH128"
      HardLinks = No
      AclSupport = Yes
      XattrSupport = Yes
    }
    File = "/usr/sbin"
  }
}
...(另有 "Windows All Drives"、"LinuxAll"、"Catalog" 三个 FileSet 定义)...

show storages(Director 视角连接 SD 的参数,密码以 [md5] 摘要形式展示):

*show storages
Storage {
  Name = "File"
  Address = "bareos-server"
  Password = "[md5]a9e2b005afed1ef2f98ba1bb7d724ed8"
  Device = "FileStorage"
  MediaType = "File"
  MaximumConcurrentJobs = 10
  PairedStorage = ""
}

其余 show clients、show pools、show schedules 同理输出对应资源定义。验证手法:改完配置文件后 reload 再 show <资源>,若新值出现即热加载成功。

4.5 messages:查看任务消息

任务(备份/恢复)的完成报告会缓存在 Director 侧等控制台读取,messages 取出并清空。实测(含一次 Full 备份 JobId 14 的完整报告):

*messages
22-Oct 03:50 bareos-server-fd JobId 14: ACL support is enabled
22-Oct 03:50 bareos-sd JobId 14: Version: 25.1.2~pre49.d5742f640 (16 October 2026) Ubuntu 22.04.5 LTS
22-Oct 03:50 bareos-sd JobId 14: JustInTime Reservation: Finding drive to reserve.
22-Oct 03:50 bareos-sd JobId 14: Using Device "FileStorage0001" (/var/lib/bareos/storage) to write.
22-Oct 03:50 bareos-sd JobId 14: Moving to end of data on volume "Full-0001"
22-Oct 03:50 bareos-sd JobId 14: Ready to append to end of Volume "Full-0001" size=76612558
22-Oct 03:50 bareos-sd JobId 14: Releasing device "FileStorage0001" (/var/lib/bareos/storage).
22-Oct 03:50 bareos-sd JobId 14: Elapsed time=00:00:01, Transfer rate=19.10 M Bytes/second
22-Oct 03:50 bareos-dir JobId 14: Insert of attributes batch table with 202 entries start
22-Oct 03:50 bareos-dir JobId 14: Insert of attributes batch table done
22-Oct 03:50 bareos-dir JobId 14: Bareos bareos-dir 25.1.2~pre49.d5742f640 (16Oct26):
  OS Information:         Ubuntu 22.04.5 LTS
  JobId:                  14
  Job:                    backup-bareos-fd.2026-10-22_03.50.40_47
  Backup Level:           Full
  Client:                 "bareos-fd" 25.1.2~pre49.d5742f640 (16 October 2026) Ubuntu 22.04.5 LTS,ubuntu
  FileSet:                "SelfTest" 2026-10-21 17:38:24
  Pool:                   "Full" (From Job FullPool override)
  Catalog:                "MyCatalog" (From Client resource)
  Storage:                "File" (From Job resource)
  Scheduled time:         22-Oct-2026 03:50:40
  Start time:             22-Oct-2026 03:50:42
  End time:               22-Oct-2026 03:50:43
  Elapsed time:           1 sec
  Priority:               10
  Allow Mixed Priority:   no
  FD Files Written:       202
  SD Files Written:       202
  FD Bytes Read:          19,081,236 (19.08 MB)
  FD Bytes Written:       19,081,236 (19.08 MB)
  SD Bytes Written:       19,101,914 (19.10 MB)
  Rate:                   19081.2 KB/s
  Software Compression:   None
  VSS:                    no
  Encryption:             no
  Accurate:               no
  Volume name(s):         Full-0001
  Volume Session Id:      13
  Volume Session Time:    1792604197
  Last Volume Bytes:      95,722,612 (95.72 MB)
  Non-fatal FD errors:    0
  SD Errors:              0
  FD termination status:  OK
  SD termination status:  OK
  Bareos binary info:     Bareos community build (UNSUPPORTED): Get professional support from https://www.bareos.com
  Job triggered by:       User
  Termination:            Backup OK

(输出节选:首部还有 Director/FD/SD 三方 TLS 连接建立的过程行,此处省略;Termination: Backup OK 为任务成功的最终判定行。)

要点:messages 读一次清一次;想让消息自动弹出可开 autodisplay on。历史任务的消息用 list joblog jobid=<N> 随时回看,不受清空影响。

4.6 run:执行备份任务

run 支持交互菜单与参数直跑两种方式。help run 的完整参数见 4.1 节。生产上最常用的直跑形态:

*run job=backup-bareos-fd level=Full yes
Using Catalog "MyCatalog"
Job queued. JobId=14

三个要素:job= 指定任务、level= 覆盖任务默认级别(不传则按计划与上次备份自动选择)、末尾 yes 跳过确认直接执行。随后 wait 阻塞等任务结束、messages 取回报告:

*wait
*messages
...(JobId 14 完整报告见 4.5 节,Termination: Backup OK)...

level 自动选择的验证:紧接着不带 level 再跑一次,Director 依「距上次 Full 很近」自动选 Incremental,0 文件改动即空备份:

*run job=backup-bareos-fd yes
Using Catalog "MyCatalog"
Job queued. JobId=13
*wait
*messages
...(报告关键字段实测值)...
  JobId:                  13
  Backup Level:           Incremental, since=2026-10-22 00:48:27
  FD Files Written:       0
  Volume name(s):         
  Termination:            Backup OK

(输出节选:since=2026-10-22 00:48:27 即上次 Full 的开始时间;Pool 从 Full 池切到 Incremental 池也由报告中的 Pool: "Incremental" (From Job IncPool override) 行体现。)

用 list jobs 复核两条任务均已入库:

*list jobs
+-------+------------------+-----------+---------------------+----------+------+-------+----------+------------+-----------+
| jobid | name             | client    | starttime           | duration | type | level | jobfiles | jobbytes   | jobstatus |
+-------+------------------+-----------+---------------------+----------+------+-------+----------+------------+-----------+
|     1 | backup-bareos-fd | bareos-fd | 2026-10-21 17:38:26 | 00:00:01 | B    | F     |      202 | 19,081,236 | T         |
|     2 | RestoreFiles     | bareos-fd | 2026-10-21 17:38:50 | 00:00:02 | R    | F     |      202 | 19,081,236 | T         |
|     3 | backup-bareos-fd | bareos-fd | 2026-10-21 18:15:23 | 00:00:01 | B    | F     |      202 | 19,081,236 | T         |
|     4 | RestoreFiles     | bareos-fd | 2026-10-21 18:22:30 | 00:00:02 | R    | F     |      202 | 19,081,236 | T         |
|     5 | backup-bareos-fd | bareos-fd | 2026-10-21 18:28:19 | 00:00:01 | B    | F     |      202 | 19,081,236 | T         |
|     6 | RestoreFiles     | bareos-fd | 2026-10-21 18:28:59 | 00:00:02 | R    | F     |      202 | 19,081,236 | T         |
|     7 | RestoreFiles     | bareos-fd | 2026-10-21 18:29:32 | 00:00:02 | R    | F     |      202 | 19,081,236 | T         |
|     8 | backup-bareos-fd | bareos-fd | 2026-10-21 21:00:02 | 00:00:01 | B    | I     |        0 |          0 | T         |
|     9 | BackupCatalog    | bareos-fd | 2026-10-21 21:10:02 | 00:00:00 | B    | F     |       69 |    161,670 | T         |
|    10 | backup-bareos-fd | bareos-fd | 2026-10-22 00:48:27 | 00:00:01 | B    | F     |      202 | 19,081,236 | T         |
|    11 | RestoreFiles     | bareos-fd | 2026-10-22 00:50:53 | 00:00:02 | R    | F     |      202 | 19,081,236 | T         |
|    12 | backup-bareos-fd | bareos-fd | 2026-10-22 00:58:31 | 00:00:00 | B    | F     |        0 |          0 | A         |
|    13 | backup-bareos-fd | bareos-fd | 2026-10-22 03:49:15 | 00:00:01 | B    | I     |        0 |          0 | T         |
+-------+------------------+-----------+---------------------+----------+------+-------+----------+------------+-----------+

cancel:取消运行中任务。实测「发起 Full 备份后立刻取消」(cancel jobid=16 紧跟 run 之后同一会话内执行):

*run job=backup-bareos-fd level=Full yes
Using Catalog "MyCatalog"
Job queued. JobId=16
*cancel jobid=16
JobId 16, Job backup-bareos-fd.2026-10-22_03.52.16_53 marked to be canceled.
*wait
*messages
...(JobId 16 报告实测节选,格式同 4.5 节)...
  JobId:                  16
  Backup Level:           Full
  FD Files Written:       0
  Volume name(s):         
  Termination:            Backup Canceled

(输出节选:最终判定行为 Termination: Backup Canceled,且目录库中该任务 jobstatus 记为 A,与 3.3 节 list jobs 中 JobId 12 的状态一致。)

cancel 的全部参数形式(help cancel 实测):storage=<name>、jobid=<id>、job=<name>、ujobid=<unique-id>、state=<state>、all yes。另有 rerun jobid=<N> 可按原参数重跑历史任务、wait 等待所有任务结束。

4.7 estimate:备份量估算

改 FileSet 或评估增量规模时,先估算不动真格:

*estimate job=backup-bareos-fd
Using Catalog "MyCatalog"
Connecting to Client bareos-fd at localhost:9102
2000 OK estimate files=202 bytes=19,081,236

2000 OK estimate files=202 bytes=19,081,236 即「按当前 FileSet 会备份 202 个文件、共 19,081,236 字节」。加 listing 关键字则逐文件列出(模拟备份清单,输出节选首尾):

*estimate job=backup-bareos-fd listing
Using Catalog "MyCatalog"
Connecting to Client bareos-fd at localhost:9102
-rwxr-xr-x   1 root     root           59528 2026-10-06 07:17:33  /usr/sbin/chgpasswd
-rwxr-xr-x   1 root     root           31104 2026-10-06 07:17:33  /usr/sbin/blockdev
-rwxr-xr-x   1 root     root           43392 2026-10-06 07:17:34  /usr/sbin/fstrim
...(共 202 行,均为 /usr/sbin 下的文件与符号链接)...
-rwxr-xr-x   1 root     root           59976 2026-10-06 07:17:34  /usr/sbin/rmt-tar

💡 说明:estimate 走 FD 实际扫描文件系统(不是查目录库),因此能反映 FileSet 修改后的真实效果;本环境文件数 202 与已完成的 Full 备份一致,说明估算与实际备份行为吻合。

4.8 关于 backup 命令的纠偏

不少 Bacula 时代资料会提到 backup 命令,Bareos 25.1 中并不存在,备份一律通过 run 下发。实测:

*help backup
  Command            Description
  =======            ===========

Can't find backup command.

同理,4.1 节 help 全表(45 条命令)中也没有 backup 一行——以本表为准即可,无需再记忆旧命令名。

4.9 卷管理:label / mount / umount / truncate / relabel / prune / purge / delete / update

卷(Volume)是 Bareos 存备份数据的物理载体(File 存储下即一个文件)。本节用一块新建测试卷把整条生命周期走一遍,所有命令在真实 Director 上实测。

label:制具新卷。help label 语法(实测):storage=<storage-name> volume=<volume-name> pool=<pool-name> slot=<slot> [ drive=<drivenum> ] [ barcodes ] [ encrypt ] [ yes ]:

*label storage=File volume=TestVol001 pool=Full yes
Automatically selected Catalog: MyCatalog
Using Catalog "MyCatalog"
Connecting to Storage daemon File at bareos-server:9103 ...
Sending label command for Volume "TestVol001" Slot 0 ...
3000 OK label. VolFiles=0 VolBytes=216 Volume="TestVol001" Device="FileStorage0000" (/var/lib/bareos/storage)
Catalog record for Volume "TestVol001", Slot 0 successfully created.
Requesting to mount FileStorage ...
3906 File device "FileStorage0000" (/var/lib/bareos/storage) is always mounted.

list volumes 立即可见新卷(mediaid=5、VolBytes=216 即卷首标签块大小):

*list volumes
Pool: Full
+---------+------------+-----------+---------+------------+----------+--------------+---------+------+-----------+-----------+---------------------+---------+------------------+
| mediaid | volumename | volstatus | enabled | volbytes   | volfiles | volretention | recycle | slot | inchanger | mediatype | lastwritten         | storage | hasencryptionkey |
+---------+------------+-----------+---------+------------+----------+--------------+---------+------+-----------+-----------+---------------------+---------+------------------+
|       1 | Full-0001  | Append    |       1 | 95,722,612 |        0 |   31,536,000 |       1 |    0 |         0 | File      | 2026-10-22 03:50:43 | File    |                0 |
|       2 | Full-0002  | Append    |       1 |        215 |        0 |   31,536,000 |       1 |    0 |         0 | File      |                     | File    |                0 |
|       3 | Full-0003  | Append    |       1 |        215 |        0 |   31,536,000 |       1 |    0 |         0 | File      |                     | File    |                0 |
|       5 | TestVol001 | Append    |       1 |        216 |        0 |   31,536,000 |       1 |    0 |         0 | File      |                     | File    |                0 |
+---------+------------+-----------+---------+------------+----------+--------------+---------+------+-----------+-----------+---------------------+---------+------------------+

mount / umount:控制 SD 驱动器的挂载状态。File 型设备会自动保持挂载,实测两条命令的「不操作」回显:

*umount storage=File
Automatically selected Catalog: MyCatalog
Using Catalog "MyCatalog"
Connecting to Storage daemon File at bareos-server:9103 ...
3901 Device ""FileStorage0000" (/var/lib/bareos/storage)" is already unmounted.
*mount storage=File
Connecting to Storage daemon File at bareos-server:9103 ...
3906 File device "FileStorage0000" (/var/lib/bareos/storage) is always mounted.

💡 说明:3901(已卸载)与 3906(常挂载)是 SD 的标准应答码;umount/unmount 同义(后者为正式名,umount 保留自老 Unix 习惯,见 4.1 节 help 全表注释)。这两条命令对磁带库/autochanger 场景才是刚需,File 存储记住「始终挂载」即可。

truncate:清空已 Purge 的卷。25.1 的 truncate 必须显式带 volstatus=Purged 参数,缺省会报用法错(实测):

*truncate volume=TestVol001 storage=File
required parameter 'volstatus' missing
USAGE: truncate volstatus=Purged [storage=<storage>] [pool=<pool>] [volume=<volume>] [drive=<drivenum>] [yes]

relabel:重制卷标签。前置条件是旧卷状态必须为 Purged/Recycled,Append 状态直接重打会被拒(实测两次踩坑回显):

*relabel storage=File oldvolume=TestVol001 volume=TestVol002 pool=Full
Using Catalog "MyCatalog"
Volume "TestVol001" has VolStatus Append. It must be Purged or Recycled before relabeling.

把卷置为 Purged 的实测路径:update volume=... volstatus=Purged 会直接被拒(25.1 不允许 update 直接写入 Purged,实测回显 Invalid VolStatus specified: Purged);正确做法是用 purge 命令清空卷上任务记录并自动置 Purged:

*purge volume=TestVol001 yes

This command can be DANGEROUS!!!

It purges (deletes) all Files from a Job,
JobId, Client or Volume; or it purges (deletes)
all Jobs from a Client or Volume without regard
to retention periods. Normally you should use the
PRUNE command, which respects retention periods.
This command requires full access to all resources.
Automatically selected Catalog: MyCatalog
Using Catalog "MyCatalog"
No Jobs found for media TestVol001 to purge from catalog.
There are no more Jobs associated with Volume "TestVol001". Marking it purged.

随后 relabel 成功:

*relabel storage=File oldvolume=TestVol001 volume=TestVol002 pool=Full
Using Catalog "MyCatalog"
Connecting to Storage daemon File at bareos-server:9103 ...
Sending relabel command from "TestVol001" to "TestVol002" ...
3000 OK label. VolFiles=0 VolBytes=216 Volume="TestVol002" Device="FileStorage0000" (/var/lib/bareos/storage)
Catalog record for Volume "TestVol002", Slot 0 successfully created.
Old volume "TestVol001" deleted from catalog.
Requesting to mount FileStorage ...
3906 File device "FileStorage0000" (/var/lib/bareos/storage) is always mounted.

prune:按保留期清理。注意 25.1 的卷剪除要求卷状态为 Full 或 Used,Append 态直接剪会被拒(实测):

*prune volume=TestVol002 pool=Full yes
The current Volume (TestVol002) retention period is: 1 year
Pruning volume TestVol002: cannot prune as Volstatus is Append but needs to be Full or Used.

prune 的完整参数(help prune 实测):files、jobs、volume [all] [=volume] [pool=<pool>] [yes]、stats [yes]、directory [=directory] ... 五个子形态,分别清理文件记录、任务记录、卷记录、统计与目录项。

delete:删除卷记录。实测(同时给出确认文案):

*delete volume=TestVol002 yes

This command will delete volume TestVol002
and all Jobs saved on that volume from the Catalog
Volume TestVol002 deleted.

update:改卷属性。合法写法(help update 实测语法):update volume=<name> [volstatus=<status>] [volretention=<time>] [voluseduration=<time>] [maxvolbytes=<size>] [enabled=<yes/no>] [recycle=<yes/no>] ...。例如延长某卷保留期为 2 年(31536000×2 秒):

*update volume=Full-0001 volretention=63072000

改完用 llist volume=Full-0001 核对 volretention 字段即生效(本例测试后已恢复原值 31,536,000)。

💡 技术拓展:卷状态(VolStatus)机是理解上述命令前置条件的钥匙,主要取值对比——

VolStatus含义可写数据可 relabel可 prune
Append追加态,池的默认新卷状态是否(实测被拒)否(实测被拒)
Full已写满或被标记写满否是是
Used已使用但未写满是(追加)是是
Purged数据记录已清空是(重新开始写)是—(已是终点态)
Recycle待回收,将被自动复用是是—
Disabled禁用否——

本文实测环境的卷流转路径为:label 生成 Append → 任务写入保持 Append → purge/prune 后转 Purged → relabel/truncate 复用。管理命令的前置条件全部围绕该状态机设计。

卷管理小结(验证手段):每一步操作后 list volumes / llist volume=<名> 核对状态字段;物理文件用 ls -l /var/lib/bareos/storage/ 对照卷名确认落盘。删除卷仅删目录库记录,卷文件需手工清理(本文测试卷文件已同步删除)。

4.10 reload:配置热加载

改完 /etc/bareos 下的配置后,无需重启服务:

*reload
reloaded

若新配置有语法错误,Director 会拒绝加载并在 messages 中给出定位(文件名+行号+列号),加载失败的配置不影响正在运行的服务——排障实录用 bareos-dir -t 更直接,见 7.2 节。配置文件体系与各资源参数的完整说明见《Bareos 25.1 配置参数详解》第七章「reload 热生效与语法错误拒载」。

4.11 sqlquery:直接查询目录库

list 覆盖不到的维度可直接写 SQL。实测交互式查询模式(以分号结尾提交、空行退出):

*sqlquery
Automatically selected Catalog: MyCatalog
Using Catalog "MyCatalog"
Entering SQL query mode.
Terminate each query with a semicolon.
Terminate query mode with a blank line.
Enter SQL query: select JobId,Name,Level,JobStatus from Job where JobStatus='T' order by JobId desc limit 3;
+-------+------------------+-------+-----------+
| jobid | name             | level | jobstatus |
+-------+------------------+-------+-----------+
|    17 | RestoreFiles     | F     | T         |
|    15 | RestoreFiles     | F     | T         |
|    14 | backup-bareos-fd | F     | T         |
+-------+------------------+-------+-----------+
Enter SQL query:
End query mode.

⚠️ 注意:sqlquery 对目录库有完整读写能力(Bareos 仅拦截了非 SELECT 语句),查询大表(如 File 表)时务必带 limit,否则会拖垮 Director 响应。

五、bconsole 命令行模式与非交互用法

5.1 完整选项表

bconsole --help 实测回显(25.1 的全部选项,注意没有 -n/-x 这类旧式短开关):

The Bareos Console.

Bareos community build (UNSUPPORTED).
Get professional support from https://www.bareos.com
Copyright (C) 2013-2026 Bareos GmbH & Co. KG
Copyright (C) 2000-2012 Free Software Foundation Europe e.V.
Copyright (C) 2010-2017 Planets Communications B.V.

Version: 25.1.2~pre49.d5742f640 (16 October 2026) Ubuntu 22.04.5 LTS


Usage: bconsole [OPTIONS]

Options:
    -h,-?,--help
        Print this help message and exit.

    --version
        Display program version information and exit

    -c,--config <path>:PATH(existing)
        Use <path> as configuration file or directory

    -D,--director <director>
        Specify director.

    -d,--debug-level <level>
        Set debug level to <level>.

    --dt,--debug-timestamps
        Print timestamps in debug output.

    -l,--list-directors
        List defined Directors.

    -p,--pam-credentials-filename <path>:FILE
        PAM Credentials file.

    -o
        Force sending pam credentials unencrypted.

    -s,--no-signals
        No signals (for debugging)

    -t,--test-config
        Test - read configuration and exit

    -u,--timeout <seconds>:POSITIVE
        Set command execution timeout to <seconds>.

    --xc,--export-config
        Excludes: --xs
        Print configuration resources and exit

    --xs,--export-schema
        Excludes: --xc
        Print configuration schema in JSON format and exit

⚠️ 踩坑实录:旧版 Bacula 资料里的 bconsole -n(非交互)在本版本会直接报 ERROR: bconsole: The following argument was not expected: -n 并打印上述帮助。25.1 的非交互方式就是标准输入重定向/管道(见 5.3 节),没有专门开关。

5.2 配置测试与 Director 发现

bconsole -t

实测:读配置、退出,回显为空、退出码 0(echo $? 验证)。

bconsole -l

列出 bconsole.conf 中定义的 Director(实测):

bareos-dir

多 Director 环境用 -D <名称> 指定目标,实测连接与单命令执行:

bconsole -D bareos-dir <<< "version"
Connecting to Director localhost:9101
 Encryption: TLS_CHACHA20_POLY1305_SHA256 TLSv1.3
1000 OK: bareos-dir Version: 25.1.2~pre49.d5742f640 (16 October 2026)
Bareos community build (UNSUPPORTED).
Get professional support from https://www.bareos.com
You are connected using the default console

Enter a period (.) to cancel a command.
version
bareos-dir Version: 25.1.2~pre49.d5742f640 (16 October 2026) Ubuntu 22.04.5 LTS ubuntu Ubuntu 22.04.5 LTS

5.3 管道非交互:脚本化操作

bconsole 从标准输入逐行读命令,管道/heredoc 即可脚本化,输出与交互模式一致:

printf "status dir\nquit\n" | bconsole
bconsole <<EOF
list jobs
quit
EOF

实测(以 5.1 节 version 探活与 4.2 节 status dir 为例,两条路径输出完全一致)。脚本化要点:

  • 每条命令占一行,结尾必须 quit 断开,否则连接悬挂等超时;
  • 交互提示的回答也按行喂入(如 restore 流程的 3、mark *、done、yes,完整脚本见第九章实录);
  • 关键自动化场景建议配合 .api 2 点命令切换到 JSON 输出模式解析,或直接查数据库。

5.4 调试与导出选项

-u <秒> 设置单命令超时(长任务如 restore 交互时可放宽):

printf "status dir\nquit\n" | bconsole -u 3

实测正常连接与输出(超时仅作用于等待应答的时间上限)。

-d <级别> 输出调试日志(级别 0–200,50 可看清 TLS 握手),实测节选:

$ printf "version\nquit\n" | bconsole -d 50
bconsole (10): lib/parse_conf.h:462-0 LoadedConfiguration: new configuration_resources_ 0x58843c53d860
Connecting to Director localhost:9101
bconsole (50): lib/tls_openssl.cc:61-0 Preparing TLS_PSK CLIENT context for identity R_CONSOLE *UserAgent*
bconsole (50): lib/bnet.cc:192-0 TLS client negotiation established.
bconsole (50): lib/cram_md5.cc:91-0 my_name: <R_CONSOLE::*UserAgent*> - challenge_name: <R_DIRECTOR::bareos-dir>
bconsole (50): lib/cram_md5.cc:136-0 send: auth cram-md5 <417626243.1792641306@R_CONSOLE::*UserAgent*> ssl=2

--xc 导出 bconsole 自身生效的资源配置(密码以 [md5] 摘要显示):

$ bconsole --xc
Director {
  Name = "bareos-dir"
  Description = "Bareos Console credentials for local Director"
  Address = "localhost"
  Password = "[md5]a082a1a46efa9b1c9ab48cc44f8d736e"
}

--xs 以 JSON 导出配置模式(schema),可脚本化校验配置字段(输出节选):

$ bconsole --xs
{
  "format-version": 2,
  "component": "bconsole",
  "version": "25.1.2~pre49.d5742f640",
  "resource": {
    "bconsole": {
      "Console": {
        "Name": {
          "datatype": "NAME",
          "code": 0,
          "equals": true,
          "required": true,
...

六、独立命令行工具详解

6.1 bls:列出卷内容

bls 直接读卷文件,不经过 Director。关键实测坑:位置参数是「SD 设备资源名」而非卷文件路径——直接给路径会报错并友好提示可用设备(实测回显):

$ bls -j -V Full-0001 /var/lib/bareos/storage/Full-0001
bls: stored/butil.cc:350-0 Could not find device "/var/lib/bareos/storage/Full-0001" in config file (null).
22-Oct 03:55 bls JobId 0: Fatal error: stored/butil.cc:207 Cannot find device "/var/lib/bareos/storage/Full-0001" in config file (null).
Available Devices:
 "FileStorage0000" (/var/lib/bareos/storage)
 "FileStorage0001" (/var/lib/bareos/storage)
 ...

正确姿势:bls -j -V <卷名> <设备资源名>(设备资源名从报错提示或 status storage 获取)。-j 列 Job 会话边界,实测:

$ bls -j -V Full-0001 FileStorage0000
bls: stored/butil.cc:356-0 Using device: "FileStorage0000" for reading.
22-Oct 03:55 bls JobId 0: Ready to read from volume "Full-0001" on device "FileStorage0000" (/var/lib/bareos/storage).
Volume Record: File:blk=0:214 SessId=0 SessTime=0 JobId=0 DataLen=179
Begin Job Session Record: File:blk=0:1048790 SessId=1 SessTime=1792604197 JobId=1
   Job=backup-bareos-fd.2026-10-21_17.38.24_05 Date=21-Oct-2026 17:38:26 Level=F Type=B
End Job Session Record: File:blk=0:19110268 SessId=1 SessTime=1792604197 JobId=1
   Job=backup-bareos-fd.2026-10-21_17.38.24_05 Date=21-Oct-2026 17:38:27 Level=F Type=B Files=202 Bytes=19,101,914 Errors=0 Status=T
Begin Job Session Record: File:blk=0:20158844 SessId=3 SessTime=1792604197 JobId=3
   Job=backup-bareos-fd.2026-10-21_18.15.21_10 Date=21-Oct-2026 18:15:24 Level=F Type=B
End Job Session Record: File:blk=0:38220322 SessId=3 SessTime=1792604197 JobId=3
   Job=backup-bareos-fd.2026-10-21_18.15.21_10 Date=21-Oct-2026 18:15:24 Level=F Type=B Files=202 Bytes=19,101,914 Errors=0 Status=T
...(后续 JobId 5、10、14 的会话记录同格式)...
22-Oct 03:55 bls JobId 0: End of Volume at file 0 on device "FileStorage0000" (/var/lib/bareos/storage), Volume "Full-0001"
22-Oct 03:55 bls JobId 0: End of all volumes.
22-Oct 03:55 bls JobId 0: Releasing device "FileStorage0000" (/var/lib/bareos/storage).

(输出节选:Full-0001 内含多个 Full 备份会话顺序追加,每个会话一对 Begin/End 记录,Files=/Bytes=/Status= 与目录库记录互相印证。)

常用参数(bls --help 实测节选):-j 列 Job 会话、-k 列块、不带 -j/-k 时列出保存的文件、-L 转储卷标签、-V <vol1|vol2> 指定卷名、-p 忽略 IO 错误继续、-v 详细输出。

6.2 bextract:绕过 Director 的裸恢复

当 Director 或数据库损坏时,用 bextract 直接从卷抽文件。用法:bextract [OPTIONS] <设备资源名> <目标目录>(--help 实测 Usage 行):

Usage: bextract [OPTIONS] [bareos-archive-device-name] [target-directory]

实测只抽取单个文件 /usr/sbin/reboot:先用 -i 给包含清单,注意文件路径必须带前导斜杠(不带斜杠匹配不到,实测第一次 usr/sbin/reboot 返回 0 files restored.,改为 /usr/sbin/reboot 后成功):

printf "/usr/sbin/reboot\n" > /tmp/bextract-inc.txt
mkdir -p /tmp/bextract-out
bextract -V Full-0001 -i /tmp/bextract-inc.txt FileStorage0000 /tmp/bextract-out

实测回显:

bextract: stored/butil.cc:356-0 Using device: "FileStorage0000" for reading.
22-Oct 03:56 bextract JobId 0: Ready to read from volume "Full-0001" on device "FileStorage0000" (/var/lib/bareos/storage).
22-Oct 03:56 bextract JobId 0: End of Volume at file 0 on device "FileStorage0000" (/var/lib/bareos/storage), Volume "Full-0001"
22-Oct 03:56 bextract JobId 0: End of all volumes.
22-Oct 03:56 bextract JobId 0: Releasing device "FileStorage0000" (/var/lib/bareos/storage).
5 files restored.

核对抽取结果(符号链接按原样恢复、指向目标与源文件一致):

$ ls -la /tmp/bextract-out/usr/sbin/
total 0
drwxr-xr-x 2 root root 60 Oct 22 03:56 .
drwxr-xr-x 3 root root 60 Oct 22 03:56 ..
lrwxrwxrwx 1 root root 14 Oct 22 03:56 reboot -> /bin/systemctl
$ sha256sum /tmp/bextract-out/usr/sbin/reboot /usr/sbin/reboot
de4c5cdd03758593036da105a57f959b76bd1ad6e0d1e567d29d16458aeaddf6  /tmp/bextract-out/usr/sbin/reboot
de4c5cdd03758593036da105a57f959b76bd1ad6e0d1e567d29d16458aeaddf6  /usr/sbin/reboot

💡 说明:5 files restored 大于清单里的 1 个文件,是因为该路径还匹配到硬链接/同会话内关联文件;bextract 不写目录库、不做增量合并,适合应急取数。常用参数:-V <卷名列表>、-i <包含清单>、-e <排除清单>、-b <bootstrap 文件>、-p 忽略 IO 错误。

6.3 bscan:扫描卷并同步目录库

bscan 逐块解析卷内容,可把卷中未被目录库登记的数据「找回来」(灾难恢复)。仅溯源确认其参数语义(bscan --help 实测节选):

Usage: bscan [OPTIONS] [device_name]

Positionals:
    device_name
        Specify the input device name (either as name of a Bareos Storage
        Daemon Device resource or identical to the Archive Device in a Bareos
        Storage Daemon Device resource).

Options:
    -h,-?,--help
        Print this help message and exit.
    ...
    -S,--show-progress
        Show scan progress periodically.

    -s,--update-db
        Synchronize or store in database.

    -V,--volumes <vol1|vol2|...>
        Specify volume names (separated by |).

    -v,--verbose
        Verbose user messages.

    -w,--working-directory <directory>
        Specify working directory.

本环境实测了「只扫描、不写库」的安全形态(对空卷 Full-0002 执行,不加 -s),同时确认配置路径不需要 -c(默认即读 /etc/bareos/bareos-sd.d;显式 -c /etc/bareos/bareos-sd.d 反而报「找不到配置」——它把参数当成了独立配置文件路径,实测回显:Failed to find configuration files under directory "/etc/bareos/bareos-sd.d"):

$ runuser -u bareos -- bscan -n bareos -u bareos -m -v -V Full-0002 FileStorage0000
bscan: stored/bscan.cc:581-0 VOL_LABEL: OK for Volume: Full-0002
22-Oct 03:56 bscan JobId 0: End of Volume at file 0 on device "FileStorage0000" (/var/lib/bareos/storage), Volume "Full-0002"
bscan (0): stored/bscan.cc:368-0 ========== JobId=0 ========
bscan: stored/bscan.cc:1111-0 Updated Media record at end of Volume: Full-0002
22-Oct 03:56 bscan JobId 0: End of all volumes.
bscan: stored/bscan.cc:1111-0 Updated Media record at end of Volume: Full-0002
bscan: stored/bscan.cc:740-0 End of all Volumes. VolFiles=0 VolBlocks=0 VolBytes=199
Records would have been added or updated in the catalog:
      1 Media
      1 Pool
      0 Job
      0 File
      0 RestoreObject
22-Oct 03:56 bscan JobId 0: Releasing device "FileStorage0000" (/var/lib/bareos/storage).

⚠️ 注意:末尾 Records would have been added or updated in the catalog 表明本次为预演(无 -s 不落库);加 -s 才会真正写入目录库。-m 允许更新 Media 记录、-n <库名> -u <库用户> 指定数据库连接(本环境实测 bareos/bareos)。对存有真实数据的卷执行 -s 前务必先备份数据库。

6.4 bareos-dbcheck:目录库一致性检查

⚠️ 踩坑实录:本工具的二进制名是 bareos-dbcheck(/usr/sbin/),不存在 dbcheck 命令——runuser -u bareos -- dbcheck 会报 failed to execute dbcheck: No such file or directory。

交互式菜单程序,实测(root 下先 -B 导出连接参数确认目标库,再以 bareos 用户跑全量检查,printf "15\n" 选择菜单项 15 Run ALL checks,后续提示用 EOF 取默认「不修改」):

$ bareos-dbcheck -B
catalog=MyCatalog
db_name=bareos
db_driver=postgresql
db_user=bareos
db_password=
db_address=
db_port=0
db_socket=
db_type=PostgreSQL
working_dir=/var/lib/bareos

$ printf "15\n" | runuser -u bareos -- bareos-dbcheck
Hello, this is the Bareos database check/correct program.
Modify database is off. Verbose is off.
Please select the function you want to perform.
     0) Quit
     1) Toggle modify database flag
     2) Toggle verbose flag
     3) Check for bad Filename records
     4) Check for bad Path records
     5) Check for duplicate Path records
     6) Check for orphaned Jobmedia records
     7) Check for orphaned File records
     8) Check for orphaned Path records
     9) Check for orphaned FileSet records
    10) Check for orphaned Client records
    11) Check for orphaned Job records
    12) Check for orphaned storage records
    13) Check for all Admin records
    14) Check for all Restore records
    15) Run ALL checks
Select function number: ===========================================================
=
= Check for bad Filename records, modify=0
=
Checking for Filenames with a trailing slash
Found 0 bad Filename records.
=
=
===========================================================
===========================================================
=
= Check for bad Path records, modify=0
=
Checking for Paths without a trailing slash
Found 0 bad Path records.
=
=
===========================================================
===========================================================
=
= Check for duplicate Path records, modify=0
=
Checking for duplicate Path entries.
Found 0 duplicate Path records.
=
=
===========================================================
===========================================================
=
= Check for orphaned Jobmedia records, modify=0
=
Checking for orphaned JobMedia entries.
=
=
===========================================================
===========================================================
=
= Check for orphaned File records, modify=0
=
Checking for orphaned File entries. This may take some time!
=
...(后续为 orphaned Path/FileSet/Client/Job/storage、Admin/Restore records 各项,同样 modify=0)...

(输出节选:共 12 类检查;modify=0 表示只查不改,交互中对该项提问答 yes 或先选菜单 1 切换 modify 标志后才执行修复。)

6.5 bregex 与 bwild:匹配规则测试

编写 FileSet 的 WildFile/RegexFile 规则前,先用这两个工具对样例文件名测试(进入交互模式,粘贴文件名列表后输入正则/通配):

bregex    # 正则(POSIX 扩展)匹配测试
bwild     # 通配符匹配测试

两者均为交互式 stdin 程序:先输入若干待匹配文件名,再输入模式,输出每个文件是否匹配。适合离线验证 FileSet 的 Options 规则,避免线上跑错范围。

6.6 其他工具

  • bcopy:卷到卷复制(如把 File 卷转存到磁带卷),参数风格与 bls/bextract 一致(bcopy --help 溯源确认,用法 bcopy [OPTIONS] <输入设备> <输出设备>),本篇未做真实复制演练;
  • bdedupestimate:dedup 存储后端的空间节省估算(--help 溯源确认);
  • bpluginfo:列出/查看已安装插件(本环境 SD 插件目录含 autoxflate-sd.so、FD 插件 bpipe-fd.so,实查 dpkg -L bareos-plugins 类路径所得);
  • testfind:按 FileSet 定义试运行文件匹配,输出将被备份的文件清单,是调试 Include/Exclude 的专用工具(与 estimate listing 互为补充:前者离线读配置,后者经 FD 实扫)。

⚠️ 注意:以上四个工具在本环境仅做帮助与存在性溯源,未执行破坏性/写入性操作;生产使用前请先在测试卷上演练。

七、配置文件校验

7.1 三守护进程 -t 校验

每个守护进程都支持 -t(读配置即退出,退出码 0 为通过)。实测:

bareos-sd -t; echo "EXIT=$?"
bareos-fd -t; echo "EXIT=$?"
bconsole -t; echo "EXIT=$?"
EXIT=0
EXIT=0
EXIT=0

bareos-dir -t 的认证坑(实测踩坑):直接以 root 运行会因 PostgreSQL 的 Peer 认证失败而误报——root 试图以 bareos 身份走本地 socket 被数据库拒绝,报错与配置本身无关:

$ bareos-dir -t; echo "EXIT=$?"
bareos-dir: dird/check_catalog.cc:72-0 Could not open Catalog "MyCatalog", database "bareos": cats/postgresql.cc:298 Unable to connect to PostgreSQL server. Database=bareos User=bareos
Possible causes: SQL server not running; password incorrect; server requires ssl; max_connections exceeded.
(connection to server on socket "/var/run/postgresql/.s.PGSQL.5432" failed: FATAL:  Peer authentication failed for user "bareos"
)

bareos-dir ERROR TERMINATION
Please correct the configuration in /etc/bareos/bareos-dir.d/*/*.conf
EXIT=1

以服务运行身份(bareos 用户)执行即通过:

$ runuser -u bareos -- bareos-dir -t; echo "EXIT=$?"
EXIT=0

⚠️ 注意:校验 bareos-dir 配置一律用 runuser -u bareos -- bareos-dir -t,且需 PostgreSQL 已启动(Peer 认证走本地 socket)。-t 只校验配置与数据库连通性,不会尝试绑定 9101 端口,可在服务运行中安全执行。

7.2 坏配置排障实测

故意在 Director 的 job 子配置目录写入一条非法参数,验证 -t 的定位能力:

printf "# 故意写错的参数\nBadParameter = yes\n" > /etc/bareos/bareos-dir.d/job/zz-bad.conf
runuser -u bareos -- bareos-dir -t

实测回显(报错精确到文件、行、列):

bareos-dir: CONFIG ERROR at lib/parse_conf_state_machine.cc:230
Config error: Expected a Resource name identifier, got: BadParameter
            : line 2, col 13 of file /etc/bareos/bareos-dir.d/job/zz-bad.conf
BadParameter = yes

此时 -t 退出码为 42(echo $? 实测)。删除坏文件后复查恢复 0:

rm -f /etc/bareos/bareos-dir.d/job/zz-bad.conf
runuser -u bareos -- bareos-dir -t; echo "EXIT=$?"
EXIT=0

💡 说明:配置错误时 bareos-dir -t 的退出码为 42(Bareos 配置错误的专用码),不是常见的 1/2;脚本里判断「非 0 即失败」即可,无需记住具体值。坏配置存在期间 systemctl restart bareos-director 会导致服务起不来,先 -t 再 reload/restart 是安全顺序。

7.3 bareos-dbcheck -B:目录库连接参数导出

-B(print bootstrap config)把 Catalog 资源解析后的数据库连接参数打出,给 bscan 等工具取连接参数时很有用(见 6.3/6.4 节实测):

$ bareos-dbcheck -B
catalog=MyCatalog
db_name=bareos
db_driver=postgresql
db_user=bareos
db_password=
db_address=
db_port=0
db_socket=
db_type=PostgreSQL
working_dir=/var/lib/bareos

八、systemd 服务管理

8.1 单元名与别名

Ubuntu 包内置三个守护进程单元,bareos-dir 等短名只是 alias(实测 systemctl list-unit-files):

$ systemctl list-unit-files | grep -i bareos
bareos-dir.service                     alias           -
bareos-director.service                enabled         enabled
bareos-fd.service                      alias           -
bareos-filedaemon.service              enabled         enabled
bareos-sd.service                      alias           -
bareos-storage.service                 enabled         enabled

日常用哪个名字都能操作(systemd 会解析别名),但看 is-enabled 结果时注意:查别名返回 alias,查真名返回 enabled(实测):

$ systemctl is-enabled bareos-dir
alias
$ systemctl is-enabled bareos-director
enabled

8.2 状态查看、重启与日志

status(实测节选):

$ systemctl status bareos-director --no-pager
● bareos-director.service - Bareos Director Daemon service
     Loaded: loaded (/lib/systemd/system/bareos-director.service; enabled; vendor preset: enabled)
     Active: active (running) since Wed 2026-10-21 17:40:48 UTC; 10h ago
       Docs: man:bareos-dir(8)
   Main PID: 15595 (bareos-dir)
      Tasks: 3 (limit: 94371)
     Memory: 5.7M
        CPU: 6.719s
     CGroup: /system.slice/.../bareos-director.service
             └─15595 /usr/sbin/bareos-dir -f

重启 Director 并验证恢复连接(每条命令针对一个单元):

systemctl restart bareos-director
systemctl is-active bareos-director
active

重启后立即用 bconsole 复核(实测回显见 3.2 节,1000 OK 即恢复)。单元日志用 journalctl 按真名查(实测):

$ journalctl -u bareos-director -n 4 --no-pager
Oct 22 03:55:17 bareos-server systemd[1]: Stopped Bareos Director Daemon service.
Oct 22 03:55:17 bareos-server systemd[1]: bareos-director.service: Consumed 6.722s CPU time.
Oct 22 03:55:17 bareos-server systemd[1]: Started Bareos Director Daemon service.
Oct 22 03:56:45 bareos-server bareos-dir[21661]: lib/bsock_tcp.cc:532 Socket is terminated=1 on call to client:::1:9101

8.3 单元文件要点

systemctl cat bareos-director 实测节选(关注依赖与启动方式):

# /lib/systemd/system/bareos-director.service
#
# Copyright (C) 2013-2023 Bareos GmbH & Co. KG
...
[Unit]
Description=Bareos Director Daemon service
Documentation=man:bareos-dir(8)
Requires=network.target nss-lookup.target time-sync.target
After=network-online.target nss-lookup.target local-fs.target remote-fs.target time-sync.target postgresql.service
# Dependency about the database
# We let administrators decide if they need it (if local db instance)
# Wants=postgresql.service
# Check if working dir exist
ConditionPathExists=/var/lib/bareos

要点:Director 在 postgresql.service 之后启动(本地库场景的关键依赖,注释中 Wants= 默认被注释掉,说明包方让管理员自行决定是否强依赖数据库);ConditionPathExists=/var/lib/bareos 不满足时服务直接跳过启动。SD/FD 单元结构同理,不再赘述。

九、恢复闭环实录

本章用环境里真实存在的备份数据(JobId 14,Full 级别,202 文件)完整走一遍「bconsole 交互恢复 → 落地校验 → 单文件恢复 → bextract 旁路交叉验证」。

9.1 bconsole 全交互恢复到自定义目录

进入 restore 后首先选择 JobId 的 13 项菜单(实测):

*restore
Automatically selected Catalog: MyCatalog
Using Catalog "MyCatalog"

First you select one or more JobIds that contain files
to be restored. You will be presented several methods
of specifying the JobIds. Then you will be allowed to
select which files from those JobIds are to be restored.

To select the JobIds, you have the following choices:
 1: List last 20 Jobs run
 2: List Jobs where a given File is saved
 3: Enter list of comma separated JobIds to select
 4: Enter SQL list command
 5: Select the most recent backup for a client
 6: Select backup for a client before a specified time
 7: Enter a list of files to restore
 8: Enter a list of files to restore before a specified time
 9: Find the JobIds of the most recent backup for a client
10: Find the JobIds for a backup for a client before a specified time
11: Enter a list of directories to restore for found JobIds
12: Select full restore to a specified Job date
13: Cancel

选 3(直接给 JobId),输入 14;Director 构建文件树后进入文件选择模式,mark * 标记全部 202 个文件,done 结束选择(实测全程回显):

Select item:  (1-13): 3
Enter JobId(s), comma separated, to restore: 14
You have selected the following JobId: 14

Building directory tree for JobId(s) 14 ...  ++++++++++++++++++++++++++++++++++++++++
202 files inserted into the tree.

You are now entering file selection mode where you add (mark) and
remove (unmark) files to be restored. No files are initially added, unless
you used the "all" keyword on the command line.
Enter "done" to leave this mode.

cwd is: /
$ mark *
202 files newly marked.
$ done
Bootstrap records written to /var/lib/bareos/bareos-dir.restore.14.bsr

The job will require the following
   Volume(s)                 Storage(s)                SD Device(s)
===========================================================================

    Full-0001                 File                      FileStorage

Volumes marked with "*" are online.


202 files selected to be restored.

Automatically selected Client: bareos-fd
Using Catalog "MyCatalog"
Run Restore job
JobName:         RestoreFiles
Bootstrap:       /var/lib/bareos/bareos-dir.restore.14.bsr
Where:           /tmp/bareos-restores
Replace:         Always
FileSet:         LinuxAll
Backup Client:   bareos-fd
Restore Client:  bareos-fd
Format:          Native
Storage:         File
When:            2026-10-22 03:51:04
Catalog:         MyCatalog
Priority:        10
Plugin Options:  *None*
OK to run? (yes/mod/no):

文件选择模式常用命令:cd/dir/ls 浏览树、mark <路径>/unmark <路径> 标记、mark * 全选、count 统计、done 结束。恢复目的地由任务模板里的 Where(默认 /tmp/bareos-restores)控制,本次要恢复到自定义目录,在 OK to run? 处答 mod 修改参数。

踩坑实录:25.1 的 mod 菜单共 14 项,Where 是第 10 项。按旧版资料想当然输入 3 会选中「Job」(实测回显,随后输入自定义路径被拒):

OK to run? (yes/mod/no): mod
Parameters to modify:
 1: Level
 2: Storage
 3: Job
 4: FileSet
 5: Restore Client
 6: Backup Format
 7: When
 8: Priority
 9: Bootstrap
10: Where
11: File Relocation
12: Replace
13: JobId
14: Plugin Options
Select parameter to modify (1-14): 3
The defined Job resources are:
1: backup-bareos-fd
2: BackupCatalog
3: RestoreFiles
Select Job resource (1-3): /tmp/bareos-restore-d1
Expected a positive integer, got: /tmp/bareos-restore-d1
Select Job resource (1-3): .
Selection aborted, nothing done.
Job not run.

正确输入 10 后填入目标路径,摘要立即更新为新的 Where,再答 yes 运行(实测):

Select parameter to modify (1-14): 10
Please enter the full path prefix for restore (/ for none): /tmp/bareos-restore-d1
Run Restore job
JobName:         RestoreFiles
Bootstrap:       /var/lib/bareos/bareos-dir.restore.14.bsr
Where:           /tmp/bareos-restore-d1
Replace:         Always
FileSet:         LinuxAll
Backup Client:   bareos-fd
Restore Client:  bareos-fd
Format:          Native
Storage:         File
When:            2026-10-22 03:51:04
Catalog:         MyCatalog
Priority:        10
Plugin Options:  *None*
OK to run? (yes/mod/no): yes
Job queued. JobId=15

wait 等任务结束后 messages 取回报告(实测完整统计块):

22-Oct 03:51 bareos-sd JobId 15: Ready to read from volume "Full-0001" on device "FileStorage0001" (/var/lib/bareos/storage).
22-Oct 03:51 bareos-sd JobId 15: Forward spacing Volume "Full-0001" to file:block 0:76612558.
22-Oct 03:51 bareos-sd JobId 15: End of Volume at file 0 on device "FileStorage0001" (/var/lib/bareos/storage), Volume "Full-0001"
22-Oct 03:51 bareos-sd JobId 15: End of all volumes.
22-Oct 03:51 bareos-sd JobId 15: Releasing device "FileStorage0001" (/var/lib/bareos/storage).
22-Oct 03:51 bareos-dir JobId 15: Bareos bareos-dir 25.1.2~pre49.d5742f640 (16Oct26):
  OS Information:         Ubuntu 22.04.5 LTS
  JobId:                  15
  Job:                    RestoreFiles.2026-10-22_03.51.12_51
  Restore Client:         "bareos-fd" 25.1.2~pre49.d5742f640 (16Oct26) Ubuntu 22.04.5 LTS,ubuntu
  Start time:             22-Oct-2026 03:51:14
  End time:               22-Oct-2026 03:51:16
  Elapsed time:           2 secs
  Files Expected:         202
  Files Restored:         202
  Bytes Restored:         19,081,236
  Rate:                   9540.6 KB/s
  FD Errors:              0
  FD termination status:  OK
  SD termination status:  OK
  Bareos binary info:     Bareos community build (UNSUPPORTED): Get professional support from https://www.bareos.com
  Job triggered by:       User
  Termination:            Restore OK

判定标准:Files Expected == Files Restored、FD Errors: 0、Termination: Restore OK 三项同时满足。

9.2 恢复结果校验

Where 前缀下按原路径结构落地(实测):

$ ls -la /tmp/bareos-restore-d1/
total 0
drwxr-xr-x  3 root bareos  60 Oct 22 03:51 .
drwxrwxrwt 13 root root   600 Oct 22 03:51 ..
drwxr-xr-x  3 root bareos  60 Oct 22 03:51 usr
$ ls -la /tmp/bareos-restore-d1/usr/sbin/ | head -6
total 18940
drwxr-xr-x 2 root root        4060 Oct 21 17:35 .
drwxr-xr-x 3 root bareos        60 Oct 22 03:51 ..
lrwxrwxrwx 1 root root           7 Oct 22 03:51 a2disconf -> a2enmod
lrwxrwxrwx 1 root root           7 Oct 22 03:51 a2dismod -> a2enmod

逐字节校验与源文件一致(sha256 与 cmp 双重验证,实测):

$ sha256sum /tmp/bareos-restore-d1/usr/sbin/reboot /usr/sbin/reboot
de4c5cdd03758593036da105a57f959b76bd1ad6e0d1e567d29d16458aeaddf6  /tmp/bareos-restore-d1/usr/sbin/reboot
de4c5cdd03758593036da105a57f959b76bd1ad6e0d1e567d29d16458aeaddf6  /usr/sbin/reboot
$ cmp /tmp/bareos-restore-d1/usr/sbin/reboot /usr/sbin/reboot && echo "cmp: 内容一致"
cmp: 内容一致

💡 说明:/usr/sbin/reboot 本身是指向 /bin/systemctl 的符号链接(ls -la 实测可见),备份与恢复都会保留符号链接属性;上面 sha256 相同是因为校验工具跟随链接读到了同一目标文件。

9.3 单文件恢复(restore 菜单选项 7)

只恢复个别文件时,菜单选项 7「直接输入文件路径」最快。实测恢复 /usr/sbin/reboot 到默认 Where:

Select item:  (1-13): 7
Automatically selected Client: bareos-fd
Enter file names with paths, or < to enter a filename
containing a list of file names with paths, and Terminate
them with a blank line.
Enter full filename: /usr/sbin/reboot
Enter full filename: 
Bootstrap records written to /var/lib/bareos/bareos-dir.restore.16.bsr

The job will require the following
   Volume(s)                 Storage(s)                SD Device(s)
===========================================================================
   
    Full-0001                 File                      FileStorage              

Volumes marked with "*" are online.


1 file selected to be restored.
...(确认运行后)...
OK to run? (yes/mod/no): yes
Job queued. JobId=17

messages 回报(实测统计块):

  JobId:                  17
  Job:                    RestoreFiles.2026-10-22_03.52.19_55
  Restore Client:         "bareos-fd" 25.1.2~pre49.d5742f640 (16Oct26) Ubuntu 22.04.5 LTS,ubuntu
  Start time:             22-Oct-2026 03:52:21
  End time:               22-Oct-2026 03:52:23
  Elapsed time:           2 secs
  Files Expected:         1
  Files Restored:         1
  Bytes Restored:         0
  FD Errors:              0
  FD termination status:  OK
  SD termination status:  OK
  Termination:            Restore OK

Bytes Restored: 0 并非异常——目标是符号链接,恢复的是链接本身(0 字节数据)。菜单其余选项速查:1 列最近 20 个任务、2 按文件名反查所在任务、4 写 SQL 选任务、5/6/9/10 按时间点与客户端选任务、11 输入目录列表恢复、12 按任务日期全量恢复。

9.4 bextract 裸恢复交叉验证

第六章 6.2 节已实测用 bextract 从同一卷 Full-0001 抽出同一文件,sha256 与 bconsole 恢复结果、源文件三方一致。两条恢复路径的数据源完全相同,验证了卷数据本身的可读性与完整性——这也是「Director 全挂时仍可取数」的实证。

9.5 三种恢复路径对比与选择

路径依赖文件选择方式目录库写入适用场景
bconsole restoreDirector + 目录库 + SD + FD目录树 mark/单文件路径/时间点是(Restore 任务记录)日常恢复的首选,可跨时间点合并增量
restore 菜单 7(单文件)同上直接给绝对路径是明确知道路径的少量文件
bextract仅 SD 侧卷文件可读-i 包含清单(带前导斜杠)否Director/数据库不可用的应急取数

选型结论:能用 bconsole restore 就用它(增量合并、版本选择、任务留痕都靠它);bextract 是最后的兜底手段,恢复前建议先 bls -j 确认卷内会话完整。

十、注意事项

  1. 交互提示下 . 是取消键:任何询问提示符(Select item、OK to run?、Enter filename 等)输入 . 即放弃当前操作;写自动化脚本时注意别把 . 混进答案序列(9.1 节踩坑即为其一)。
  2. messages 读一次清一次:重要报告先 list joblog jobid=<N> 固化再清理,或直接依赖目录库留痕。
  3. 卷操作先看状态再动手:relabel/truncate/prune 的前置条件都挂在 VolStatus 状态机上(4.9 节技术拓展表),操作前 llist volume=<名> 确认状态,避免盲目 purge——purge 是无视保留期的危险操作。
  4. delete 不删物理文件:delete volume 只删目录库记录,/var/lib/bareos/storage/<卷名> 文件需手工 rm(本文测试卷文件已同步清理)。
  5. 配置校验身份:bareos-dir -t 必须 runuser -u bareos 执行(7.1 节 Peer 认证坑);改配置后先 -t 再 reload,退出码 42 即配置错误(7.2 节)。
  6. 独立工具的参数是设备资源名不是路径:bls/bextract/bscan 的位置参数一律填 SD Device 资源名(如 FileStorage0000),设备清单从报错提示或 status storage 获取(6.1 节踩坑)。
  7. bscan 加 -s 才写库:预演不带 -s,对有数据的卷写入前先备份数据库(6.3 节)。
  8. bextract 包含清单路径带前导斜杠:/usr/sbin/reboot 能匹配,usr/sbin/reboot 返回 0 文件(6.2 节实测)。
  9. 任务排队有并发上限:Director/Storage 资源默认 Maximum Concurrent Jobs = 10(4.4 节 show storages 可见),批量发起任务前留意该值。

十一、常见问题

Q1:bconsole 报 Failed to connect to Director. Giving up.? 按序排查:systemctl is-active bareos-director(服务挂了先 restart)→ bconsole -t(bconsole.conf 是否合法)→ bconsole -l(Director 名是否匹配)→ -d 50 看 TLS/网络层日志(5.4 节)。Director 正常但连接被拒时,检查 bareos-dir.conf 的监听地址与防火墙 9101。

Q2:任务跑完了但 list jobs 看不到? 运行中的任务不实时入库,结束后才写入;刚结束的任务若 messages 有报告而 list jobs 无记录,检查 Director 到数据库的连接(status dir 首行 db:postgresql 旁无报错即正常)。确认目录库服务本身:systemctl is-active postgresql。

Q3:relabel 报 "must be Purged or Recycled before relabeling"? 见 4.9 节实测:卷处于 Append/Full/Used 状态时先 purge volume=<名> yes(或按保留期 prune)转 Purged,再 relabel。update volume=<名> volstatus=Purged 这条路走不通,25.1 会回 Invalid VolStatus specified: Purged。

Q4:恢复后文件是空的/字节数为 0? 先确认源文件是否符号链接(9.3 节 Bytes Restored: 0 即链接文件属正常)。真实文件为 0 字节时用 list joblog jobid=<N> 查恢复过程有无 FD 报错,并用 bls -j 核对卷内该 Job 会话的 Status=T。

Q5:systemctl is-enabled bareos-dir 返回 alias 而不是 enabled? 单元别名机制所致,真名为 bareos-director.service(8.1 节实测)。判断开机自启用真名查询或直接看 list-unit-files 中真名行的 enabled 状态。

Q6:restore 的 mod 菜单里找不到 Where? 25.1 的参数菜单共 14 项、Where 编号 10(9.1 节踩坑实录含完整菜单回显),不要照搬旧版 Bacula 资料的编号。

Q7:estimate 结果与上次备份文件数不一致? estimate 经 FD 实时扫描当前文件系统,反映的是 FileSet 的当下状态;上次备份是历史快照。若要核对 FileSet 规则本身,用 testfind 离线匹配(6.6 节)。

Q8:想按 SQL 自定义报表? sqlquery 直查目录库(4.11 节实测),常用表:Job(任务)、Media(卷)、Pool、Client、FileSet、JobMedia(任务-卷映射)、Filename/Path/File(文件明细)。只读查询也建议带 limit。

十二、参考链接