Jump to content

Docker + VM Templates: History (to revert changes), backup and restore (import / export)


Recommended Posts

If you've ever worked on a CMS like WordPress, you've got a nice feature called "Post Revisions". This feature adapted to Docker + VM Templates including a backup and restore feature would be awesome.

 

Usecases:

1. If you deploy a docker or VM, made changes, left off to do something else and came back to see it stopped working buuut you can't remember the last settings that worked. D'oh!

2. Switch between docker.img and docker directory for custom / non-apps docker / docker-compose applications.

3. To have different settings to switch between for testing / development purpose.

4. Switch / easily duplicate between UnRaid machines

 

How it could be done:

1. A service watches for filechanges over date and time in /boot/config/plugins/dockerMan/templates-user and /boot/config/plugins/dockerMan/templates

- On file creation the original template file get's a "original" tag - example: my-homeassistant~original.xml

- On file change the changed template get's a "date and time" tag - example: my-homeassistant~20230731_1125.xml (1115 = 11:25)

2. At the template setting page,

- add a select list or dropdown to choose from the different template versions for this specific / fitting docker image

- add a tick to "cross-import" a template for non-fitting docker image so all template files get showed inside the select list (especially usefull if you want to switch between different docker image sources)*

- add a download button to download all or a selected template file

- add a delete button to delete one or multiple templates / revisions*

* Add a modal that this action might not be save to do

 

Here's a quick ChatGPT generated hint how to implement the "watch for filechange" feature"

Below is an example Linux Bash script that uses the `inotifywait` command to constantly watch for file changes in the specified directories and implements the features (NOT TESTED):

```bash

#!/bin/bash

# Define the log file path
log_file="/path/to/log/file.log"

# Define the directories to watch
watch_directories=(
    "/boot/config/plugins/dockerMan/templates"
    "/boot/config/plugins/dockerMan/templates-user"
)

# Function to handle file changes
handle_file_change() {
    event=$1
    file_path=$2

    if [[ $event == "CREATE" ]]; then
        # If a file is created, create a copy with ~original in the filename
        filename=$(basename "$file_path")
        new_filename="${filename%.*}~original.${filename##*.}"
        cp "$file_path" "${file_path%/*}/$new_filename"
    elif [[ $event == "MODIFY" ]]; then
        # If a file is modified, create a copy with date and time in the filename
        filename=$(basename "$file_path")
        new_filename="${filename%.*}~$(date +'%Y%m%d_%H%M').${filename##*.}"
        cp "$file_path" "${file_path%/*}/$new_filename"
    fi
}

# Watch for file changes in the specified directories
while true; do
    for dir in "${watch_directories[@]}"; do
        inotifywait -e create,moved_to,modify -q -r "$dir" 2>/dev/null |
        while read -r event_path _ event_file; do
            file_path="$event_path$event_file"
            handle_file_change "$event" "$file_path"
        done
    done
done >> "$log_file" 2>&1

```

Save the script in a file, e.g., `file_change_watcher.sh`, and make it executable by running:

```bash
chmod +x docker_vm_file_change_watcher.sh
```

To run the script as a background process and redirect the output to the log file, execute the following command:

```bash
nohup ./file_change_watcher.sh &
```

 

The script will keep running as a background process and continuously monitor the specified directories for file changes, creating copies of files as specified in the original requirements. The output of the script will be appended to the log file specified in the script (/path/to/log/file.log). You can check the log file to see the script's updates and any potential errors:

```bash

tail -f /path/to/log/file.log

```

 

The script will keep running and monitoring the specified directories for any file changes. If a file is created, it will be copied with `~original` appended to the filename, and if a file is modified, it will be copied with the date and time appended to the filename.

Please note that the script requires the `inotifywait` command, which is usually available in the `inotify-tools` package. If you don't have it installed, you can install it using your package manager. For example, on Debian/Ubuntu, you can run:

```bash
sudo apt-get install inotify-tools
```

Edited by p0p
Link to comment
7 hours ago, p0p said:

