NAME

PDK::Device::ConfigBackup - Device Configuration Backup and Management Module

SYNOPSIS

use PDK::Device::ConfigBackup;

my $cb = PDK::Device::ConfigBackup->new();

# Retrieve the list of devices from the database
use PDK::DBI::Pg;

# Database connection parameters
my $db_params = {
  host     => '192.168.99.99',
  port     => 5432,
  dbname   => 'netdisco',
  user     => 'netdisco',
  password => 'Cisc0123'
};

# Create a database connection
my $dbi = PDK::DBI::Pg->new($db_params);

# SQL query to retrieve device information
my $sql = <<SQL;
SELECT name, ip, os
  FROM device
SQL

# Execute the query and retrieve all devices
my $devices = $dbi->execute($sql)->all;

# Fetch device configurations in parallel
$cb->getConfigJob($devices);

# Send commands to devices
$cb->execCommandsJob($devices, ['conf t', 'no ip do loo', 'end', 'wri']);

# Execute FTP backup
$cb->ftpConfigJob($devices);

DESCRIPTION

This module provides a set of methods for fetching configurations from devices, sending commands to devices, and executing FTP backups. It supports parallel processing to enhance efficiency.

ATTRIBUTES

queue

Specifies the number of concurrent tasks. Default is 10 or the value of the PDK_DEVICE_BACKUP_QUEUE environment variable.

workdir

Specifies the root directory for configuration files. Default is the user's home directory or the value of the PDK_DEVICE_BACKUP_HOME environment variable.

debug

Specifies the debug level. Default is 0 or the value of the PDK_DEVICE_BACKUP_DEBUG environment variable.

result

Stores the results of operations (getConfig, ftpConfig, execCommands) with success and fail lists.

METHODS

getConfigJob($devices)

Fetches device configurations in parallel.

ftpConfigJob($devices)

Executes FTP backup for multiple devices in parallel.

execCommandsJob($devices, $commands)

Sends the same commands to multiple devices in parallel.

runCommandsJob($devices)

Executes device-specific commands in parallel. Each device in the $devices array should have its own 'commands' key with an array of commands.

ftpConfig($param)

Performs FTP configuration backup for a single device.

getConfig($param)

Retrieves the configuration for a single device.

execCommands($param, $commands)

Executes commands on a single device.

startFtpConfig($param)

Initiates FTP configuration backup and records the result.

startExecCommands($param, $commands)

Initiates command execution and records the result.

startGetConfig($param)

Initiates configuration retrieval and records the result.

dump($msg)

Outputs debug information based on the debug level.

USAGE EXAMPLE

Here is a complete example demonstrating the usage of this module:

#!/usr/bin/perl

use 5.030;
use strict;
use warnings;

use Test::More;
use PDK::DBI::Pg;
use PDK::Device::ConfigBackup;

# Database connection parameters
my $db_params = {
  host     => '192.168.99.99',
  port     => 5432,
  dbname   => 'netdisco',
  user     => 'netdisco',
  password => 'Cisc0123'
};

# Create a database connection
my $dbi = PDK::DBI::Pg->new($db_params);

# SQL query to retrieve device information
my $sql = <<SQL;
SELECT name, ip, os
  FROM device
SQL

# Execute the query and retrieve all devices
my $devices = $dbi->execute($sql)->all;

# Initialize the configuration backup object
my $cb = PDK::Device::ConfigBackup->new(dbi => $dbi);

# Fetch device configurations in parallel
$cb->getConfigJob($devices);

# Send a set of commands to the devices
$cb->execCommandsJob($devices, ['conf t', 'no ip do loo', 'end', 'wri']);

# Execute FTP backup
$cb->ftpConfigJob($devices);

AUTHOR

WENWU YAN <968828@gmail.com>

LICENSE AND COPYRIGHT

Copyright (C) 2024 WENWU YAN

This program is free software; you can redistribute it and/or modify it under the terms of either: the GNU General Public License as published by the Free Software Foundation; or the Artistic License.

SEE ALSO

PDK::DBI::Pg, Parallel::ForkManager, Thread::Queue