File: listbackups.py

package info (click to toggle)
django-dbbackup 4.2.1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 512 kB
  • sloc: python: 3,767; makefile: 7
file content (73 lines) | stat: -rw-r--r-- 2,213 bytes parent folder | download | duplicates (2)
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
"""
List backups.
"""

from ... import utils
from ...storage import get_storage
from ._base import BaseDbBackupCommand, make_option

ROW_TEMPLATE = "{name:40} {datetime:20}"
FILTER_KEYS = ("encrypted", "compressed", "content_type", "database")


class Command(BaseDbBackupCommand):
    option_list = (
        make_option("-d", "--database", help="Filter by database name"),
        make_option(
            "-z",
            "--compressed",
            help="Exclude non-compressed",
            action="store_true",
            default=None,
            dest="compressed",
        ),
        make_option(
            "-Z",
            "--not-compressed",
            help="Exclude compressed",
            action="store_false",
            default=None,
            dest="compressed",
        ),
        make_option(
            "-e",
            "--encrypted",
            help="Exclude non-encrypted",
            action="store_true",
            default=None,
            dest="encrypted",
        ),
        make_option(
            "-E",
            "--not-encrypted",
            help="Exclude encrypted",
            action="store_false",
            default=None,
            dest="encrypted",
        ),
        make_option(
            "-c", "--content-type", help="Filter by content type 'db' or 'media'"
        ),
    )

    def handle(self, **options):
        self.quiet = options.get("quiet")
        self.storage = get_storage()
        files_attr = self.get_backup_attrs(options)
        if not self.quiet:
            title = ROW_TEMPLATE.format(name="Name", datetime="Datetime")
            self.stdout.write(title)
        for file_attr in files_attr:
            row = ROW_TEMPLATE.format(**file_attr)
            self.stdout.write(row)

    def get_backup_attrs(self, options):
        filters = {k: v for k, v in options.items() if k in FILTER_KEYS}
        filenames = self.storage.list_backups(**filters)
        return [
            {
                "datetime": utils.filename_to_date(filename).strftime("%x %X"),
                "name": filename,
            }
            for filename in filenames
        ]