If you've ever worked on a CMS like WordPress, you've got a nice feature called "Post Revisions". This feature adapted to Docker + VM Templates including a backup and restore feature would be awesome.

 

Usecases:

1. If you deploy a docker or VM, made changes, left off to do something else and came back to see it stopped working buuut you can't remember the last settings that worked. D'oh!

2. Switch between docker.img and docker directory for custom / non-apps docker / docker-compose applications.

3. To have different settings to switch between for testing / development purpose.

4. Switch / easily duplicate between UnRaid machines

 

How it could be done:

1. A service watches for filechanges over date and time in /boot/config/plugins/dockerMan/templates-user and /boot/config/plugins/dockerMan/templates

- On file creation the original template file get's a "original" tag - example: my-homeassistant~original.xml

- On file change the changed template get's a "date and time" tag - example: my-homeassistant~20230731_1125.xml (1115 = 11:25)

2. At the template setting page,

- add a select list or dropdown to choose from the different template versions for this specific / fitting docker image

- add a tick to "cross-import" a template for non-fitting docker image so all template files get showed inside the select list (especially usefull if you want to switch between different docker image sources)*

- add a download button to download all or a selected template file

- add a delete button to delete one or multiple templates / revisions*

* Add a modal that this action might not be save to do

 

Here's a quick ChatGPT generated hint how to implement the "watch for filechange" feature"

Below is an example Linux Bash script that uses the `inotifywait` command to constantly watch for file changes in the specified directories and implements the features (NOT TESTED):

```bash

#!/bin/bash

# Define the log file path
log_file="/path/to/log/file.log"

# Define the directories to watch
watch_directories=(
    "/boot/config/plugins/dockerMan/templates"
    "/boot/config/plugins/dockerMan/templates-user"
)

# Function to handle file changes
handle_file_change() {
    event=$1
    file_path=$2

    if [[ $event == "CREATE" ]]; then
        # If a file is created, create a copy with ~original in the filename
        filename=$(basename "$file_path")
        new_filename="${filename%.*}~original.${filename##*.}"
        cp "$file_path" "${file_path%/*}/$new_filename"
    elif [[ $event == "MODIFY" ]]; then
        # If a file is modified, create a copy with date and time in the filename
        filename=$(basename "$file_path")
        new_filename="${filename%.*}~$(date +'%Y%m%d_%H%M').${filename##*.}"
        cp "$file_path" "${file_path%/*}/$new_filename"
    fi
}

# Watch for file changes in the specified directories
while true; do
    for dir in "${watch_directories[@]}"; do
        inotifywait -e create,moved_to,modify -q -r "$dir" 2>/dev/null |
        while read -r event_path _ event_file; do
            file_path="$event_path$event_file"
            handle_file_change "$event" "$file_path"
        done
    done
done >> "$log_file" 2>&1

```

Save the script in a file, e.g., `file_change_watcher.sh`, and make it executable by running:

```bash
chmod +x docker_vm_file_change_watcher.sh
```

To run the script as a background process and redirect the output to the log file, execute the following command:

```bash
nohup ./file_change_watcher.sh &
```

 

The script will keep running as a background process and continuously monitor the specified directories for file changes, creating copies of files as specified in the original requirements. The output of the script will be appended to the log file specified in the script (/path/to/log/file.log). You can check the log file to see the script's updates and any potential errors:

```bash

tail -f /path/to/log/file.log

```

 

The script will keep running and monitoring the specified directories for any file changes. If a file is created, it will be copied with `~original` appended to the filename, and if a file is modified, it will be copied with the date and time appended to the filename.

Please note that the script requires the `inotifywait` command, which is usually available in the `inotify-tools` package. If you don't have it installed, you can install it using your package manager. For example, on Debian/Ubuntu, you can run:

```bash
sudo apt-get install inotify-tools
```

For VMs there is a planned Snapshot feature which will support this type of rollback. Maybe another solution would be to record diffs in an audit log which so users could see the changes made to templates/XMLs

Link to comment

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...