Logging, backup and undo
Here's a nice-to-have idea.
- Create a logging table in local.sqlite to record all actions that modify the locally installed package set
- Any time a package is deleted or replaced, create a backup tarball and stash it away under /var/cache/pkg/backup
- New commands
pkg historyandpkg undo - New config item: PKG_UNDO_LEVEL (integer) -- number of package history operations to be able to undo
- Extend
pkg cleanto clear out old package backups according to PKG_UNDO_LEVEL
If we want to get fancy and crunch down on space usage, the backup package could contain just the files that differ between old and new versions.
More complex than it seems. If initially package bar-1.00 depends on foo-1.00, and after update we have bar-2.00 depends on foo-2.00, then reverting the foo-1.00 -> foo-2.00 update implies reverting the bar-1.00 to bar-2.00 update and vice-versa.
This is partially implemented by the pkg-plugins-zfssnap plugin which creates a ZFS snapshot before taking any install/deinstall actions.
On the other hand it would be really nice to have a native "history" command in pkg which does this.
The logging table could also be a separate sqlite db, e.g. /var/db/history.sqlite in order to keep the local.sqlite small in size for doing backups and restore for example.
Issue #359 probably could be implemented as part of this one as well.
Package database backup
https://github.com/freebsd/freebsd-doc/pull/542/commits/1f8242e8c383a84fd8a0872a162e62b1072e1045 in:
- https://github.com/freebsd/freebsd-doc/pull/542
Users may be unaware of the script.
Also: backups (not limited to the package database) may be non-existent or, as shown below, significantly outdated.
/var/backups/pkg.sql.xz before and after a manual run of 411.pkg-backup
root@pkg:~ # date ; ls -hlnrt /var/backups
Sun Aug 31 21:26:26 BST 2025
total 322
-rw-r--r-- 1 0 0 1.6K May 12 2022 aliases.bak2
-rw-r--r-- 1 0 0 716B Oct 2 2022 group.bak2
-rw------- 1 0 0 2.8K Oct 2 2022 master.passwd.bak2
-rw-r--r-- 1 0 0 437B Oct 3 2022 kern.geom.conftxt.bak2
-rw-r--r-- 1 0 0 142B Oct 3 2022 gpart.ada0.bak
-rw-r--r-- 1 0 0 512K Oct 3 2022 boot.ada0p1.bak2
-rw-r--r-- 1 0 0 1.6K Feb 21 2025 aliases.bak
-rw-r--r-- 1 0 0 739B Mar 21 02:53 group.bak
-rw------- 1 0 0 2.9K Mar 21 02:53 master.passwd.bak
-rw-r--r-- 1 0 0 428B Mar 21 03:01 kern.geom.conftxt.bak
-rw-r--r-- 1 0 0 512K Mar 21 03:01 boot.ada0p1.bak
-rw-r--r-- 1 0 0 0B Mar 21 03:58 pkg.sql.xz
root@pkg:~ # /usr/local/etc/periodic/daily/411.pkg-backup
Backing up pkg database:
root@pkg:~ # ls -hln /var/backups/pkg.sql.xz
-rw-r--r-- 1 0 0 17M Aug 31 21:27 /var/backups/pkg.sql.xz
root@pkg:~ #
https://github.com/freebsd/pkg/blob/main/scripts/periodic/411.pkg-backup.in
Restoration from a backup
rm /var/db/pkg/local.sqlite
xzcat /var/backups/pkg.sql.xz | pkg shell
From pkg(8) examples.