modified to use a single R/W snapshot
This commit is contained in:
parent
d58d047d2a
commit
74a055dfe6
5 changed files with 20 additions and 116 deletions
31
README.md
31
README.md
|
|
@ -1,40 +1,25 @@
|
|||
# snap-pac
|
||||
|
||||

|
||||

|
||||
|
||||
## Synopsis
|
||||
|
||||
This is a set of [pacman](https://wiki.archlinux.org/index.php/Pacman) hooks and script
|
||||
that automatically causes [snapper](http://snapper.io/) to perform a pre and post
|
||||
snapshot before and after pacman transactions, similar to how YaST does with OpenSuse.
|
||||
This provides a simple way to undo changes to a system after a pacman transaction.
|
||||
This is a set of [pacman](https://wiki.archlinux.org/index.php/Pacman) hooks and scripts that automatically cause [snapper](http://snapper.io/) to perform a snapshot before pacman transactions.
|
||||
|
||||
For more information, [see the documentation](https://wesbarnett.github.io/snap-pac/).
|
||||
Forked from Wes Barnett's solution for my own personal needs. Takes a single screenshot vs. the original's pre/post pair.
|
||||
|
||||
## Installation
|
||||
|
||||
Install the `snap-pac` package using pacman.
|
||||
|
||||
For instructions on how to install without pacman, [see the
|
||||
documentation](https://wesbarnett.github.io/snap-pac/installation.html).
|
||||
## [Installation] (https://wesbarnett.github.io/snap-pac/installation.html).
|
||||
|
||||
## Configuration
|
||||
|
||||
Most likely, configuration is not needed. By default, the snapper configuration named
|
||||
`root` will have pre/post snapshots taken for every pacman transaction.
|
||||
Most likely, configuration is not needed. By default, the snapper configuration named `root` will have snapshots taken before every pacman transaction.
|
||||
|
||||
For more information on configuring snap-pac, see [the
|
||||
documentation](https://wesbarnett.github.io/snap-pac/configuration.html).
|
||||
For more information on configuring snap-pac, see [the documentation](https://wesbarnett.github.io/snap-pac/configuration.html).
|
||||
|
||||
## Documentation
|
||||
|
||||
See the [documentation here](https://wesbarnett.github.io/snap-pac/) or `man 8 snap-pac`
|
||||
after installation.
|
||||
See the [documentation here](https://wesbarnett.github.io/snap-pac/) or `man 8 snap-pac` after installation.
|
||||
|
||||
## Troubleshooting
|
||||
|
||||
After reviewing the documentation, [check the issues page] and file a new issue if your
|
||||
problem is not covered.
|
||||
After reviewing the documentation, [check the issues page] and file a new issue if your problem is not covered.
|
||||
|
||||
[check the issues page]: https://github.com/wesbarnett/snap-pac/issues
|
||||
[check the issues page]: https://codeberg.org/ak95/snap-pac/issues
|
||||
|
|
|
|||
|
|
@ -20,14 +20,6 @@
|
|||
## Default is number
|
||||
#cleanup_algorithm = number
|
||||
|
||||
## Pre snapshot description.
|
||||
## Default is the pacman command that triggered the hook
|
||||
#pre_description = pacman pre snapshot
|
||||
|
||||
## Post snapshot description.
|
||||
## Default is the list of packages involved in the pacman transaction
|
||||
#post_description = pacman post snapshot
|
||||
|
||||
## Uncomment to add "important=yes" to userdata for snapshots referring to these packages
|
||||
## Default is []
|
||||
#important_packages = ["linux", "linux-lts"]
|
||||
|
|
|
|||
|
|
@ -24,9 +24,9 @@ Type = Package
|
|||
Target = *
|
||||
|
||||
[Action]
|
||||
Description = Performing snapper pre snapshots for the following configurations...
|
||||
Description = Taking snapper snapshot before installing...
|
||||
Depends = snap-pac
|
||||
When = PreTransaction
|
||||
Exec = /usr/share/libalpm/scripts/snap-pac pre
|
||||
Exec = /usr/share/libalpm/scripts/snap-pac
|
||||
NeedsTargets
|
||||
AbortOnFail
|
||||
|
|
@ -1,31 +0,0 @@
|
|||
# snap-pac
|
||||
# https://github.com/wesbarnett/snap-pac
|
||||
# Copyright (C) 2016, 2017, 2018 James W. Barnett
|
||||
|
||||
# This program is free software; you can redistribute it and/or modify
|
||||
# it under the terms of the GNU General Public License as published by
|
||||
# the Free Software Foundation; either version 2 of the License, or
|
||||
# (at your option) any later version.
|
||||
|
||||
# This program is distributed in the hope that it will be useful,
|
||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
# GNU General Public License for more details.
|
||||
|
||||
# You should have received a copy of the GNU General Public License along
|
||||
# with this program; if not, write to the Free Software Foundation, Inc.,
|
||||
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
|
||||
[Trigger]
|
||||
Operation = Upgrade
|
||||
Operation = Install
|
||||
Operation = Remove
|
||||
Type = Package
|
||||
Target = *
|
||||
|
||||
[Action]
|
||||
Description = Performing snapper post snapshots for the following configurations...
|
||||
Depends = snap-pac
|
||||
When = PostTransaction
|
||||
Exec = /usr/share/libalpm/scripts/snap-pac post
|
||||
NeedsTargets
|
||||
|
|
@ -14,7 +14,7 @@
|
|||
# You should have received a copy of the GNU General Public License along
|
||||
# with this program; if not, write to the Free Software Foundation, Inc.,
|
||||
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
"""Script for taking pre/post snapshots; run from pacman hooks."""
|
||||
"""Script for taking snapshots; run from pacman hooks."""
|
||||
|
||||
from argparse import ArgumentParser
|
||||
from configparser import ConfigParser
|
||||
|
|
@ -31,8 +31,7 @@ logging.basicConfig(format="%(message)s", level=logging.INFO)
|
|||
|
||||
class SnapperCmd:
|
||||
|
||||
def __init__(self, config, snapshot_type, cleanup_algorithm, description="",
|
||||
nodbus=False, pre_number=None, userdata=""):
|
||||
def __init__(self, config, cleanup_algorithm, description="", nodbus=False, userdata=""):
|
||||
self.cmd = ["snapper"]
|
||||
if nodbus:
|
||||
self.cmd.append("--no-dbus")
|
||||
|
|
@ -41,19 +40,11 @@ class SnapperCmd:
|
|||
f"--cleanup-algorithm {cleanup_algorithm}",
|
||||
"--print-number"
|
||||
])
|
||||
self.cmd.append("--read-write")
|
||||
if description:
|
||||
self.cmd.append(f"--description \"{description}\"")
|
||||
if userdata:
|
||||
self.cmd.append(f"--userdata \"{userdata}\"")
|
||||
if snapshot_type == "post":
|
||||
if pre_number is not None:
|
||||
self.cmd.append(f"--pre-number {pre_number}")
|
||||
else:
|
||||
logging.debug("snapshot type specified as 'post' but no pre snapshot number, "
|
||||
"so setting snapshot type to 'single'. If installing "
|
||||
"snap-pac this is normal.")
|
||||
snapshot_type = "single"
|
||||
self.cmd.append(f"--type {snapshot_type}")
|
||||
|
||||
def __call__(self):
|
||||
return os.popen(self.__str__()).read().rstrip("\n")
|
||||
|
|
@ -64,7 +55,7 @@ class SnapperCmd:
|
|||
|
||||
class ConfigProcessor:
|
||||
|
||||
def __init__(self, ini_file, snapshot_type, parent_cmd=None, packages=None):
|
||||
def __init__(self, ini_file, parent_cmd=None, packages=None):
|
||||
"""Set up defaults for snap-pac configuration."""
|
||||
|
||||
if parent_cmd is None:
|
||||
|
|
@ -77,14 +68,11 @@ class ConfigProcessor:
|
|||
else:
|
||||
self.packages = packages
|
||||
|
||||
self.snapshot_type = snapshot_type
|
||||
|
||||
self.config = ConfigParser()
|
||||
self.config["DEFAULT"] = {
|
||||
"snapshot": False,
|
||||
"cleanup_algorithm": "number",
|
||||
"pre_description": self.parent_cmd,
|
||||
"post_description": " ".join(self.packages),
|
||||
"description": self.parent_cmd,
|
||||
"desc_limit": 72,
|
||||
"important_packages": [],
|
||||
"important_commands": [],
|
||||
|
|
@ -100,7 +88,7 @@ class ConfigProcessor:
|
|||
|
||||
def get_description(self, section):
|
||||
desc_limit = self.config.getint(section, "desc_limit")
|
||||
return self.config.get(section, f"{self.snapshot_type}_description")[:desc_limit]
|
||||
return self.config.get(section, "description")[:desc_limit]
|
||||
|
||||
def check_important_commands(self, section):
|
||||
return self.parent_cmd in json.loads(self.config.get(section, "important_commands"))
|
||||
|
|
@ -138,38 +126,12 @@ def get_snapper_configs(conf_file):
|
|||
return line[1].lstrip("\"").split()
|
||||
|
||||
|
||||
class Prefile:
|
||||
"""Handles reading and writing of pre snapshot number."""
|
||||
def __init__(self, snapper_config, snapshot_type):
|
||||
self.file = Path(tempfile.gettempdir()) / f"snap-pac-pre_{snapper_config}"
|
||||
self.snapshot_type = snapshot_type
|
||||
|
||||
def read(self):
|
||||
if self.snapshot_type == "pre":
|
||||
pre_number = None
|
||||
else:
|
||||
try:
|
||||
pre_number = self.file.read_text().rstrip("\n")
|
||||
except FileNotFoundError:
|
||||
pre_number = None
|
||||
logging.debug(f"prefile {self.file} not found. Ensure you have run the pre snapshot first. "
|
||||
"If installing snap-pac this is normal.")
|
||||
else:
|
||||
self.file.unlink()
|
||||
return pre_number
|
||||
|
||||
def write(self, num):
|
||||
if self.snapshot_type == "pre":
|
||||
self.file.write_text(num)
|
||||
|
||||
|
||||
def check_skip():
|
||||
return os.getenv("SNAP_PAC_SKIP", "n").lower() in ["y", "yes", "true", "1"]
|
||||
|
||||
|
||||
def parse_args():
|
||||
parser = ArgumentParser(description="Script for taking pre/post snapper snapshots. Used with pacman hooks.")
|
||||
parser.add_argument(dest="type", choices=["pre", "post"], help="snapper snapshot type")
|
||||
parser = ArgumentParser(description="Script for taking snapper snapshots. Used with pacman hooks.")
|
||||
parser.add_argument(
|
||||
"--ini", dest="snap_pac_ini", type=Path,
|
||||
default=Path("/etc/snap-pac.ini"), help="snap-pac ini file path"
|
||||
|
|
@ -188,16 +150,12 @@ if __name__ == "__main__":
|
|||
quit()
|
||||
|
||||
args = parse_args()
|
||||
config_processor = ConfigProcessor(args.snap_pac_ini, args.type)
|
||||
config_processor = ConfigProcessor(args.snap_pac_ini)
|
||||
chroot = os.stat("/") != os.stat("/proc/1/root/.")
|
||||
|
||||
for snapper_config in get_snapper_configs(args.snapper_conf_file):
|
||||
|
||||
data = config_processor(snapper_config)
|
||||
if data["snapshot"]:
|
||||
prefile = Prefile(snapper_config, args.type)
|
||||
pre_number = prefile.read()
|
||||
num = SnapperCmd(snapper_config, args.type, data["cleanup_algorithm"],
|
||||
data["description"], chroot, pre_number, data["userdata"])()
|
||||
num = SnapperCmd(snapper_config, data["cleanup_algorithm"], data["description"], chroot, data["userdata"])()
|
||||
logging.info(f"==> {snapper_config}: {num}")
|
||||
prefile.write(num)
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue