about summary refs log tree commit diff stats
path: root/leo.raku
blob: 365e3ad7890b1539766bad0ee31a4cc653f4fae8 (plain) (blame)
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
#!/usr/bin/env raku

use v6.d;
use Config;

enum Actions (
    backup => "Make backup",
    rotate => "Rotate the backups",
    list => "List profiles / profile options",
);

sub USAGE {
    say $*USAGE;
    say "\nActions:";
    for Actions.enums.kv -> $action, $description {
        say " " x 4, $action,
        " " x (Actions.enums.keys.map(*.chars).max - $action.chars) + 4,
        $description;
    }
}

sub read-configuration(--> Config) {
    return Config.new.read(
        %*ENV<XDG_CONFIG_HOME> ??
        %*ENV<XDG_CONFIG_HOME> ~ "/leo.toml" !! %*ENV<HOME> ~ "/.config/leo.toml"
    );
}

# If nothing is passed then named argument multi prevails so we just
# create one so that USAGE() gets printed if no argument is passed.
multi sub MAIN(Bool :$help-implied) is hidden-from-USAGE { USAGE() }
multi sub MAIN(Bool :$version) is hidden-from-USAGE { say "Leo v0.6.0" }

multi sub MAIN(Actions $action, Bool :v(:$verbose)) is hidden-from-USAGE {
    note 'Verbosity on' if $verbose;
    note 'Dispatched Actions only sub' if $verbose;
    note 'Reading configuration' if $verbose;

    my Config $config = read-configuration();

    given $action.key {
        when 'list' {
            say "Profiles:";
            say " " x 4, $_ for $config<profiles>.keys.sort;
            exit 0;
        }
        default {
            note "(!!) No profile passed";
            exit 1;
        }
    }
}

