snapper (btrfs backup)

最後更新: 2019-05-21

介紹

 

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
  • Help
  • Basic Usage
  • Usage Step
  • filesystem layout
  • Automatic timeline snapshots
  • Manual snapshots
  • logs
  • Turning
  • status & diff
  • Filters

 


Install

 

# U16.04

apt-get install snapper

 


Help

 

snapper --version

snapper 0.1.8

snapper --help

 


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

A snapshot's path is

/path/to/subvolume/.snapshots/#/snapshot

 * where "#" is the snapshot number.

i.e.

# create a snapper config "pc_bak" 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.

snapper -c pc_data set-config TIMELINE_CREATE=no

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 subvolume 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)

if [ "$TIMELINE_CREATE" = "yes" ] ; then
    snapper --config=$CONFIG --quiet create --description="timeline" --cleanup-algorithm="timeline"
fi

A snapshot timeline can be created with a configurable number of snapshots kept per hour/day/month/year.

# Enable/Disable

# create hourly snapshots
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.

# 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

* no special relationship to other snapshots. (stored permanently or until deleted.)

snapper -c CONFIG create --description "desc"

i.e.

# -d, --description description

snapper -c pc_data create -d "test"

# 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 | 1    |       | Monday, April 18, 2016 PM07:17:02 HKT   | root | timeline | timeline    |
single | 294  |       | Sunday, May 01, 2016 AM12:17:02 HKT     | root | timeline | timeline    |
...
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 |          | test        |

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 delete 1-100000

 


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]