App-Yabsm

 view release on metacpan or  search on metacpan

lib/App/Yabsm/Backup/Generic.pm  view on Meta::CPAN

    }
    elsif ($backup_type eq 'ssh') {
        local_backup_exists_or_die($backup, $config_ref);
    }
    else { is_backup_type_or_die($backup_type) }

    my $tmp_snapshot_dir = yabsm_dir($config_ref) . "/.yabsm-var/${backup_type}_backups/$backup/tmp-snapshot/$tframe";

    if ($die_unless_exists{DIE_UNLESS_EXISTS}) {
        unless (-d $tmp_snapshot_dir && -r $tmp_snapshot_dir) {
            my $username = getpwuid $<;
            die "yabsm: error: no directory '$tmp_snapshot_dir' that is readable by user '$username'. This directory should have been initialized when the daemon started.\n";
        }
    }

    return $tmp_snapshot_dir;
}

sub take_bootstrap_snapshot {

    # Take a btrfs bootstrap snapshot of $backup and return its path.

lib/App/Yabsm/Backup/Generic.pm  view on Meta::CPAN

        ssh_backup_exists_or_die($backup, $config_ref);
    }
    if ($backup_type eq 'local') {
        local_backup_exists_or_die($backup, $config_ref);
    }

    my $bootstrap_dir = yabsm_dir($config_ref) . "/.yabsm-var/${backup_type}_backups/$backup/bootstrap-snapshot";

    if ($or_die{DIE_UNLESS_EXISTS}) {
        unless (-d $bootstrap_dir && -r $bootstrap_dir) {
            my  $username = getpwuid $<;
            die "yabsm: error: no directory '$bootstrap_dir' that is readable by user '$username'. This directory should have been initialized when the daemon started.\n";
        }
    }

    return $bootstrap_dir;
}

