forked from skorokithakis/apt-btrfs-snapshot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
apt-btrfs-snapshot
executable file
·125 lines (109 loc) · 3.97 KB
/
apt-btrfs-snapshot
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
#!/usr/bin/python3
# Copyright (C) 2011 Canonical
#
# Author:
# Michael Vogt
#
# 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; version 3.
#
# 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
import argparse
import datetime
import logging
import os
import string
import subprocess
import sys
import tempfile
import gettext
from gettext import gettext as _
from apt_btrfs_snapshot import AptBtrfsSnapshot
if __name__ == "__main__":
gettext.bindtextdomain("apt-btrfs-snapshot")
# command line parser
description = _("Filesystem snapshot support for apt")
parser = argparse.ArgumentParser(description=description)
parser.add_argument("--debug", action="store_true", default=False,
help="enable debug output")
subparser = parser.add_subparsers(title="Commands")
# supported
command = subparser.add_parser(
"supported", help=_("Print if snapshots are supported"))
command.set_defaults(command="supported")
# list
command = subparser.add_parser(
"list", help=_("List available snapshots"))
command.set_defaults(command="list")
# snapshot
command = subparser.add_parser(
"snapshot", help=_("Create a new snapshot"))
command.set_defaults(command="snapshot")
# set-default
command = subparser.add_parser(
"set-default", help=_("Revert to previous snapshot"))
command.add_argument("snapshot")
command.set_defaults(command="set-default")
# delete
command = subparser.add_parser(
"delete", help=_("Delete snapshot"))
command.add_argument("snapshot")
command.set_defaults(command="delete")
# list-older-than
command = subparser.add_parser(
"list-older-than", help=_("Show snapshots older than N days"))
command.add_argument("time")
command.set_defaults(command="list-older-than")
# delete-older-than
command = subparser.add_parser(
"delete-older-than", help=_("Delete snapshots older than N days"))
command.add_argument("time")
command.set_defaults(command="delete-older-than")
# parse args
args = parser.parse_args()
# global options
if args.debug:
logging.basicConfig(level=logging.DEBUG)
else:
logging.basicConfig(level=logging.INFO)
if os.getuid() != 0:
print(_("Sorry, you need to be root to run this program"))
sys.exit(1)
apt_btrfs = AptBtrfsSnapshot()
if not apt_btrfs.snapshots_supported():
print(_("Sorry, your system lacks support for the snapshot feature"))
sys.exit(1)
res = False
if args.command == "supported":
res = apt_btrfs.snapshots_supported()
if res:
print(_("Supported"))
else:
print(_("Not supported"))
elif args.command == "snapshot":
res = apt_btrfs.create_btrfs_root_snapshot()
elif args.command == "list":
res = apt_btrfs.print_btrfs_root_snapshots()
elif args.command == "set-default":
res = apt_btrfs.command_set_default(args.snapshot)
elif args.command == "delete":
res = apt_btrfs.delete_snapshot(args.snapshot)
elif args.command == "list-older-than":
res = apt_btrfs.print_btrfs_root_snapshots_older_than(args.time)
elif args.command == "delete-older-than":
res = apt_btrfs.clean_btrfs_root_snapshots_older_than(args.time)
else:
print(_("ERROR: Unhandled command: '%s'") % args.command)
# return the right exit code
if res:
sys.exit(0)
else:
sys.exit(1)