-
Notifications
You must be signed in to change notification settings - Fork 0
/
btrfs_simple_snapshots.py
144 lines (107 loc) · 4.67 KB
/
btrfs_simple_snapshots.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
"""
Copyright (c) 2018 Arnaud Levaufre <arnaud@levaufre.name>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
"""
import argparse
import datetime
import logging
import os
import subprocess
logger = logging.getLogger(__name__)
SNAP_FORMAT = "%Y-%m-%d-%H%M%S"
def get_snap_dir(subvolume):
return os.path.join(subvolume, ".snapshots")
def btrfs_create_subvolume(subvolume):
cmd = ["btrfs", "subvolume", "create", subvolume]
try:
subprocess.run(cmd, check=True)
except subprocess.CalledProcessError:
return False
return True
def btrfs_delete_subvolume(subvolume):
cmd = ["btrfs", "subvolume", "delete", subvolume]
subprocess.run(cmd)
def btrfs_snapshot(subvolume, snapshot_path):
cmd = ["btrfs", "subvolume", "snapshot", "-r", subvolume, snapshot_path]
subprocess.run(cmd)
def snapshot(subvolume):
snap_dir = get_snap_dir(subvolume)
if not os.path.exists(snap_dir):
if btrfs_create_subvolume(snap_dir):
stat = os.stat(subvolume)
os.chown(snap_dir, stat.st_uid, stat.st_gid)
snap_name = datetime.datetime.now().strftime(SNAP_FORMAT)
snap_path = os.path.join(snap_dir, snap_name)
if not os.path.exists(snap_path):
btrfs_snapshot(subvolume, snap_path)
def apply_retention_policy(subvolume, policies=None):
logger.info("Applying retention policies to %s snapshots", subvolume)
if not policies:
policies = [
(86400, '%Y%m%d%H'), # Keep an hourly snapshot for 24 hours
(2419200, '%Y%m%d'), # Keep a daily snapshot for 4 weeks
(7257600, '%Y%W'), # Keep a weekly snapshot for 12 weeks
(31536000, '%Y%m'), # Keep a monthly snaphost for 12 months
(315360000, '%Y'), # Keep a yearly snapshot 10 years
]
snap_dir = get_snap_dir(subvolume)
if os.path.isdir(snap_dir):
snaps = os.listdir(snap_dir)
else:
logger.info("%s doesn't exist so there is nothing to do", snap_dir)
return
# use a cmmon refenrece to now for every steps of the terention policy
# application.
now = datetime.datetime.now()
# Populate policy workspace used to find which snapshot must be kept or
# deleted according to the various retentio policies
policy_workspaces = {}
for snap in snaps:
snap_datetime = datetime.datetime.strptime(snap, SNAP_FORMAT)
# Populate weekly snapshots
for duration, policy in policies:
if policy not in policy_workspaces:
policy_workspaces[policy] = {}
key = snap_datetime.strftime(policy)
if key not in policy_workspaces[policy]:
policy_workspaces[policy][key] = []
if now - snap_datetime < datetime.timedelta(seconds=duration):
policy_workspaces[policy][key].append(snap)
# Find which snapshots must be kept
exclude_snap = []
for policy_workspace in policy_workspaces.values():
for workspace_snaps in policy_workspace.values():
if len(workspace_snaps):
exclude_snap.append(sorted(workspace_snaps)[0])
snaps_to_delete = filter(lambda x: x not in exclude_snap, snaps)
for snap in snaps_to_delete:
btrfs_delete_subvolume(os.path.join(snap_dir, snap))
def main():
logging.basicConfig(level="DEBUG")
parser = argparse.ArgumentParser(
description="""
Btrfs simple snapshot utility that create snapshots inside a .snapshots
subvolume inside the snapshot you want to backup.
"""
)
parser.add_argument("subvolume", nargs="+")
args = parser.parse_args()
for subvolume in args.subvolume:
snapshot(subvolume)
apply_retention_policy(subvolume)
if __name__ == "__main__":
main()