https://wiki.archlinux.org/api.php?action=feedcontributions&user=Coldiron&feedformat=atomArchWiki - User contributions [en]2024-03-29T12:54:07ZUser contributionsMediaWiki 1.41.0https://wiki.archlinux.org/index.php?title=Snapper&diff=572225Snapper2019-04-30T05:23:12Z<p>Coldiron: Fixed typo - inherit -> inherent</p>
<hr />
<div>[[Category:File systems]]<br />
[[ja:Snapper]]<br />
[[zh-hans:Snapper]]<br />
{{Related articles start}}<br />
{{Related|Btrfs}}<br />
{{Related|mkinitcpio}}<br />
{{Related articles end}}<br />
<br />
[http://snapper.io Snapper] is a tool created by openSUSE's Arvin Schnell that helps with managing snapshots of [[Btrfs]] subvolumes and thin-provisioned [[LVM]] volumes. It can create and compare snapshots, revert between snapshots, and supports automatic snapshots timelines.<br />
<br />
==Installation==<br />
<br />
[[Install]] the {{pkg|snapper}} package. The development version {{AUR|snapper-git}} is also available.<br />
<br />
Additionally, a GUI is available with {{AUR|snapper-gui-git}}.<br />
<br />
==Create a new configuration==<br />
<br />
Before creating a snapper configuration for a btrfs subvolume, the subvolume must already exist. If it does not, you should [[Btrfs#Creating a subvolume|create]] it before generating a snapper configuration.<br />
<br />
To create a new snapper configuration named {{ic|''config''}} for the btrfs subvolume at {{ic|''/path/to/subvolume''}} do:<br />
<br />
# snapper -c ''config'' create-config ''/path/to/subvolume''<br />
<br />
This will:<br />
*create a configuration file at {{ic|/etc/snapper/configs/''config''}} based on the default template from {{ic|/etc/snapper/config-templates}}.<br />
*create a subvolume at {{ic|''/path/to/subvolume''/.snapshots}} where future snapshots of for this configuration will be stored. A snapshot's path is {{ic|''/path/to/subvolume''/.snapshots/''#''/snapshot}}, where {{ic|''#''}} is the snapshot number.<br />
*add {{ic|''config''}} to {{ic|SNAPPER_CONFIGS}} in {{ic|/etc/conf.d/snapper}}.<br />
<br />
For example, to create a configuration file for the subvolume mounted at {{ic|/}} do:<br />
<br />
# snapper -c root create-config /<br />
<br />
At this point, the configuration is active. If your [[cron]] daemon is running, snapper will take [[#Automatic timeline snapshots]]. If you do not use a [[cron]] daemon, you will need to use the systemd service and timer. See [[#Enable/disable]].<br />
<br />
See [[man page]] for {{ic|snapper-configs}}.<br />
<br />
== Take snapshots ==<br />
<br />
=== Automatic timeline snapshots ===<br />
<br />
A snapshot timeline can be created with a configurable number of snapshots kept per hour/day/month/year. When the timeline is enabled, by default a snapshot gets created once an hour. Once a day the snapshots get cleaned up by the timeline cleanup algorithm.<br />
<br />
==== Enable/disable ====<br />
<br />
If you have a [[cron]] daemon, this feature should start automatically. To disable it, edit the configuration file corresponding with the subvolume you do not want to have this feature and set:<br />
<br />
{{bc|<nowiki>TIMELINE_CREATE="no"</nowiki>}}<br />
<br />
If you do not have a [[cron]] daemon, you can use the provided systemd units. [[Start]] and [[enable]] {{ic|snapper-timeline.timer}} to start the automatic snapshot timeline. Additionally, [[start]] and [[enable]] {{ic|snapper-cleanup.timer}} to periodically cleanup older snapshots.<br />
<br />
{{Note|If you have a cron daemon and also enable the systemd units, this may result in duplicate snapshots being created. If you wish to disable cron integration while using the systemd units, one possible solution is not to install the snapper package's cron files via [[pacman]]'s [[Pacman#Skip_files_from_being_installed_to_system|NoExtract]] and [[Pacman#Skip_file_from_being_upgraded|NoUpgrade]] configuration options. See [https://unix.stackexchange.com/questions/425570/snapper-has-recently-started-performing-duplicate-snapshots-each-hour]}}<br />
<br />
==== Set snapshot limits ====<br />
<br />
The default settings will keep 10 hourly, 10 daily, 10 monthly and 10 yearly snapshots. You may want to change this in the configuration, especially on busy subvolumes like {{ic|/}}. See [[#Preventing slowdowns]].<br />
<br />
Here is an example section of a configuration named {{ic|''config''}} with only 5 hourly snapshots, 7 daily ones, no monthly and no yearly ones:<br />
{{hc|head=/etc/snapper/configs/''config''|output=<br />
TIMELINE_MIN_AGE="1800"<br />
TIMELINE_LIMIT_HOURLY="5"<br />
TIMELINE_LIMIT_DAILY="7"<br />
TIMELINE_LIMIT_WEEKLY="0"<br />
TIMELINE_LIMIT_MONTHLY="0"<br />
TIMELINE_LIMIT_YEARLY="0"<br />
}}<br />
<br />
==== Change snapshot and cleanup frequencies ====<br />
<br />
If you are using the provided systemd timers, you can [[edit]] them to change the snapshot and cleanup frequency.<br />
<br />
For example, when editing the {{ic|snapper-timeline.timer}}, add the following to make the frequency every five minutes, instead of hourly:<br />
<br />
[Timer]<br />
OnCalendar=<br />
OnCalendar=*:0/5<br />
<br />
{{Note|The configuration parameter {{ic|TIMELINE_LIMIT_HOURLY}} is tied to the above setting. In the above example it now refers to how many 5-minute snapshots are kept.}}<br />
<br />
When editing {{ic|snapper-cleanup.timer}}, you need to change {{ic|OnUnitActiveSec}}. To make cleanups occur every hour instead of every day, add:<br />
<br />
[Timer]<br />
OnUnitActiveSec=1h<br />
<br />
See [[Systemd/Timers]] and [[Systemd#Drop-in files]].<br />
<br />
=== Manual snapshots ===<br />
<br />
==== Simple snapshots ====<br />
<br />
By default snapper takes snapshots that are of the ''simple'' type, having no special relationship to other snapshots.<br />
<br />
To take a snapshot of a subvolume manually, do:<br />
<br />
# snapper -c ''config'' create --description ''desc''<br />
<br />
The above command does not use any cleanup algorithm, so the snapshot is stored permanently or until [[#Delete_a_snapshot|deleted]].<br />
<br />
To set a cleanup algorithm, use the {{ic|-c}} flag after {{ic|create}} and choose either {{ic|number}}, {{ic|timeline}}, {{ic|pre}}, or {{ic|post}}. {{ic|number}} sets snapper to periodically remove snapshots that have exceeded a set number in the configuration file. For example, to create a snaphot that uses the {{ic|number}} algorithm for cleanup do:<br />
<br />
# snapper -c ''config'' create -c number<br />
<br />
See [[#Automatic timeline snapshots]] for how {{ic|timeline}} snapshots work and see [[#Pre/post snapshots]] on how {{ic|pre}} and {{ic|post}} work.<br />
<br />
==== Pre/post snapshots ====<br />
<br />
In addition to ''simple'' snapshots, you can also create ''pre/post'' snapshots where ''pre'' snapshots always have a corresponding ''post'' snapshot. The purpose of these pairs is to create a snapshot before and after a system modification.<br />
<br />
To create a pre/post snapshot pair, first create a ''pre'' snapshot:<br />
<br />
# snapper -c ''config'' create -t pre -p<br />
<br />
Note the number of the snapshot printed, as it is required for the post snapshot.<br />
<br />
Then perform a system modification (*e.g.*, install a new program, upgrade, etc.).<br />
<br />
Now create the ''post'' snapshot:<br />
<br />
# snapper -c ''config'' create -t post --pre-number ''N''<br />
<br />
where {{ic|''N''}} is the corresponding ''pre'' snapshot number.<br />
<br />
An alternative method is to use the {{ic|--command}} flag for {{ic|create}}, which wraps a command with pre/post snapshots:<br />
<br />
# snapper -c ''config'' create --command ''cmd''<br />
<br />
where {{ic|''cmd''}} is the command you wish to wrap with pre/post snapshots.<br />
<br />
See [[#Wrapping pacman transactions in snapshots]].<br />
<br />
=== Snapshots on boot ===<br />
<br />
To have snapper take a snapshot of the {{ic|root}} configuration, [[enable]] {{ic|snapper-boot.timer}}.<br />
<br />
== List snapshots ==<br />
<br />
To list snapshots taken for a given configuration ''config'' do:<br />
<br />
# snapper -c ''config'' list<br />
<br />
== List configurations ==<br />
<br />
To list all [[#Create a new configuration|configurations]] you have created do:<br />
<br />
# snapper list-configs<br />
<br />
== Delete a snapshot ==<br />
<br />
To delete a snapshot number {{ic|''N''}} do:<br />
<br />
# snapper -c ''config'' delete ''N''<br />
<br />
Multiple snapshots can be deleted at one time. For example, to delete snapshots 65 and 70 of the root configuration do:<br />
<br />
# snapper -c root delete 65 70<br />
<br />
{{Note|When deleting a pre snapshot, you should always delete its corresponding post snapshot and vice versa.}}<br />
<br />
==Access for non-root users==<br />
Each config is created with the root user, and by default, only root can see and access it.<br />
<br />
To be able to list the snapshots for a given config for a specific user, simply change the value of {{ic|ALLOW_USERS}} in your {{ic|/etc/snapper/configs/''config''}} file. You should now be able to run {{ic|snapper -c ''config'' list}} as a normal user.<br />
<br />
Eventually, you want to be able to browse the {{ic|.snapshots}} directory with a user, but the owner of this directory must stay root. Therefore, you should change the group owner by a group containing the user you are interested in, such as {{ic|users}} for example:<br />
<br />
# chmod a+rx .snapshots<br />
# chown :users .snapshots<br />
<br />
== Tips and tricks ==<br />
<br />
=== Wrapping pacman transactions in snapshots===<br />
<br />
There are a couple of packages used for automatically creating snapshots upon a pacman transaction:<br />
<br />
* {{App|pacupg|"Script that wraps package and AUR upgrades in btrfs snapshots and provides an easy way to roll them back."|https://github.com/crossroads1112/bin/tree/master/pacupg|{{AUR|pacupg}}}}<br />
* {{App|snap-pac|"Makes pacman automatically use snapper to create [[#Pre/post snapshots]] like openSUSE's YaST". Uses [[Pacman#Hooks]].|https://github.com/wesbarnett/snap-pac|{{pkg|snap-pac}}}}<br />
* {{App|snap-pac-grub|"Additionally updates GRUB entries for {{Pkg|grub-btrfs}} after {{pkg|snap-pac}} made the snapshots. Also uses [[Pacman#Hooks]].|{{AUR|snap-pac-grub}}|{{aur|snap-pac-grub}}}}<br />
* {{App|snp|wrap any shell command in a snapper pre-post snapshot, e.g. {{ic|$ snp pacman -Syu}}|https://gist.github.com/erikw/5229436|}}<br />
<br />
==== Backup non-btrfs boot partition on pacman transactions ====<br />
<br />
If your {{ic|/boot}} partition is on a non btrfs filesystem (e.g. an [[ESP]]) you are not able to do snapper backups with it. You can copy the boot partition automatically on a kernel update to your btrfs root with a hook. This also plays nice together with {{Pkg|snap-pac}}.<br />
{{hc|1=/usr/share/libalpm/hooks/50_bootbackup.hook|2=<br />
[Trigger]<br />
Operation = Upgrade<br />
Operation = Install<br />
Operation = Remove<br />
Type = Package<br />
Target = linux*<br />
<br />
[Action]<br />
Depends = rsync<br />
Description = Backing up /boot...<br />
When = PreTransaction<br />
Exec = /usr/bin/rsync -a --delete /boot /.bootbackup<br />
}}<br />
<br />
=== Incremental backup to external drive ===<br />
<br />
The following packages use {{ic|btrfs send}} and {{ic|btrfs receive}} to send backups incrementally to an external drive. Refer to their documenation to see differences in implementation, features, and requirements.<br />
<br />
* {{App|buttersink|"Buttersink is like rsync for btrfs snapshots."|https://github.com/AmesCornish/buttersink.git|{{AUR|buttersink-git}}}}<br />
* {{App|snap-sync|"Use snapper snapshots to backup to external drive."|https://github.com/wesbarnett/snap-sync.git|{{Pkg|snap-sync}}}}<br />
* {{App|snapsync|"A synchronization tool for snapper"|https://github.com/doudou/snapsync|{{AUR|ruby-snapsync}}}}<br />
<br />
===Suggested filesystem layout===<br />
<br />
{{Note|1=The following layout is intended ''not'' to be used with {{ic|snapper rollback}}, but is intended to mitigate inherent problems with restoring {{ic|/}} with that command. See [https://bbs.archlinux.org/viewtopic.php?id=194491 this forum thread].}}<br />
<br />
Here is a suggested file system layout for easily restoring your {{ic|/}} to a previous snapshot:<br />
<br />
subvolid=5<br />
|<br />
├── @<br />
| |<br />
| ├── /usr<br />
| |<br />
| ├── /bin<br />
| |<br />
| ├── /.snapshots<br />
| |<br />
| ├── ...<br />
|<br />
├── @snapshots<br />
|<br />
└── @...<br />
<br />
Where {{ic|/.snapshots}} is a mountpoint for {{ic|@snapshots}}. {{ic|@...}} are subvolumes that you want to keep separate from the subvolume you will be mounting as {{ic|/}} ({{ic|@}}). When taking a snapshot of {{ic|/}}, these other subvolumes are not included. However, you can still snapshot these other subvolumes separately by creating other snapper configurations for them. Additionally, if you were to restore your system to a previous snapshots of {{ic|/}}, these other subvolumes will remain unaffected.<br />
<br />
For example if you want to be able restore {{ic|/}} to a previous snapshot but keep your {{ic|/home}} intact, you should create a subvolume that will be mounted at {{ic|/home}}. See [[Btrfs#Mounting subvolumes]].<br />
<br />
This layout allows the snapper utility to take regular snapshots of {{ic|/}}, while at the same time making it easy to restore {{ic|/}} from an Arch Live CD if it becomes unbootable.<br />
<br />
In this sceneario, after the initial setup, snapper needs no changes, and will work as expected.<br />
<br />
{{Note|Even if a subvolume is nested below {{ic|@}}, a snapshot of {{ic|/}} will ''not'' include it. Be sure to set up snapper for any additional subvolumes you want to keep snapshots of besides the one mounted at {{ic|/}}.}}<br />
<br />
====Configuration of snapper and mount point====<br />
<br />
Make sure {{ic|/.snapshots}} is ''not'' mounted and does ''not'' exist as folder.<br />
# umount /.snapshots<br />
# rm -r /.snapshots<br />
<br />
Then [[#Create a new configuration]] for {{ic|/}}.<br />
<br />
Now [[mount]] {{ic|@snapshots}} to {{ic|/.snapshots}}.<br />
For example, for a file system located on {{ic|/dev/sda1}}:<br />
# mount -o subvol=@snapshots /dev/sda1 /.snapshots<br />
To make this mount permanent, add an entry to your [[fstab]].<br />
<br />
Or if you have an existing fstab entry remount the snapshot subvolume:<br />
# mount -a<br />
<br />
Give the folder {{ic|750}} [[Permissions#Numeric_method|permissions]].<br />
<br />
This will make all snapshots that snapper creates be stored outside of the {{ic|@}} subvolume, so that {{ic|@}} can easily be replaced anytime without losing the snapper snapshots.<br />
<br />
====Restoring {{ic|/}} to a previous snapshot of {{ic|@}} ====<br />
<br />
If you ever want to restore {{ic|/}} using one of snapper's snapshots, first boot into a live Arch Linux USB/CD.<br />
<br />
[[Mount]] the toplevel subvolume (subvolid=5). That is, omit any {{ic|subvolid}} mount flags.<br />
<br />
Find the snapshot you want to recover in {{ic|/mnt/@snapshots/*/info.xml}}.<br />
{{Tip| You can use {{ic|vi}} to easily browse through each file:<br />
# vi /mnt/@snapshots/*/info.xml<br />
Use {{ic|:n}} to see the next file and {{ic|:rew}} to go back to the first file.}}<br />
<br />
Browse through the {{ic|<description>}} tags and the {{ic|<date>}} tags, and when you find the snapshot you wish to restore, remember the {{ic|<num>}} number.<br />
<br />
Now, move {{ic|@}} to another location (''e.g.'' {{ic|/@.broken}}) to save a copy of the current system. Alternatively, simply delete {{ic|@}} using {{ic|btrfs subvolume delete}}.<br />
<br />
Create a read-write snapshot of the read-only snapshot snapper took:<br />
<br />
# btrfs subvol snapshot /mnt/@snapshots/''#''/snapshot /mnt/@<br />
<br />
Where {{ic|''#''}} is the number of the snapper snapshot you wish to restore. Your {{ic|/}} has now been restore to the previous snapshot. Now just simply reboot.<br />
<br />
===Deleting files from snapshots===<br />
<br />
If you want to delete a specific file or folder from past snapshots without deleting the snapshots themselves, [https://pypi.python.org/pypi/snapperS snapperS] is a script that adds this functionality to Snapper. This script can also be used to manipulate past snapshots in a number of other ways that Snapper does not currently support.<br />
<br />
If you want to remove a file without using an extra script, you just need to [http://unix.stackexchange.com/a/149933/3587 make your snapshot subvolume read-write], which you can do with:<br />
<br />
# btrfs property set /path/to/.snapshots/<snapshot_num>/snapshot ro false<br />
<br />
verify that <tt>ro=false</tt>:<br />
<br />
# btrfs property get /path/to/.snapshots/<snapshot_num>/snapshot<br />
ro=false<br />
<br />
You can now modify files in <tt>/path/to/.snapshots/<snapshot_num>/snapshot</tt> like normal. You can use a shell loop to work on your snapshots in bulk.<br />
<br />
=== Preventing slowdowns ===<br />
<br />
Keeping many of snapshots for a large timeframe on a busy filesystem like {{ic|/}}, where many system updates happen over time, can cause serious slowdowns. You can prevent it by:<br />
* [[Btrfs#Creating_a_subvolume|Creating]] subvolumes for things that are not worth being snapshotted, like {{ic|/var/cache/pacman/pkg}}, {{ic|/var/abs}}, {{ic|/var/tmp}}, and {{ic|/srv}}.<br />
* Editing the default settings for hourly/daily/monthly/yearly snapshots when using [[#Automatic timeline snapshots]].<br />
<br />
==== updatedb ====<br />
<br />
By default, {{ic|updatedb}} will also index the {{ic|.snapshots}} directory created by snapper, which can cause serious slowdown and excessive memory usage if you have many snapshots. You can prevent {{ic|updatedb}} from indexing over it by editing:<br />
{{hc|/etc/updatedb.conf|2=PRUNENAMES = ".snapshots"}}<br />
<br />
=== Preserving log files ===<br />
<br />
It's recommended to create a subvolume for {{ic|/var/log}} so that snapshots of {{ic|/}} exclude it. That way if a snapshot of {{ic|/}} is restored your log files will not also be reverted to the previous state. This make it easier to troubleshoot.<br />
<br />
=== Cleanup based on disk usage ===<br />
<br />
{{Expansion|See [http://snapper.io/2016/05/18/space-aware-cleanup.html] for ideas.}}<br />
<br />
== Troubleshooting ==<br />
===Snapper logs===<br />
Snapper writes all activity to {{ic|/var/log/snapper.log}} - check this file first if you think something goes wrong.<br />
<br />
If you have issues with hourly/daily/weekly snapshots, the most common cause for this so far has been that the cronie service (or whatever cron daemon you are using) was not running.<br />
<br />
===IO error===<br />
If you get an 'IO Error' when trying to create a snapshot please make sure that the [https://bbs.archlinux.org/viewtopic.php?id=164404 .snapshots] directory associated to the subvolume you are trying to snapshot is a subvolume by itself.<br />
<br />
Another possible cause is that .snapshots directory does not have root as an owner (You will find {{ic|Btrfs.cc(openInfosDir):219 - .snapshots must have owner root}} in the {{ic|/var/log/snapper.log}}).<br />
<br />
== See also ==<br />
* [http://snapper.io/ Snapper homepage]<br />
* [https://en.opensuse.org/Portal:Snapper openSUSE Snapper portal]<br />
* [https://btrfs.wiki.kernel.org/index.php/Main_Page Btrfs homepage]<br />
* [https://www.linux.com/news/enterprise/systems-management/878490-snapper-suses-ultimate-btrfs-snapshot-manager/ Linux.com: Snapper: SUSE's Ultimate Btrfs Snapshot Manager]</div>Coldiron