routeros-config-export/app.sh

63 lines
1.9 KiB
Bash
Raw Normal View History

2023-07-19 20:46:20 +02:00
#!/bin/bash
# RouterOS devices SSH connection settings
DEVICE_CONFIGS="$DEVICE_CONFIGS" # Comma-separated list of device configurations in the format: "device,username,/path/to/ssh/key"
# Git repository settings
GIT_REPO_URL="$GIT_REPO_URL" # Git repository SSH URL
GIT_USERNAME="$GIT_USERNAME" # Git username for SSH authentication
GIT_SSH_KEY="$GIT_SSH_KEY" # Git SSH private key path (mounted in Docker container)
GIT_REPO_PATH="/app/config_repo" # Directory where the Git repository is cloned in the Docker container
2023-07-19 21:41:48 +02:00
mkdir -p /app
2023-07-19 20:46:20 +02:00
# Function to export RouterOS configuration
export_routeros_config() {
local device="$1"
local username="$2"
local ssh_key="$3"
2023-07-19 21:41:48 +02:00
ssh -i "$ssh_key" "$username"@"$device" /export > "/${GIT_REPO_PATH}/${device}.txt"
}
# Function to check if the Git repository exists and pull or clone accordingly
check_and_pull_git_repo() {
if [ -d "$GIT_REPO_PATH" ]; then
cd "$GIT_REPO_PATH"
if [ -d ".git" ]; then
git pull origin master
else
echo "Error: The directory exists but is not a Git repository."
exit 1
fi
else
git clone "$GIT_REPO_URL" "$GIT_REPO_PATH"
fi
2023-07-19 20:46:20 +02:00
}
# Function to commit and push the configuration to the Git repository
commit_and_push_to_git() {
cd "$GIT_REPO_PATH"
git config --global user.email "$GIT_USERNAME"
git config --global user.name "$GIT_USERNAME"
git add .
git commit -m "Update configuration $(date +%Y-%m-%d)"
git push origin master
}
# Main script
IFS=',' read -r -a devices <<< "$DEVICE_CONFIGS"
for device_config in "${devices[@]}"; do
IFS=' ' read -r -a config <<< "$device_config"
device="${config[0]}"
username="${config[1]}"
ssh_key="${config[2]}"
export_routeros_config "$device" "$username" "$ssh_key"
done
2023-07-19 21:41:48 +02:00
check_and_pull_git_repo
2023-07-19 20:46:20 +02:00
commit_and_push_to_git