From dddfdddb763cc5f70e13cbbfaf8a5fac5f3bef7b Mon Sep 17 00:00:00 2001 From: 0x3639 <0x3639@protonmail.com> Date: Sun, 15 Dec 2024 16:27:16 -0600 Subject: [PATCH 1/5] add local backup and restore --- backup.sh | 162 ++++++++++++++++++++++++++++++++++++++++++++++++++++ go-zenon.sh | 60 ++++++++++++++----- 2 files changed, 208 insertions(+), 14 deletions(-) create mode 100644 backup.sh diff --git a/backup.sh b/backup.sh new file mode 100644 index 0000000..d9e68d8 --- /dev/null +++ b/backup.sh @@ -0,0 +1,162 @@ +#!/bin/bash + +# Function to print usage instructions +usage() { + echo "Usage: $0 --backup|--restore" + exit 1 +} + +# Function to perform backup +do_backup() { + # Ensure zip is installed + if ! command -v zip &> /dev/null; then + echo "zip is not installed. Attempting to install zip..." + if command -v apt-get &> /dev/null; then + apt-get update && apt-get install -y zip + elif command -v yum &> /dev/null; then + yum install -y zip + elif command -v dnf &> /dev/null; then + dnf install -y zip + else + echo "Could not install zip. No supported package manager found (apt-get/yum/dnf)." + exit 1 + fi + fi + + # Define date and file variables + current_datetime=$(date +"%Y%m%d%H%M%S") + zip_file_name="bootstrap-$current_datetime.zip" + hash_file_name="bootstrap-$current_datetime.hash" + + # Stop the service + systemctl stop go-zenon + + # Sleep 10 seconds to ensure service has stopped + sleep 10 + + # Navigate to the directory + cd /root/.znn + + # Create backup directory if it doesn't exist + mkdir -p /root/.znn/backup + backup_dir="/root/.znn/backup/$current_datetime" + mkdir -p "$backup_dir" + + # Copy necessary folders to backup directory + cp -r nom/ "$backup_dir/nom.bak" + cp -r network/ "$backup_dir/network.bak" + cp -r consensus/ "$backup_dir/consensus.bak" + + # Check if cache directory exists and copy if it does + if [ -d "cache/" ]; then + cp -r cache/ "$backup_dir/cache.bak" + fi + + # Start the service + systemctl start go-zenon + + # Create the zip and hash files + cd "$backup_dir" + zip -r "$zip_file_name" ./*.bak + echo "$(sha256sum "$zip_file_name" | awk '{ print $1 }')" > "$hash_file_name" + + # Remove the .bak directories + rm -rf nom.bak network.bak consensus.bak + if [ -d "cache.bak" ]; then + rm -rf cache.bak + fi + + echo "Backup completed at $backup_dir" + echo "Cleanup completed!" +} + +# Function to perform restore +do_restore() { + # Check if backup directory exists + if [ ! -d "/root/.znn/backup" ]; then + echo "No backup directory found at /root/.znn/backup" + exit 1 + fi + + # List available backups and let user choose + echo "Available backups:" + backup_dates=($(ls -1 /root/.znn/backup)) + if [ ${#backup_dates[@]} -eq 0 ]; then + echo "No backups found" + exit 1 + fi + + for i in "${!backup_dates[@]}"; do + echo "$((i+1)): ${backup_dates[$i]}" + done + + read -p "Select backup to restore (1-${#backup_dates[@]}): " selection + + # Validate selection + if ! [[ "$selection" =~ ^[0-9]+$ ]] || [ "$selection" -lt 1 ] || [ "$selection" -gt ${#backup_dates[@]} ]; then + echo "Invalid selection" + exit 1 + fi + + selected_date=${backup_dates[$((selection-1))]} + backup_path="/root/.znn/backup/$selected_date" + current_datetime=$(date +"%Y%m%d%H%M%S") + archive_dir="/root/.znn/archive/$current_datetime" + + # Stop the service + systemctl stop go-zenon + + # Sleep 10 seconds to ensure service has stopped + sleep 10 + + # Create archive directory + mkdir -p "$archive_dir" + + # Move current directories to archive with timestamp + for dir in nom network consensus cache; do + if [ -d "/root/.znn/$dir" ]; then + mv "/root/.znn/$dir" "$archive_dir/${dir}.bak-$current_datetime" + fi + done + + # Unzip the backup + cd "$backup_path" + unzip "bootstrap-$selected_date.zip" + + # Move restored files to main directory + for dir in nom.bak network.bak consensus.bak cache.bak; do + if [ -d "$dir" ]; then + mv "$dir" "/root/.znn/${dir%.bak}" + fi + done + + # Start the service + systemctl start go-zenon + + echo "Restore completed successfully!" + echo "Previous files archived in: $archive_dir" +} + +# Check arguments +if [ "$#" -ne 1 ]; then + usage +fi + +# Make sure to run with root permissions +if [[ $EUID -ne 0 ]]; then + echo "This script must be run as root" + exit 1 +fi + +# Process commands +case "$1" in + --backup) + do_backup + ;; + --restore) + do_restore + ;; + *) + usage + ;; +esac \ No newline at end of file diff --git a/go-zenon.sh b/go-zenon.sh index 3ef67a0..de550b1 100755 --- a/go-zenon.sh +++ b/go-zenon.sh @@ -238,10 +238,9 @@ deploy_go_zenon() { start_go_zenon } -# Function to restore go-zenon from bootstrap -restore_go_zenon() { - echo "Restoring go-zenon from bootstrap..." - # Download and run the restore.sh script +# Function to restore go-zenon from DO bootstrap +restore_go_zenon_do() { + echo "Restoring go-zenon from Digital Ocean bootstrap..." wget -O go-zenon_restore.sh "https://gist.githubusercontent.com/0x3639/05c6e2ba6b7f0c2a502a6bb4da6f4746/raw/ff4343433b31a6c85020c887256c0fd3e18f01d9/restore.sh" chmod +x go-zenon_restore.sh ./go-zenon_restore.sh @@ -250,6 +249,29 @@ restore_go_zenon() { rm go-zenon_restore.sh } +# Function to backup go-zenon +backup_go_zenon() { + echo "Backing up go-zenon..." + # Download and run the backup.sh script + wget -O go-zenon_backup.sh "https://raw.githubusercontent.com/zenon-network/znn-bundle/master/backup.sh" + chmod +x go-zenon_backup.sh + ./go-zenon_backup.sh --backup + + # Cleanup the temporary backup script + rm go-zenon_backup.sh +} + +# Function to restore from local backup +restore_go_zenon_local() { + echo "Restoring from local backup..." + wget -O go-zenon_backup.sh "https://raw.githubusercontent.com/zenon-network/znn-bundle/master/backup.sh" + chmod +x go-zenon_backup.sh + ./go-zenon_backup.sh --restore + + # Cleanup the temporary backup script + rm go-zenon_backup.sh +} + # Function to restart go-zenon restart_go_zenon() { echo "Restarting go-zenon..." @@ -295,13 +317,15 @@ show_help() { echo "Options:" echo " --deploy Deploy and set up the Zenon Network" echo " --buildSource [URL] Build from a specific source repository. If URL is provided, it will be used as the source." - echo " --restore Restore go-zenon from bootstrap" - echo " --restart Restart the go-zenon service" - echo " --stop Stop the go-zenon service" - echo " --start Start the go-zenon service" - echo " --status Monitor znnd logs" - echo " --grafana Install Grafana" - echo " --help Display this help message" + echo " --restore Restore go-zenon from local backup" + echo " --restoreDO Restore go-zenon from Digital Ocean bootstrap" + echo " --backup Backup go-zenon data" + echo " --restart Restart the go-zenon service" + echo " --stop Stop the go-zenon service" + echo " --start Start the go-zenon service" + echo " --status Monitor znnd logs" + echo " --grafana Install Grafana" + echo " --help Display this help message" echo } @@ -322,11 +346,19 @@ else BUILD_SOURCE_URL="$1" shift fi - deploy_go_zenon # Added this line + deploy_go_zenon exit ;; --restore ) - restore_go_zenon + restore_go_zenon_local + exit + ;; + --restoreDO ) + restore_go_zenon_do + exit + ;; + --backup ) + backup_go_zenon exit ;; --restart ) @@ -355,7 +387,7 @@ else ;; * ) echo "Invalid option: $1" - echo "Usage: $0 [--deploy] [--buildSource [URL]] [--restore] [--restart] [--stop] [--start] [--status] [--grafana] [--help]" + echo "Usage: $0 [--deploy] [--buildSource [URL]] [--restore] [--restoreDO] [--backup] [--restart] [--stop] [--start] [--status] [--grafana] [--help]" exit 1 esac done From e308a7b516becf5bbee6a72c3fe507183e69d942 Mon Sep 17 00:00:00 2001 From: 0x3639 <0x3639@protonmail.com> Date: Sun, 15 Dec 2024 16:29:45 -0600 Subject: [PATCH 2/5] update readme --- readme.md | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/readme.md b/readme.md index 1938bab..975d0d0 100644 --- a/readme.md +++ b/readme.md @@ -8,7 +8,7 @@ This script automates the setup, management, and restoration of the Zenon Networ - **Automated Zenon Deployment**: Clones the `go-zenon` repository, builds it, and sets it up as a service. - **Automated Dependencies Installation**: Installs `make`, `gcc`, and `jq` automatically without user intervention. - **Zenon Service Management**: Provides options to stop, start, and restart the `go-zenon` service. -- **Restore from Bootstrap**: Downloads and runs a script to restore the node from a bootstrap. +- **Backup and Restore**: Options to backup your node data and restore from either local backup or Digital Ocean bootstrap. - **Log Monitoring**: Allows you to monitor `znnd` logs in real-time. - **Grafana Installation**: Optionally installs Grafana for monitoring Zenon metrics. - **Non-Interactive Installations**: Automatically selects default options during package installation to avoid any prompts. @@ -28,7 +28,10 @@ sudo ./go-zenon.sh [OPTIONS] ### Options - `--deploy`: Deploy and set up the Zenon Network. -- `--restore`: Restore `go-zenon` from a bootstrap. +- `--buildSource [URL]`: Build from a specific source repository. If URL is provided, it will be used as the source. +- `--restore`: Restore go-zenon from local backup. +- `--restoreDO`: Restore go-zenon from Digital Ocean bootstrap. +- `--backup`: Backup go-zenon data. - `--restart`: Restart the `go-zenon` service. - `--stop`: Stop the `go-zenon` service. - `--start`: Start the `go-zenon` service. @@ -53,14 +56,23 @@ This will: - Build the project. - Set up and enable the `go-zenon` service. -#### Restoring from Bootstrap +#### Backup and Restore -To restore from a bootstrap, use: +To create a backup of your node: +```bash +sudo ./go-zenon.sh --backup +``` +To restore from a local backup: ```bash sudo ./go-zenon.sh --restore ``` +To restore from Digital Ocean bootstrap: +```bash +sudo ./go-zenon.sh --restoreDO +``` + #### Monitoring Logs To monitor the `znnd` logs, run: @@ -89,6 +101,7 @@ You can adjust the Go version or repository URL by modifying the following varia - Ensure you run this script as root or use `sudo` for it to function properly. - The script is designed to be non-interactive when installing dependencies, so you won't be prompted to select any options during the installation process. - Be cautious when running the script, as it will automatically update and upgrade your system packages during the `apt-get` operations. +- When using restore options, `--restore` will use your local backups while `--restoreDO` will download and restore from the Digital Ocean bootstrap. --- From bea9592f8574d2df002768189d8ceb4fd56e8886 Mon Sep 17 00:00:00 2001 From: 0x3639 <5406130+0x3639@users.noreply.github.com> Date: Sun, 15 Dec 2024 15:51:51 -0700 Subject: [PATCH 3/5] Delete readme.md Signed-off-by: 0x3639 <5406130+0x3639@users.noreply.github.com> --- readme.md | 108 ------------------------------------------------------ 1 file changed, 108 deletions(-) delete mode 100644 readme.md diff --git a/readme.md b/readme.md deleted file mode 100644 index 975d0d0..0000000 --- a/readme.md +++ /dev/null @@ -1,108 +0,0 @@ -# Zenon Network Setup Script - -This script automates the setup, management, and restoration of the Zenon Network (`go-zenon`) node. It handles dependencies installation, Go installation, Zenon deployment, and service management. The script also offers additional options for restoring from a bootstrap, monitoring logs, and installing Grafana for visualizing data. - -## Features - -- **Automated Go Installation**: Installs Go 1.23.0 (or another version if changed) based on the system architecture. -- **Automated Zenon Deployment**: Clones the `go-zenon` repository, builds it, and sets it up as a service. -- **Automated Dependencies Installation**: Installs `make`, `gcc`, and `jq` automatically without user intervention. -- **Zenon Service Management**: Provides options to stop, start, and restart the `go-zenon` service. -- **Backup and Restore**: Options to backup your node data and restore from either local backup or Digital Ocean bootstrap. -- **Log Monitoring**: Allows you to monitor `znnd` logs in real-time. -- **Grafana Installation**: Optionally installs Grafana for monitoring Zenon metrics. -- **Non-Interactive Installations**: Automatically selects default options during package installation to avoid any prompts. - -## Prerequisites - -This script assumes you're running a Linux distribution that uses `apt` as a package manager (e.g., Ubuntu or Debian). You need to have `git` installed. You must also have superuser (root) privileges to execute this script. - -## Usage - -Clone the script or save it locally, then run it using a bash terminal: - -```bash -sudo ./go-zenon.sh [OPTIONS] -``` - -### Options - -- `--deploy`: Deploy and set up the Zenon Network. -- `--buildSource [URL]`: Build from a specific source repository. If URL is provided, it will be used as the source. -- `--restore`: Restore go-zenon from local backup. -- `--restoreDO`: Restore go-zenon from Digital Ocean bootstrap. -- `--backup`: Backup go-zenon data. -- `--restart`: Restart the `go-zenon` service. -- `--stop`: Stop the `go-zenon` service. -- `--start`: Start the `go-zenon` service. -- `--status`: Monitor `znnd` logs. -- `--grafana`: Install Grafana for monitoring metrics. -- `--help`: Display the help message. - -### Example Usage - -#### Deploying Zenon Network - -To deploy and set up the Zenon Network, run: - -```bash -sudo ./go-zenon.sh --deploy -``` - -This will: -- Install required dependencies (`make`, `gcc`, `jq`). -- Download and install Go. -- Clone the `go-zenon` repository. -- Build the project. -- Set up and enable the `go-zenon` service. - -#### Backup and Restore - -To create a backup of your node: -```bash -sudo ./go-zenon.sh --backup -``` - -To restore from a local backup: -```bash -sudo ./go-zenon.sh --restore -``` - -To restore from Digital Ocean bootstrap: -```bash -sudo ./go-zenon.sh --restoreDO -``` - -#### Monitoring Logs - -To monitor the `znnd` logs, run: - -```bash -sudo ./go-zenon.sh --status -``` - -#### Installing Grafana - -To install Grafana for visualizing Zenon metrics: - -```bash -sudo ./go-zenon.sh --grafana -``` - -### Customizing the Script - -You can adjust the Go version or repository URL by modifying the following variables in the script: - -- **Go version**: Set in the variable `GO_VERSION`. The default is `1.23.0`. -- **Repository URL**: By default, it uses `https://github.com/zenon-network/go-zenon.git`. You can input a different URL when prompted, or modify the script to always use a specific repository. - -## Notes - -- Ensure you run this script as root or use `sudo` for it to function properly. -- The script is designed to be non-interactive when installing dependencies, so you won't be prompted to select any options during the installation process. -- Be cautious when running the script, as it will automatically update and upgrade your system packages during the `apt-get` operations. -- When using restore options, `--restore` will use your local backups while `--restoreDO` will download and restore from the Digital Ocean bootstrap. - ---- - -This `README.md` provides an overview of how to use the script, its features, and specific commands for deployment and service management. Let me know if you need any further adjustments! From c0396e7a84f7ca586cb0ac8d4c989025e75be27b Mon Sep 17 00:00:00 2001 From: 0x3639 <0x3639@protonmail.com> Date: Sun, 15 Dec 2024 16:57:33 -0600 Subject: [PATCH 4/5] update readme --- README.md | 35 ++++++++++++++++++++++++++++++----- 1 file changed, 30 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 1938bab..aadb15e 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # Zenon Network Setup Script -This script automates the setup, management, and restoration of the Zenon Network (`go-zenon`) node. It handles dependencies installation, Go installation, Zenon deployment, and service management. The script also offers additional options for restoring from a bootstrap, monitoring logs, and installing Grafana for visualizing data. +This script automates the setup, management, and restoration of the Zenon Network (`go-zenon`) node. It handles dependencies installation, Go installation, Zenon deployment, and service management. The script also offers additional options for backing up and restoring data, monitoring logs, and installing Grafana for visualizing data. ## Features @@ -8,7 +8,7 @@ This script automates the setup, management, and restoration of the Zenon Networ - **Automated Zenon Deployment**: Clones the `go-zenon` repository, builds it, and sets it up as a service. - **Automated Dependencies Installation**: Installs `make`, `gcc`, and `jq` automatically without user intervention. - **Zenon Service Management**: Provides options to stop, start, and restart the `go-zenon` service. -- **Restore from Bootstrap**: Downloads and runs a script to restore the node from a bootstrap. +- **Backup and Restore**: Allows you to backup your node data and restore it when needed. Backup source can be local of Digital Ocean. - **Log Monitoring**: Allows you to monitor `znnd` logs in real-time. - **Grafana Installation**: Optionally installs Grafana for monitoring Zenon metrics. - **Non-Interactive Installations**: Automatically selects default options during package installation to avoid any prompts. @@ -28,7 +28,10 @@ sudo ./go-zenon.sh [OPTIONS] ### Options - `--deploy`: Deploy and set up the Zenon Network. -- `--restore`: Restore `go-zenon` from a bootstrap. +- `--buildSource [URL]`: Build from a specific source repository. If URL is provided, it will be used as the source. +- `--backup`: Create a backup of your node data. +- `--restore`: Restore node data from a local backup. +- `--restoreDO`: Restore `go-zenon` from Digital Ocean bootstrap. - `--restart`: Restart the `go-zenon` service. - `--stop`: Stop the `go-zenon` service. - `--start`: Start the `go-zenon` service. @@ -53,14 +56,34 @@ This will: - Build the project. - Set up and enable the `go-zenon` service. -#### Restoring from Bootstrap +#### Backing Up Node Data -To restore from a bootstrap, use: +To create a backup of your node data: + +```bash +sudo ./go-zenon.sh --backup +``` + +This will create a timestamped backup of your node data in the specified backup directory. + +#### Restoring from Local Backup + +To restore your node data from a local backup: ```bash sudo ./go-zenon.sh --restore ``` +This will list available backups and allow you to select one to restore. + +#### Restoring from Digital Ocean Bootstrap + +To restore from the Digital Ocean bootstrap: + +```bash +sudo ./go-zenon.sh --restoreDO +``` + #### Monitoring Logs To monitor the `znnd` logs, run: @@ -89,6 +112,8 @@ You can adjust the Go version or repository URL by modifying the following varia - Ensure you run this script as root or use `sudo` for it to function properly. - The script is designed to be non-interactive when installing dependencies, so you won't be prompted to select any options during the installation process. - Be cautious when running the script, as it will automatically update and upgrade your system packages during the `apt-get` operations. +- Regular backups are recommended to prevent data loss. +- When restoring from a backup, the node service will be automatically stopped before restoration and started afterward. --- From 3af9fe14dbe5a03c2e4a382af0fe0e59ea61f14f Mon Sep 17 00:00:00 2001 From: 0x3639 <0x3639@protonmail.com> Date: Tue, 17 Dec 2024 03:36:11 -0700 Subject: [PATCH 5/5] fix typos --- README.md | 24 +++++++++++++++--------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index aadb15e..c076388 100644 --- a/README.md +++ b/README.md @@ -8,7 +8,7 @@ This script automates the setup, management, and restoration of the Zenon Networ - **Automated Zenon Deployment**: Clones the `go-zenon` repository, builds it, and sets it up as a service. - **Automated Dependencies Installation**: Installs `make`, `gcc`, and `jq` automatically without user intervention. - **Zenon Service Management**: Provides options to stop, start, and restart the `go-zenon` service. -- **Backup and Restore**: Allows you to backup your node data and restore it when needed. Backup source can be local of Digital Ocean. +- **Backup and Restore**: Allows you to backup your node data and restore it when needed. Backup source can be local or Digital Ocean. - **Log Monitoring**: Allows you to monitor `znnd` logs in real-time. - **Grafana Installation**: Optionally installs Grafana for monitoring Zenon metrics. - **Non-Interactive Installations**: Automatically selects default options during package installation to avoid any prompts. @@ -19,25 +19,31 @@ This script assumes you're running a Linux distribution that uses `apt` as a pac ## Usage -Clone the script or save it locally, then run it using a bash terminal: +Clone the script and make it executable +```bash +git clone https://github.com/hypercore-one/deployment.git +cd deployment +sudo chmod +x go-zenon.sh +``` +Run the script ```bash sudo ./go-zenon.sh [OPTIONS] ``` ### Options -- `--deploy`: Deploy and set up the Zenon Network. -- `--buildSource [URL]`: Build from a specific source repository. If URL is provided, it will be used as the source. -- `--backup`: Create a backup of your node data. +- `--deploy`: Deploy and set up the `go-zenon` node +- `--buildSource [URL]`: Build from a specific source repository. If URL is provided, it will be used as the source. Otherwise the script will ask for a URL during installation. +- `--backup`: Create a local backup of your node data. - `--restore`: Restore node data from a local backup. -- `--restoreDO`: Restore `go-zenon` from Digital Ocean bootstrap. +- `--restoreDO`: Restore `go-zenon` from a Digital Ocean bootstrap. - `--restart`: Restart the `go-zenon` service. - `--stop`: Stop the `go-zenon` service. - `--start`: Start the `go-zenon` service. - `--status`: Monitor `znnd` logs. - `--grafana`: Install Grafana for monitoring metrics. -- `--help`: Display the help message. +- `--help`: Display the flag options. ### Example Usage @@ -58,7 +64,7 @@ This will: #### Backing Up Node Data -To create a backup of your node data: +To create a local backup of your node data: ```bash sudo ./go-zenon.sh --backup @@ -74,7 +80,7 @@ To restore your node data from a local backup: sudo ./go-zenon.sh --restore ``` -This will list available backups and allow you to select one to restore. +This will list available local backups and allow you to select one to restore. #### Restoring from Digital Ocean Bootstrap