Skip to content

Commit

Permalink
Added backup directory to SshConnection (#16)
Browse files Browse the repository at this point in the history
  • Loading branch information
chughes741 authored Aug 4, 2023
1 parent 5d763c1 commit f2c54bc
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 5 deletions.
1 change: 1 addition & 0 deletions openct/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
config.identity.username,
config.settings.connection_timeout,
config.identity.key_file,
config.dirs.backup_dir,
)

if device.is_available():
Expand Down
12 changes: 10 additions & 2 deletions openct/connections/connections.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,18 @@ class SshConnection(DeviceConnection):
"""DeviceConnection using Fabric"""

def __init__(
self, ip_address: str, username: str, connection_timeout: int, key_file: str
self,
ip_address: str,
username: str,
connection_timeout: int,
key_file: str,
backup_dir: str,
) -> None:
self.ip_address = ip_address
self.username = username
self.connection_timeout = connection_timeout
self.key_file = key_file
self.backup_dir = backup_dir

def test_connection(self) -> bool:
with FabricConnection(
Expand All @@ -48,5 +54,7 @@ def fetch_backup(self) -> None:
connect_kwargs={"key_filename": self.key_file},
) as connection:
connection.run("/export file=backup", hide=True, warn=False)
connection.get("backup.rsc", f"backups/backup_{self.ip_address}.rsc")
connection.get(
"backup.rsc", f"{self.backup_dir}/backup_{self.ip_address}.rsc"
)
connection.run("file/remove backup.rsc", hide=True, warn=False)
12 changes: 10 additions & 2 deletions openct/devices/devices.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

from openct.connections import DeviceConnection, SshConnection


class Device(ABC):
"""Device abstract base class"""

Expand All @@ -22,10 +23,17 @@ class RouterOSDevice(Device):
"""RouterOS device"""

def __init__(
self, ip_address: str, username: str, connection_timeout: int, key_file: str
self,
ip_address: str,
username: str,
connection_timeout: int,
key_file: str,
backup_dir: str,
) -> None:
super().__init__(
SshConnection(ip_address, username, connection_timeout, key_file)
SshConnection(
ip_address, username, connection_timeout, key_file, backup_dir
)
)

def fetch_backup(self) -> str:
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "openct"
version = "0.1.1"
version = "0.1.2"
description = "Configuration backup and analysis tools for devices running pfSense and RouterOS"
authors = ["Weehooey <[email protected]>"]
license = "GNU GPL v3.0"
Expand Down

0 comments on commit f2c54bc

Please sign in to comment.