sub the_local_bootstrap_snapshot {

    # Return the local bootstrap snapshot for $backup if it exists and return

lib/App/Yabsm/Backup/Local.pm  view on Meta::CPAN


    # We can't perform a backup if the bootstrap process is currently being
    # performed.
    if (bootstrap_lock_file($local_backup, 'local', $config_ref)) {
        return undef;
    }

    my $backup_dir = local_backup_dir($local_backup, $tframe, $config_ref);

    unless (is_btrfs_dir($backup_dir) && -r $backup_dir) {
        my $username = getpwuid $<;
        die "yabsm: error: '$backup_dir' is not a directory residing on a btrfs filesystem that is readable by user '$username'\n";
    }

    my $tmp_snapshot       = take_tmp_snapshot($local_backup, 'local', $tframe, $config_ref);
    my $bootstrap_snapshot = maybe_do_local_backup_bootstrap($local_backup, $config_ref);

    system_or_die("sudo -n btrfs send -p '$bootstrap_snapshot' '$tmp_snapshot' | sudo -n btrfs receive '$backup_dir' >/dev/null 2>&1");

    # @backups is sorted from newest to oldest
    my @backups = sort_snapshots(do {

lib/App/Yabsm/Backup/Local.pm  view on Meta::CPAN

    # return undef otherwise. Die if we find multiple bootstrap snapshots.

    arg_count_or_die(2, 2, @_);

    my $local_backup = shift;
    my $config_ref   = shift;

    my $backup_dir_base = local_backup_dir($local_backup, undef, $config_ref);

    unless (-d $backup_dir_base && -r $backup_dir_base) {
        my $username = getpwuid $<;
        die "yabsm: error: no directory '$backup_dir_base' that is readable by user '$username'\n";
    }

    opendir my $dh, $backup_dir_base or confess("yabsm: internal error: cannot opendir '$backup_dir_base'");
    my @boot_snaps = grep { is_snapshot_name($_, ONLY_BOOTSTRAP => 1) } readdir($dh);
    closedir $dh;

    map { $_ = "$backup_dir_base/$_" } @boot_snaps;

    if (0 == @boot_snaps) {

lib/App/Yabsm/Backup/SSH.pm  view on Meta::CPAN

sub new_ssh_conn {

    # Return a Net::OpenSSH connection object to $ssh_backup's ssh destination or
    # die if a connection cannot be established.

    arg_count_or_die(2, 2, @_);

    my $ssh_backup = shift;
    my $config_ref = shift;

    my $home_dir = (getpwuid $<)[7]
      or die q(yabsm: error: user ').scalar(getpwuid $<).q(' does not have a home directory to hold SSH keys);

    my $pub_key  = "$home_dir/.ssh/id_ed25519.pub";
    my $priv_key = "$home_dir/.ssh/id_ed25519";

    unless (-f $pub_key) {
        my $username = getpwuid $<;
        die "yabsm: error: cannot not find '$username' users SSH public SSH key '$pub_key'\n";
    }

    unless (-f $priv_key) {
        my $username = getpwuid $<;
        die "yabsm: error: cannot not find '$username' users private SSH key '$priv_key'\n";
    }

    my $ssh_dest = ssh_backup_ssh_dest($ssh_backup, $config_ref);

    my $ssh = Net::OpenSSH->new(
        $ssh_dest,
        master_opts  => [ '-q' ], # quiet
        batch_mode   => 1, # Key based auth only
        ctl_dir      => '/tmp',

lib/App/Yabsm/Command/Daemon.pm  view on Meta::CPAN


    my $force      = shift;
    my $config_ref = shift;

    if ($force || all_ssh_backups($config_ref)) {

        my $yabsm_uid = getpwnam('yabsm') or confess(q(yabsm: internal error: cannot find user named 'yabsm'));
        my $yabsm_gid = getgrnam('yabsm') or confess(q(yabsm: internal error: cannot find group named 'yabsm'));

        unless (POSIX::getuid() == $yabsm_uid && POSIX::getgid() == $yabsm_gid) {
            my $username  = getpwuid POSIX::getuid();
            my $groupname = getgrgid POSIX::getgid();
            confess "yabsm: internal error: expected to be running as user and group yabsm but instead running as user '$username' and group '$groupname'";
        }

        my $yabsm_user_home = yabsm_user_home($config_ref);

        my $ssh_dir  = "$yabsm_user_home/.ssh";
        my $priv_key = "$ssh_dir/id_ed25519";
        my $pub_key  = "$ssh_dir/id_ed25519.pub";

lib/App/Yabsm/Tools.pm  view on Meta::CPAN

    return 0+(0 == system('sudo -n btrfs --help >/dev/null 2>&1'));
}

sub have_sudo_access_to_btrfs_or_die {

    # Wrapper around have_sudo_access_to_btrfs() that Carp::Confess's if it
    # returns false.

    arg_count_or_die(0, 0, @_);

    my $username = getpwuid $<;

    have_sudo_access_to_btrfs() ? return 1 : die("yabsm: internal error: no sudo access rights to 'btrfs' while running as user '$username'");
}

sub is_btrfs_dir {

    # Return 1 if $dir is a directory residing on a btrfs subvolume
    # and return 0 otherwise.

    arg_count_or_die(1, 1, @_);

lib/App/Yabsm/Tools.pm  view on Meta::CPAN

    until (-d $dir) {
        $dir = dirname($dir);
    }

    my ($uid, $gid) = (stat $dir)[4,5];

    -d $path and return 1;

    make_path($path, {uid => $uid, group => $gid}) and return 1;

    my $username = getpwuid $<;

    die "yabsm: error: could not create path '$path' while running as user '$username'\n";
}

sub i_am_root {

    # Return 1 if current user is root and return 0 otherwise.

    return 0+(0 == $<);
}

sub i_am_root_or_die {

    # Die unless running as the root user.

    arg_count_or_die(0, 0, @_);

    unless (i_am_root()) {
        my $username = getpwuid $<;
        confess("yabsm: internal error: not running as root - running as '$username'");
    }

    return 1;
}

1;



( run in 0.391 second using v1.01-cache-2.11-cpan-8d75d55dd25 )