Skip to content

Commit

Permalink
back up media files with netbox backup
Browse files Browse the repository at this point in the history
  • Loading branch information
mmguero committed Jan 10, 2023
1 parent 32365e3 commit faf1e40
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
2 changes: 1 addition & 1 deletion docs/netbox.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ The NetBox database may be backed up and restored using `./scripts/netbox-backup

```
$ ./scripts/netbox-backup
NetBox configuration database saved to malcolm_netbox_backup_20230110-125620.psql.gz
NetBox configuration database saved to ('malcolm_netbox_backup_20230110-133855.psql.gz', 'malcolm_netbox_backup_20230110-133855.psql.media.tar.gz')
```

To clear the existing NetBox database and restore a previous backup, run the following command (substituting the filename of the `netbox_….psql.gz` you wish to restore) from within the Malcolm installation directory while Malcolm is running:
Expand Down
17 changes: 16 additions & 1 deletion scripts/control.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import stat
import string
import sys
import tarfile
import time

from malcolm_common import *
Expand Down Expand Up @@ -307,7 +308,12 @@ def netboxBackup(backupFileName=None):
with gzip.GzipFile(backupFileName, "wb") as f:
f.write(bytes('\n'.join(results), 'utf-8'))

return backupFileName
backupFileParts = os.path.splitext(backupFileName)
backupMediaFileName = backupFileParts[0] + ".media.tar.gz"
with tarfile.open(backupMediaFileName, 'w:gz') as t:
t.add(os.path.join(os.path.join(MalcolmPath, 'netbox'), 'media'), arcname='.')

return backupFileName, backupMediaFileName


###################################################################################################
Expand Down Expand Up @@ -360,6 +366,15 @@ def netboxRestore(backupFileName=None):
if (err != 0) or (len(results) == 0):
raise Exception(f'Error performing NetBox migration')

# restore media directory
backupFileParts = os.path.splitext(backupFileName)
backupMediaFileName = backupFileParts[0] + ".media.tar.gz"
mediaPath = os.path.join(os.path.join(MalcolmPath, 'netbox'), 'media')
if os.path.isfile(backupMediaFileName) and os.path.isdir(mediaPath):
RemoveEmptyFolders(mediaPath, removeRoot=False)
with tarfile.open(backupMediaFileName) as t:
t.extractall(mediaPath)


###################################################################################################
def logs():
Expand Down

0 comments on commit faf1e40

Please sign in to comment.