最後更新: 2019-05-21
介紹
snapper - Linux filesystem snapshot management tool
Homepage: http://snapper.io/
Snapper never modifies the content of snapshots (Thus snapper creates read-only snapshots)
Supported filesystems:
- btrfs
- thin-provisioned lvm - ext4
CLI
* snapper
* snapperd - DBus daemon for snapper (not for direct use by the user)
目錄
- Install
- Basic Usage
- Setup snapper
- Filesystem layout
- Automatic Timeline Snapshots
- Manual snapshots
- Config
- Log
- Turning
- status & diff
- Filters
- Restore & Rollback
- Other cmd
- Doc
Install
# U16.04
apt-get install snapper
snapper --version
snapper 0.1.8
Basic Usage
snapper -c configname create-config /path/to/subvolume
* "-c" + create-config => create a configuration file at /etc/snapper/configs/configname
* based on the default template from /etc/snapper/config-templates/default
* create a subvolume at /path/to/subvolume/.snapshots
* add config to SNAPPER_CONFIGS in /etc/conf.d/snapper
P.S.
刪除 config
snapper -c configname delete-config
A snapshot's path is
/path/to/subvolume/.snapshots/#/snapshot
* where "#" is the snapshot number.
i.e.
# create a snapper config "pc_data" for btrfs subvol '/data/pc_data' by calling:
snapper -c pc_data create-config /data/pc_data
Snapshots Type
- pre # Pre snapshots should always have a corresponding post snapshot.
- post #
- single # These snapshots have no special relationship to other snapshots.
# List configurations
snapper list-configs
Config | Subvolume -------+---------- data | /data
查看 config
snapper -c CONFIG get-config
i.e.
snapper -c pc_data get-config
Key | Value -----------------------+--------------------- ALLOW_GROUPS | ALLOW_USERS | BACKGROUND_COMPARISON | yes EMPTY_PRE_POST_CLEANUP | yes EMPTY_PRE_POST_MIN_AGE | 1800 FSTYPE | btrfs NUMBER_CLEANUP | yes NUMBER_LIMIT | 50 NUMBER_LIMIT_IMPORTANT | 10 # snapshots have important=yes in the userdata. NUMBER_MIN_AGE | 1800 SUBVOLUME | /data/pc_data TIMELINE_CLEANUP | yes TIMELINE_CREATE | yes # hourly snapshots TIMELINE_LIMIT_DAILY | 10 TIMELINE_LIMIT_HOURLY | 10 TIMELINE_LIMIT_MONTHLY | 10 TIMELINE_LIMIT_YEARLY | 10 TIMELINE_MIN_AGE | 1800
修改設定
snapper -c CONFIG set-config <configdata>
i.e.
# 設定 TIMELINE backup 的數量
snapper -c pc_data set-config TIMELINE_LIMIT_DAILY=14
snapper -c cp_data set-config TIMELINE_LIMIT_HOURLY=12
#
snapper -c pc_data set-config NUMBER_LIMIT=14
Setup snapper
0. mount btrfs 到 /data
/etc/fstab
/dev/mapper/vg3t-data_disk /data btrfs noatime 0 0
1. Create btrfs Volume - pc_data
btrfs subvolume create /data/pc_data
2. Create snapper config - pc_data
snapper -c pc_data create-config /data/pc_data
* 會建立 config file: /etc/snapper/configs/pc_data
* 在 /etc/default/snapper 會加入新設定 "pc_data"
SNAPPER_CONFIGS="other .. pc_data"
btrfs subvol list /data
ID 1829 gen 3223 top level 5 path pc_data
ID 1830 gen 3224 top level 1829 path pc_data/.snapshots
Filesystem layout
tree -L 2 /data
/data ├── pc_data │ └── .snapshots ├── ... ...
每個 snapshot 都有以下結構
.snapshots/N/info.xml # type, num, date, description
.snapshots/N/snapshot # Folder
Automatic Timeline Snapshots
A cron-job creates hourly snapshots (/etc/cron.hourly/snapper)
A snapshot timeline can be created with a configurable number of snapshots kept per hour/day/month/year.
if [ "$TIMELINE_CREATE" = "yes" ] ; then snapper --config=$CONFIG --quiet create --cleanup-algorithm="timeline" --description="By CLI" fi
GLOBAL OPTIONS
- -c, --config name
Create options
- -c, --cleanup-algorithm # cleanup-algorithm
- -d, --description # description
Disable Automatic Timeline Snapshots
snapper -c pc_data set-config TIMELINE_CREATE=no
Set timeline snapshot limits
TIMELINE_MIN_AGE="1800" TIMELINE_LIMIT_HOURLY="5" # Default: "10" TIMELINE_LIMIT_DAILY="7" # Default: "10" TIMELINE_LIMIT_WEEKLY="0" # Default: "10" TIMELINE_LIMIT_MONTHLY="0" # Default: "10" TIMELINE_LIMIT_YEARLY="0" # Default: "10"
# snapshot and cleanup frequencies
# cleanup hourly snapshots after some time TIMELINE_CLEANUP="yes"
* cleanup 的發動條件:
exceeds a number specified with the TIMELINE_LIMIT_* options AND an age specified with TIMELINE_MIN_AGE
人手做一次 cleanup
# snapper cleanup <cleanup-algorithm>
# number Deletes old snapshots when a certain number of snapshots is reached
# timeline Deletes old snapshots but keeps a number of hourly, daily, monthly and yearly snapshots
# empty-pre-post Deletes pre/post snapshot pairs with empty diffs
i.e.
snapper -c pc_data cleanup timeline
/etc/cron.daily/snapper
if [ "$TIMELINE_CLEANUP" = "yes" ] ; then snapper --config=$CONFIG --quiet cleanup timeline fi
Cleanup Algorithms
- number (Deletes old snapshots when a certain number of snapshots is reached)
- timeline (Deletes old snapshots but keeps a number of hourly, daily, monthly and yearly snapshots)
- empty-pre-post
NUMBER Cleanup
# Deletes old snapshots when a certain number of snapshots is reached.
Setting:
# run daily number cleanup NUMBER_CLEANUP="yes" # limit for number cleanup NUMBER_MIN_AGE="1800" NUMBER_LIMIT="30" NUMBER_LIMIT_IMPORTANT="10"
Manual snapshots
# Create snapshots (stored permanently or until deleted)
* no special relationship to other snapshots.
snapper -c CONFIG create --description "desc"
i.e.
# -d, --description description
snapper -c pc_data create -d "test1"
# 人手建立會 rotate 的 backup (-c number)
snapper -c pc_data create -d "test2" -c number
Create options
- -d, --description # description
- -c, --cleanup-algorithm # cleanup-algorithm
# List snapshots
snapper -c CONFIG list
i.e.
snapper -c pc_data list
Type | # | Pre # | Date | User | Cleanup | Description | Userdata
-------+------+-------+-----------------------------------------+------+----------+-------------+---------
single | 0 | | | root | | current |
...
single | 1604 | | Friday, June 24, 2016 PM08:17:02 HKT | root | timeline | timeline |
single | 1605 | | Friday, June 24, 2016 PM09:17:02 HKT | root | timeline | timeline |
single | 1606 | | Sunday, July 30, 2017 PM08:34:26 HKT | root | | test1 |
single | 1607 | | Sunday, July 30, 2017 PM08:33:26 HKT | root | number | test2 |
Userdata
The userdata is a list of key-value pairs where the keys and values are strings
在 create 或 modify 時可以用 "-u, --userdata userdata" 設定它
e.g. requestid=42,user=arthur
Delete a snapshot
snapper -c CONFIG delete N
* 'N' = snapshot number
# Delete 一堆 backup "snapper -c configname delete number1-number2"
snapper -c pc_data delete 1-100000
Config
NUMBER_LIMIT=50
Defines how many snapshots the number cleanup algorithm should keep.
Deletes old snapshots when a certain number of snapshots is reached.
* 它的數量與 timeline backup 無關
NUMBER_CLEANUP=no
Defines whether the number cleanup algorithm should be run for the config
影響 NUMBER_LIMIT 及 NUMBER_LIMIT_IMPORTANT
NUMBER_MIN_AGE=1800
snapshot 的建立時間 > NUMBER_MIN_AGE 才會被 NUMBER_CLEANUP 刪除
* 不論一共有多少個 snapshot, 只要它仍新鮮(AGE<NUMBER_MIN_AGE)就不會被刪除
SYNC_ACL(Default: no)
如果 SYNC_ACL 設定為 yes
Snapper 會使用 ACL 自動允許 ALLOW_USERS 或 ALLOW_GROUPS 中的使用者/群組存取 snapshot
The user-names must be separated by spaces
ls -l
drwxr-x---+ 1 root root 400 Apr 26 22:36 .snapshots
getfacl .snapshots
Log
Snapper writes all activity to /var/log/snapper.log
Turning
updatedb
/etc/updatedb.conf
PRUNENAMES = ".git .bzr .hg .svn .snapshots"
PRUNEPATHS="/tmp /var/spool /media /home/.ecryptfs /backup"
status & diff
# Show what files and directories have been changed between snapshots (需要一定時間去 compare)
Format: old..new
# snap 21 與 snap 22 作比較, 表示由 21 返到 22 會有什麼變化
snapper -c config status 21..22
"+" means the file was created "-" means the file was deleted "c" means the content of the file has changed "t" means the type of the file has changed (e.g. from regular file to directory) "p" means the permissions are have changed "u" means the user ownership has changed "g" means the group ownership has changed "x" means the extended attribute information has changed "a" means the ACL information has changed "." means no change
* Number 0 = current
# Show the diff (difference in actual files) between snapshots
snapper -c pc_data diff 0..3 /data/pc_data/test.txt
--- /data/pc_data/test.txt 2019-05-21 10:17:08.112200121 +0800 +++ /data/pc_data/.snapshots/3/snapshot/test.txt 2019-05-21 10:13:33.443938610 +0800 @@ -1 +1 @@ -abc +2
Filters
Each line in all files /etc/snapper/filters/*.txt specifies a pattern.
ignores all files and directories matching any of those patterns by using fnmatch
Default: /etc/snapper/filters/base.txt
/etc/adjtime /etc/mtab /var/lib/logrotate.status /var/lib/misc/random-seed /var/lib/ntp/drift/ntp.drift
Restore & Rollback
undochange
Undo changes done between snapshot number1 and number2.
undochange [options] number1..number2 [files]
-i, --input file # Read files for which to undo changes from file file.
Rollback
snapper rollback [number]
Other cmd
snapper mount & snapper umount
only useful when snapper is used with thin-provisioned LVM snapshots.
Theese commands will mount and unmount LVM snapshots in the .snapshots/$number/snapshot directory.
Since btrfs subvolumes are always accessible form the .snapshots directory without having to be mounted,
snapper mount and snapper umount don't do anything with btrfs.
Doc
- man snapper-configs