multi sub MAIN(
    Actions $action, #= action to perform
    *@profiles, #= profiles to perform the action on
    Bool :v(:$verbose), #= increase verbosity
    Bool :$version, #= print version
) {
    note 'Verbosity on' if $verbose;
    note 'Reading configuration' if $verbose;

    my Config $config = read-configuration();

    # Default number of backups to hold / keep.
    my int ($default_hold, $default_keep) = (1, 2);

    # $backup_dir holds path to backup directory.
    my Str $backup_dir;
    my DateTime $date = DateTime.new(time);

    $backup_dir = $config<backup> ?? $config<backup> !! "/tmp/backups";

    # Fix $backup_dir permission.
    unless $backup_dir.IO.mode eq "0700" {
        note "Setting mode to 700: '$backup_dir'" if $verbose;
        $backup_dir.IO.chmod(0o700);
    }

    for @profiles -> $profile {
        say "[$profile]";
        without ($config<profiles>{$profile}) {
            note "(!!) No such profile";
            exit 1;
        }

        my Str $profile_dir = $backup_dir ~ "/$profile";

        # Create the profile backup directory if it doesn't exist.
        unless $profile_dir.IO ~~ :d {
            note "Creating profile backup directory: '$profile_dir'" if $verbose;
            mkdir($profile_dir, 0o700) or die "$!";
        }

        given $action.key {
            when 'backup' {
                my IO $backup_file = "$profile_dir/$date.tgz".IO;

                say "Backup: ", $backup_file.Str;

                note "\nCalling backup-profile subroutine" if $verbose;
                backup-profile($profile, $backup_file, $config, $verbose);

                if (
                    ($config<profiles>{$profile}<encrypt> // $config<gpg><encrypt>) or
                    ($config<profiles>{$profile}<sign> // $config<gpg><sign>)
                ) {
                    note "\nCalling gnupg subroutine" if $verbose;
                    gnupg($profile, $backup_file, $config, $verbose);
                }
            }
            when 'list' {
                say "Encryption ", ($config<profiles>{$profile}<encrypt> //
                                    $config<gpg><encrypt>) ??
                                                           "ON" !! "OFF";

                say "GnuPG sign ", ($config<profiles>{$profile}<sign> //
                                    $config<gpg><sign>) ??
                                                        "ON" !! "OFF";

                say "Holding ", (
                    $config<profiles>{$profile}<rotate><hold> //
                    $config<rotate><hold> // $default_hold
                ), " backups";

                say "Keeping ", (
                    $config<profiles>{$profile}<rotate><keep> //
                    $config<rotate><keep> // $default_keep
                ), " backups";

                say "Base directory: ",
                ($config<profiles>{$profile}<base_dir> //
                 $config<base_dir> //
                 "/");

                say "\nPaths: ";
                say " " x 4, $_ for @($config<profiles>{$profile}<paths> //
                                      ".");

                if $config<profiles>{$profile}<exclude>.defined {
                    say "\nExclude: ";
                    say " " x 4, $_ for @($config<profiles>{$profile}<exclude>);
                }
            }
            when 'rotate' {
                my DateTime %backups;
                for dir $profile_dir -> $path {
                    next unless $path.f;
                    if $path.Str ~~ /$profile '/' (.*)
                    ['.tar'|'.tar.gpg'|'.tgz'|'.tgz.gpg']$/ -> $match {
                        %backups{$path.Str} = DateTime.new($match[0].Str);
                    }
                }

                say "Total backups: ", %backups.elems;
                exit 0 if %backups.elems == 0; # Exit if 0 backups.

                # Hold the backups that are to be held. Default is to hold 1
                # backup.
                for %backups.sort(*.value).map(*.key).head(
                    $config<profiles>{$profile}<rotate><hold> //
                    $config<rotate><hold> // $default_hold
                ) {
                    note "(H) Holding: ", $_ if $verbose;
                    %backups{$_}:delete;
                }

                # We'll keep `n' latest backups where `n' equals to
                # the number of backups we want to keep. Default is to
                # keep 2 backups.
                for %backups.sort(*.value).map(*.key).tail(
                    $config<profiles>{$profile}<rotate><keep> //
                    $config<rotate><keep> // $default_keep
                ) {
                    note "(K) Keeping: ", $_ if $verbose;
                    %backups{$_}:delete;
                }

                # Now we just remove all backups in %backups.
                for %backups -> $backup {
                    note "(D) Deleting: ", $backup.key if $verbose;
                    unlink($backup.key);
                }
            }
            default {
                note "Invalid action";
                exit 1;
            }
        }
    }
}

sub backup-profile (
    Str $profile, IO $backup_file, Config $config, Bool $verbose
) {
    my IO $base_dir = $_.IO with (
        $config<profiles>{$profile}<base_dir> //
        $config<base_dir> //
        "/"
    );
    chdir $base_dir or die "$!";

    my Str @options = <-c -z>;
    push @options, '-vv' if $verbose;
    push @options, '-f', $backup_file.Str;
    push @options, '-C', $base_dir.Str;

    my Str @backup_paths;
    for (
        @($config<profiles>{$profile}<paths> // ".")
    ) -> $path {
        if $path.IO.d {
            push @backup_paths, $_.Str for dir $path;
        } else {
            push @backup_paths, $path;
        }
    }

    run <tar>, @options, (
        @backup_paths (-) @($config<profiles>{$profile}<exclude>)
    ).keys;
}

sub gnupg (
    Str $profile, IO $backup_file, Config $config, Bool $verbose
) {
    my Str @options = '--yes';

    if (
        $config<profiles>{$profile}<encrypt> //
        $config<gpg><encrypt>
    ) {
        push @options, '--encrypt';
        with $config<gpg><recipients> {
            push @options, "--recipient", $_ for @($_);
        }
    }

    push @options, $verbose ?? '--verbose' !! '--quiet';
    push @options, '--sign' if (
        $config<profiles>{$profile}<sign> //
        $config<gpg><sign>
    );
    push @options, '--default-key', $_ with $config<gpg><fingerprint>;

    run <gpg2>, @options, $backup_file;

    unlink($backup_file) or die "$!";
}