From 68164d21e50943c1d054a5f5d2f6f3c64c2a7a1c Mon Sep 17 00:00:00 2001 From: Samantha McVey Date: Wed, 28 Mar 2018 12:16:43 -0700 Subject: [PATCH 1/3] Set STDERR, STDOUT, log file and config file binmode to UTF8 In addition use three argument open for opening the log file instead of combining the mode and filename together. --- btrbk | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/btrbk b/btrbk index c48fc6a..0f5dd14 100755 --- a/btrbk +++ b/btrbk @@ -27,6 +27,8 @@ use strict; use warnings FATAL => qw( all ), NONFATAL => qw( deprecated ); +binmode STDOUT, ':encoding(UTF-8)'; +binmode STDERR, ':encoding(UTF-8)'; use Carp qw(confess); use Getopt::Long qw(GetOptions); @@ -417,7 +419,9 @@ sub init_transaction_log($$) my $file = shift; my $config_syslog_facility = shift; if(defined($file) && (not $dryrun)) { - if(open($tlog_fh, ">> $file")) { + if(open($tlog_fh, '>>', $file)) { + binmode $tlog_fh, ':encoding(UTF-8)'; + # print headers (disabled) # print_formatted("transaction", [ ], output_format => "tlog", outfile => $tlog_fh); INFO "Using transaction log: $file"; @@ -3535,6 +3539,8 @@ sub parse_config(@) INFO "Using configuration: $file"; open(FILE, '<', $file) or die $!; + binmode FILE, ':encoding(UTF-8)'; + while () { chomp; s/#.*//; # remove comments From 05df702ff92dc3aa44cd62c7e01bd72705166ca9 Mon Sep 17 00:00:00 2001 From: Samantha McVey Date: Wed, 28 Mar 2018 13:16:54 -0700 Subject: [PATCH 2/3] Use heredocs for the help message Avoids use having to use 40 print's and having to quote each line and is much easier to edit. --- btrbk | 86 ++++++++++++++++++++++++++++++----------------------------- 1 file changed, 44 insertions(+), 42 deletions(-) diff --git a/btrbk b/btrbk index 0f5dd14..d1edba2 100755 --- a/btrbk +++ b/btrbk @@ -305,48 +305,50 @@ sub VERSION_MESSAGE sub HELP_MESSAGE { - print STDERR "usage: btrbk [options] [filter...]\n"; - print STDERR "\n"; - print STDERR "options:\n"; - # "--------------------------------------------------------------------------------"; # 80 - print STDERR " -h, --help display this help message\n"; - print STDERR " --version display version information\n"; - print STDERR " -c, --config=FILE specify configuration file\n"; - print STDERR " -n, --dry-run perform a trial run with no changes made\n"; - print STDERR " -p, --preserve preserve all (do not delete anything)\n"; - print STDERR " --preserve-snapshots preserve snapshots (do not delete snapshots)\n"; - print STDERR " --preserve-backups preserve backups (do not delete backups)\n"; - print STDERR " --wipe delete all but latest snapshots\n"; - print STDERR " -v, --verbose be verbose (set loglevel=info)\n"; - print STDERR " -q, --quiet be quiet (do not print backup summary)\n"; - print STDERR " -l, --loglevel=LEVEL set logging level (warn, info, debug, trace)\n"; - print STDERR " -t, --table change output to table format\n"; - print STDERR " --format=FORMAT change output format, FORMAT=table|long|raw\n"; - print STDERR " --print-schedule print scheduler details (for the \"run\" command)\n"; - print STDERR " --progress show progress bar on send-receive operation\n"; - print STDERR "\n"; - print STDERR "commands:\n"; - print STDERR " run run snapshot and backup operations\n"; - print STDERR " dryrun don't run btrfs commands; show what would be executed\n"; - print STDERR " snapshot run snapshot operations only\n"; - print STDERR " resume run backup operations, and delete snapshots\n"; - print STDERR " prune only delete snapshots and backups\n"; - print STDERR " archive recursively copy all subvolumes (experimental)\n"; - print STDERR " clean delete incomplete (garbled) backups\n"; - print STDERR " stats print snapshot/backup statistics\n"; - print STDERR " list available subcommands are:\n"; - print STDERR " backups all backups and corresponding snapshots\n"; - print STDERR " snapshots all snapshots and corresponding backups\n"; - print STDERR " latest most recent snapshots and backups\n"; - print STDERR " config configured source/snapshot/target relations\n"; - print STDERR " source configured source/snapshot relations\n"; - print STDERR " volume configured volume sections\n"; - print STDERR " target configured targets\n"; - print STDERR " usage print filesystem usage\n"; - print STDERR " origin print origin information for subvolume\n"; - print STDERR " diff shows new files between related subvolumes\n"; - print STDERR "\n"; - print STDERR "For additional information, see $PROJECT_HOME\n"; + print STDERR <<"END_HELP"; +usage: btrbk [options] [filter...] + +options: +-------------------------------------------------------------------------------- + -h, --help display this help message + --version display version information + -c, --config=FILE specify configuration file + -n, --dry-run perform a trial run with no changes made + -p, --preserve preserve all (do not delete anything) + --preserve-snapshots preserve snapshots (do not delete snapshots) + --preserve-backups preserve backups (do not delete backups) + --wipe delete all but latest snapshots + -v, --verbose be verbose (set loglevel=info) + -q, --quiet be quiet (do not print backup summary) + -l, --loglevel=LEVEL set logging level (warn, info, debug, trace) + -t, --table change output to table format + --format=FORMAT change output format, FORMAT=table|long|raw + --print-schedule print scheduler details (for the "run" command) + --progress show progress bar on send-receive operation + +commands: + run run snapshot and backup operations + dryrun don't run btrfs commands; show what would be executed + snapshot run snapshot operations only + resume run backup operations, and delete snapshots + prune only delete snapshots and backups + archive recursively copy all subvolumes (experimental) + clean delete incomplete (garbled) backups + stats print snapshot/backup statistics + list available subcommands are: + backups all backups and corresponding snapshots + snapshots all snapshots and corresponding backups + latest most recent snapshots and backups + config configured source/snapshot/target relations + source configured source/snapshot relations + volume configured volume sections + target configured targets + usage print filesystem usage + origin print origin information for subvolume + diff shows new files between related subvolumes + +For additional information, see $PROJECT_HOME +END_HELP } From d5a27a6d17c4c0f373efd622cf8fd95250b16b89 Mon Sep 17 00:00:00 2001 From: Samantha McVey Date: Sun, 3 Jun 2018 09:16:26 -0700 Subject: [PATCH 3/3] Reformat a regex to be much more readable --- btrbk | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/btrbk b/btrbk index d1edba2..c927d87 100755 --- a/btrbk +++ b/btrbk @@ -1041,7 +1041,14 @@ sub btrfs_subvolume_list($;@) { my %node; # NOTE: btrfs-progs >= 4.13.2 pads uuid's with 36 whitespaces - unless(/^ID\s+([0-9]+)\s+gen\s+([0-9]+)\s+cgen\s+([0-9]+)\s+top level\s+([0-9]+)\s+parent_uuid\s+([0-9a-f-]+)\s+received_uuid\s+([0-9a-f-]+)\s+uuid\s+([0-9a-f-]+)\s+path\s+(.+)$/) { + unless(/^ID \s+ ([0-9]+) \s+ + gen \s+ ([0-9]+) \s+ + cgen \s+ ([0-9]+) \s+ + top\ level \s+ ([0-9]+) \s+ + parent_uuid \s+ ([0-9a-f-]+) \s+ + received_uuid \s+ ([0-9a-f-]+) \s+ + uuid \s+ ([0-9a-f-]+) \s+ + path \s+ (.+) $/x) { ERROR "Failed to parse subvolume list (unsupported btrfs-progs) for: $vol->{PRINT}"; DEBUG "Offending line: $_"; return undef;