~rgrjr/rgrjr-scripts

c8d3cfaacc8052c44ca157a0e67fc5724c503f03 — Bob Rogers 3 years ago d50f03a
* backup-dbs.pl, check-passwd.pl:
   + Document these scripts.
2 files changed, 201 insertions(+), 2 deletions(-)

M backup-dbs.pl
M check-passwd.pl
M backup-dbs.pl => backup-dbs.pl +158 -2
@@ 4,7 4,6 @@
#
# [created.  -- rgr, 15-Jun-13.]
#
# $Id$

use strict;
use warnings;


@@ 14,6 13,9 @@ use Pod::Usage;
use Date::Format;

# Command-line option variables.
my $help = 0;
my $man = 0;
my $usage = 0;
my $verbose_p = 0;
my $overwrite_ok = 0;
my $time_format_string;


@@ 22,12 24,16 @@ my $backup_dir;

### Process command-line arguments.

GetOptions('verbose+' => \$verbose_p,
GetOptions('help' => \$help, 'man' => \$man, 'usage' => \$usage,
	   'verbose+' => \$verbose_p,
	   'prefix=s' => \@prefixes,
	   'overwrite!' => \$overwrite_ok,
	   'time-format=s' => \$time_format_string,
	   'backup-dir=s' => \$backup_dir)
    or pod2usage(2);
pod2usage(2) if $usage;
pod2usage(1) if $help;
pod2usage(-exitstatus => 0, -verbose => 2) if $man;

# Another security hack:  Make these files readable by the creator only.
umask(127);


@@ 154,3 160,153 @@ die "$0:  No configuration files"
for my $file (@ARGV) {
    backup_config(read_config_file($file));
}

__END__

=head1 NAME

backup-dbs.pl -- backup databases defined by configuration files

=head1 SYNOPSIS

    backup-dbs.pl [ --help ] [ --man ] [ --usage ] [ --verbose ... ]
		  [ --prefix=<string> ... ] [ --[no]overwrite ]
		  [ --time-format=<string> ... ] [ --backup-dir=<dir> ]
		  <config-file> ...

=head1 DESCRIPTION

Given one or more configuration files on the command line, extract
database connection information from each in turn, and perform a
complete database backup.  Configuration files are expected to have a
simple "keyword = value" structure, with optional comments starting
with "#" in the first column.  In order to be considered for backup,
a configuration file must have a complete set of
C<db_user>, C<db_password>, C<db_database>, and C<db_host> in order to
connect to the database.

Files are written to the C<--backup-dir=> (possibly overridden per
config file by a C<db_dump_directory> option) with file names of the
format

    <pfx>-<date>.dump

where C<< <pfx> >> may be specified by the C<--prefix> option
(possibly overridden per config file by a C<db_dump_prefix> option)
and the format of C<< <date> >> may be controlled by C<--time-format>
(possibly overridden per config file by a C<db_dump_time_format>
option).  Before creation, there is a check for duplication or
overwriting (controlled by C<--[no]overwrite>), the dump is first
written to a temporary file, and if successful, renamed to its final
destination.  If the C<--verbose> option is specified, extra progress
messages are emitted to the standard error stream; otherwise, the
operation is completely silent.

=head2 Time-limited backup series

The C<--overwrite> and C<--time-format> options can be used together
to institute a time-limited backup sequence.  For instance, specifying
just "%a" to get the day of the week abbreviation (e.g. "Tue") as the
date of the backup and allowing new backups to overwrite old ones
means that you will get only seven files (named, for example,
F<data-Sun.dump> through F<data-Sat.dump>) before the new files begin
to overwrite the oldest ones.

=head1 OPTIONS

As with all other C<Getopt::Long> scripts, option names can be
abbreviated to anything long enough to be unambiguous (e.g. C<--line-len>
or C<--lin> for C<--line-length>), options with arguments can be given as
two words (e.g. C<--line 100>) or in one word separated by an "="
(e.g. C<--line=100>), and "-" can be used instead of "--".

=over 4

=item B<--backup-dir>

Specifies the directory into which to put the backup files.  This may
be overridden by a C<db_dump_directory> option in the configuration
file, and defaults to the current directory if neither C<--backup-dir>
nor C<db_dump_directory> is specified.  C<backup-dbs.pl> dies with an
error message if the result is not a writable directory that is owned
by the current user.

=item B<--help>

Prints the L<"SYNOPSIS"> and L<"OPTIONS"> sections of this documentation.

=item B<--man>

Prints the full documentation in the Unix `manpage' style.

=item B<--no-overwrite>

=item B<--overwrite>

Specifies whether to overwrite existing backup files.  Note that this
only applies to backups made at some other time; if the file was made
earlier by this invocation of C<backup-dbs.pl> (by another
configuration file), then it dies with an error message.

=item B<--prefix>

Specifies a dump file name prefix.  The C<--prefix> option may be
specified multiple times; prefixes are consumed by configuration files
in their command-line order.  The prefix may be also be specified by a
C<db_dump_prefix> option in the configuration file (in which case a
command-line prefix is not used), and defaults to the C<db_database>
if no C<db_dump_prefix> is specified and no C<--prefix> options are
left.

=item B<--time-format>

Specifies a format string to date the backup files, which must not
contain any "/" characters.  See the C<Date::Format> module for
directives that may be included.  A good value is "%Y%m%d" which gives
the day part of the ISO date, e.g. "20210206" for 6-Feb-2021.
This may be overridden by a C<db_dump_time_format> option in the
configuration file, and defaults to "%R (which produces 24-hour times
such as 21:05) if neither C<--time-format> nor C<db_dump_time_format>
is specified.

=item B<--usage>

Prints just the L<"SYNOPSIS"> section of this documentation.

=item B<--verbose>

Prints debugging information if specified.  May be specified multiple
times to get more debugging information (but the extra information is
usually pretty obscure).

=back

=head1 SEE ALSO

=over 4

=item System backups (L<http://www.rgrjr.com/linux/backup.html>)

=back

=head1 BUGS

SECURITY BUG:  The database password is visible to the causual C<ps>
user while each database dump is running.

There should be a C<db_dump_date_format> option so that time-limited
backups could be done on a per-configuration-file basis.

There should also be a C<db_dump_overwrite_p>, for similar reasons.

=head1 AUTHOR

Bob Rogers C<E<lt> rogers@rgrjr.com E<gt>>

=head1 COPYRIGHT

Copyright (C) 2013 by Bob Rogers C<E<lt> rogers@rgrjr.com E<gt>>.
This script is free software; you may redistribute it
and/or modify it under the same terms as Perl itself.

=cut

M check-passwd.pl => check-passwd.pl +43 -0
@@ 27,3 27,46 @@ for my $id (keys(%names_from_id)) {
    print "$id is duplicated for ", join(', ', @$names), ".\n"
	if @$names > 1;
}

__END__

=head1 NAME

check-passwd.pl -- check (e.g.) /etc/passwd for duplicates

=head1 SYNOPSIS

    check-passwd.pl /etc/passwd

=head1 DESCRIPTION

Given a colon-delimited file of

	name:id:other:stuff:...

on the standard input or command line, such as C</etc/passwd> or
C</etc/group>, report duplicate names or IDs.  Do not try to supply
both C</etc/passwd> and C</etc/group> on the same invocation, or
you'll get lots of spurious duplications.

=head1 SEE ALSO

=over 4

=item L<passwd(5)>

=item L<group(5)>

=back

=head1 AUTHOR

Bob Rogers C<E<lt> rogers@rgrjr.com E<gt>>

=head1 COPYRIGHT

Copyright (C) 2018 by Bob Rogers C<E<lt> rogers@rgrjr.com E<gt>>.
This script is free software; you may redistribute it
and/or modify it under the same terms as Perl itself.

=cut