Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add backup and restore #24

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 39 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
# 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

- **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.
- **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 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.
Expand All @@ -19,22 +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.
- `--restore`: Restore `go-zenon` from a bootstrap.
- `--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 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

Expand All @@ -53,14 +62,34 @@ This will:
- Build the project.
- Set up and enable the `go-zenon` service.

#### Restoring from Bootstrap
#### Backing Up Node Data

To create a local 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.

To restore from a bootstrap, use:
#### Restoring from Local Backup

To restore your node data from a local backup:

```bash
sudo ./go-zenon.sh --restore
```

This will list available local 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:
Expand Down Expand Up @@ -89,6 +118,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.

---

Expand Down
162 changes: 162 additions & 0 deletions backup.sh
Original file line number Diff line number Diff line change
@@ -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
60 changes: 46 additions & 14 deletions go-zenon.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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..."
Expand Down Expand Up @@ -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
}

Expand All @@ -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 )
Expand Down Expand Up @@ -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
Expand Down
Loading