Skip to content
This repository has been archived by the owner on Jun 6, 2024. It is now read-only.

Commit

Permalink
Velero (#142)
Browse files Browse the repository at this point in the history
[Features]
- Velero install script in menu
- - Open Menu `heavyscript` -> 3) Backup Options -> 4) Install/Update Velero
- - This is in preparation for Truecharts velero implementation
- Update deploy.sh to better allow for installation into non-root users directories
- Default to 500 lines for pod log function
- Add disable options for all enable features, closes #132 
- Ability to move back one time in some parts of the menu

[Improvements]
- Update Readme to show two different methods of running the one-liner script, (privileged/unprivileged) 
- Update Readme to explain cron job paths
- Print cron path out when installing HeavyScript with one liner
  • Loading branch information
Heavybullets8 authored Nov 19, 2023
1 parent 09095a5 commit e7af3a7
Show file tree
Hide file tree
Showing 15 changed files with 568 additions and 315 deletions.
91 changes: 64 additions & 27 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ If you have questions or would like to contribute, I have a sub discord category
* [Pod](#pod)
* [PVC](#pvc)
* [Self-Update](#self-update)
* [Sync](#sync)
* [Update](#update)
* [How to Install](#how-to-install)
* [How to Update](#how-to-update)
Expand Down Expand Up @@ -125,7 +126,12 @@ ___
|------------|--------------------|-------------------------------------------|
| --major | --major | Includes major updates when self-updating |

<br>

### Sync
> heavyscript sync
Syncs the catalog.

<br>

Expand Down Expand Up @@ -155,19 +161,51 @@ ___

## How to Install

### One Line Install
```
HeavyScript can be installed in two different ways depending on your needs and privileges on the system:

### Option 1: Non-Privileged Install (Regular User)

> This installation method is suitable if you don't have root access or prefer not to install HeavyScript with elevated privileges.
**Installation Command:**
```bash
curl -s https://raw.githubusercontent.com/Heavybullets8/heavy_script/main/functions/deploy.sh | bash && source "$HOME/.bashrc" 2>/dev/null && source "$HOME/.zshrc" 2>/dev/null
```

This will:
- Download HeavyScript, then place you on the latest release
- Place HeavyScript in `/root`
- Make HeavyScript executable
- Allow you to run HeavyScript from any directory with `heavyscript`
> This does not include Cron Jobs, see the Cron section for more information.
**What This Does:**
- Downloads and places HeavyScript in your user directory.
- Makes HeavyScript executable.
- Allows you to run HeavyScript from any directory using `heavyscript`.

**Note:**
- Without root privileges, the script will not create a system-wide symlink in `/usr/local/bin`.
- You might see a warning message indicating the lack of root privileges. You can proceed without root access, but you'll need to run HeavyScript with root privileges at least once to create the system-wide symlink, if required.

### Option 2: Privileged Install (Root or Sudo)

> If you have root access or can use `sudo`, this method will set up HeavyScript for all users on the system.
**Installation Command:**
```bash
curl -s https://raw.githubusercontent.com/Heavybullets8/heavy_script/main/functions/deploy.sh | sudo bash && source "$HOME/.bashrc" 2>/dev/null && source "$HOME/.zshrc" 2>/dev/null
```

**What This Does:**
- Installs HeavyScript with root privileges.
- Creates a system-wide symlink in `/usr/local/bin`, making HeavyScript accessible to all users.
- Downloads and places HeavyScript in the root directory (`/root`).
- Makes HeavyScript executable and accessible system-wide.

**Note:**
- This method requires root access or sudo privileges.
- It's recommended for environments where HeavyScript needs to be accessible to multiple users.

From here, you can just run HeavyScript with `heavyscript -ARGUMENTS`
---

### Choosing the Right Option:

- **Non-Privileged Install:** Choose this if you're more concerened with security and want to keep HeavyScript isolated to your user account, at least during the initial setup.
- **Privileged Install:** Choose this if you are less concerened about security and want to make HeavyScript accessible to all users on the system, including the root and sudo user.

<br>

Expand Down Expand Up @@ -227,34 +265,33 @@ ___

### How to Create a Cron Job

1. TrueNAS SCALE GUI
2. System Settings
3. Advanced
4. Cron Jobs
1. Click Add
To automate tasks using HeavyScript, you can create a cron job. Here's how to set it up in TrueNAS SCALE:

1. Navigate to the TrueNAS SCALE GUI.
2. Go to **System Settings** > **Advanced**.
3. Click on **Cron Jobs**.
4. Click **Add** to create a new cron job.

![image](https://user-images.githubusercontent.com/20793231/229404447-6836ff1f-ba28-439e-99fe-745371f0f24c.png)

### Important Note on the Command Path
The command for the cron job should use the full path to the `heavy_script.sh` file. This path depends on the user who installed HeavyScript. For instance, if you installed HeavyScript as a non-root user, replace `/root` with your home directory path.

- Command: `bash /root/heavy_script/heavy_script.sh update`
> The `bash`, as well as the full path to the script is required for cron jobs to work properly.
- Run as: root
> Running as root is required for the script to work properly.
- Schedule: I run mine daily at 4:00 AM
- Hide Standard Output: Unchecked
- Hide Standard Error: Unchecked
> Keep these both unchecked so you can recive an email.
> You can find your home directory path by running `echo $HOME` in the terminal.
### Cron Job Settings

<br >
- **Command:** Use the full command with the correct path, as shown in the examples above. The `bash` prefix and the full path are required for proper execution.
- **Run as:** Typically, you should run the script as the user who installed HeavyScript. If it was installed with root privileges, use `root`.
- **Schedule:** Choose the frequency and time for the script to run. For example, daily at 4:00 AM.
- **Hide Standard Output/Error:** Uncheck these options if you wish to receive email notifications about the cron job's output and errors.

### My Personal Cron Job

Here's an example of how I set up my personal cron job:

```
bash /root/heavy_script/heavy_script.sh update --backup 14 --concurrent 10 --prune --rollback --sync --self-update
```


<br >
<br >

> Remember to adjust the path in the command based on where HeavyScript is installed and the user account used for installation.
48 changes: 27 additions & 21 deletions bin/heavyscript
Original file line number Diff line number Diff line change
Expand Up @@ -4,47 +4,53 @@ set -Eeuo pipefail

find_script_dir() {
local home_dirs
home_dirs=$(getent passwd | \
cut -d: -f6 | \
sort -u | \
grep -vE '/var/|/run/|/bin|/dev|/nonexistent|/srv/tftp|usr/share/man|^/$')
mapfile -t home_dirs < <(cli -m csv -c "account user query home,builtin" | grep ",false" | grep -v "nonexistent," | awk -F ',' '{print $1}')

for home_dir in $home_dirs; do
for home_dir in "${home_dirs[@]}"; do
if [[ -d "$home_dir/heavy_script" ]]; then
echo "$home_dir/heavy_script"
return
fi
done

>&2 echo "Error: heavy_script directory not found."
exit 1
err_exit "Error: heavy_script directory not found."
}

get_user_home() {
local user_home

if [[ $EUID -eq 0 ]]; then # Script is running as root
# Use SUDO_USER if set, otherwise fall back to root's home
user_home=$(getent passwd "${SUDO_USER:-root}" | cut -d: -f6)
else # Script is run by a regular user
user_home=$HOME
fi

echo "$user_home"
}

err_exit() {
>&2 echo "$1"
exit "${2:-1}"
}

if [[ -d "/root/heavy_script" ]]; then
# Use get_user_home to determine the home directory and check for the script
user_home=$(get_user_home)
if [[ -d "$user_home/heavy_script" ]]; then
script_dir="$user_home/heavy_script"
elif [[ -d "/root/heavy_script" ]]; then
script_dir="/root/heavy_script"
elif [[ $EUID -eq 0 && -n ${SUDO_USER:-} && -d "/home/$SUDO_USER/heavy_script" ]]; then
script_dir="/home/$SUDO_USER/heavy_script"
elif [[ -d "$HOME/heavy_script" ]]; then
script_dir="$HOME/heavy_script"
else
script_dir=$(find_script_dir)
script_dir=$(find_script_dir) # Fallback to find_script_dir if not found
fi

if [[ ! -d "$script_dir" ]]; then
err_exit "Error: $script_dir does not exist."
fi

(
# Change to the script directory
if ! cd "$script_dir" ; then
err_exit "Error: Failed to change to $script_dir"
fi
# Change to the script directory and execute heavy_script.sh
if ! cd "$script_dir" ; then
err_exit "Error: Failed to change to $script_dir"
fi

# Pass all arguments '$@' to heavy_script.sh
bash ./heavy_script.sh "$@"
)
bash ./heavy_script.sh "$@"
105 changes: 74 additions & 31 deletions functions/deploy.sh
Original file line number Diff line number Diff line change
@@ -1,19 +1,26 @@
#!/bin/bash

# colors
reset='\033[0m'
red='\033[0;31m'
yellow='\033[1;33m'
green='\033[0;32m'
blue='\033[0;34m'

get_user_home() {
local user_home

if [[ $EUID -eq 0 ]]; then # Script is running as root
# Use SUDO_USER if set, otherwise fall back to root's home
user_home=$(getent passwd "${SUDO_USER:-root}" | cut -d: -f6)
else # Script is run by a regular user
user_home=$HOME
fi

# Check if user has a home
if [[ -z "$HOME" || $HOME == "/nonexistent" ]]; then
echo -e "${red}This script requires a home directory.${reset}" >&2
exit 1
fi
echo "$user_home"
}

get_invoking_user() {
if [[ $EUID -eq 0 ]]; then # Script is running as root
# Use SUDO_USER if set, otherwise fall back to root
echo "${SUDO_USER:-root}"
else # Script is run by a regular user
whoami
fi
}

update_repo() {
local script_dir="$1"
Expand All @@ -31,16 +38,37 @@ update_repo() {
fi
}

# colors
reset='\033[0m'
red='\033[0;31m'
yellow='\033[1;33m'
green='\033[0;32m'
blue='\033[0;34m'

# Define variables
USER_HOME=$(get_user_home)
script_name='heavyscript'
script_dir="$HOME/heavy_script"
user_bin_dir="$HOME/bin"
script_dir="$USER_HOME/heavy_script"
user_bin_dir="$USER_HOME/bin"
system_bin_dir="/usr/local/bin"
user_script_wrapper="$user_bin_dir/$script_name"
system_script_wrapper="$system_bin_dir/$script_name"

main() {
# Check if user has a home
if [[ -z "$USER_HOME" || $USER_HOME == "/nonexistent" ]]; then
echo -e "${red}This script requires a home directory.${reset}" >&2
echo -e "${red}Please create a home directory for the user.${reset}" >&2
echo -e "${red}You can do so under the credentials tab in the Truenas SCALE GUI${reset}" >&2
exit 1
fi

if [[ $EUID -ne 0 ]]; then
echo -e "${yellow}Warning: The script is not running with root privileges. To create a symlink in ${blue}$system_bin_dir${yellow}, please rerun the script with sudo or as the root user.${reset}"
echo -e "${yellow}You can exit now with CTRL+C to rerun with sudo, or wait 10 seconds to continue without creating the system-wide symlink.${reset}"
sleep 10
fi

# Check if the script repository already exists
if [[ -d "$script_dir" ]]; then
echo -e "${yellow}The ${blue}$script_name${yellow} repository already exists.${reset}"
Expand Down Expand Up @@ -68,7 +96,7 @@ main() {
else
# Clone the script repository
echo -e "${blue}Cloning $script_name repository...${reset}"
cd "$HOME" || exit 1
cd "$USER_HOME" || exit 1
if ! git clone "https://github.com/Heavybullets8/heavy_script.git" &>/dev/null; then
echo -e "${red}Failed to clone the repository${reset}"
exit 1
Expand All @@ -90,33 +118,48 @@ main() {
mkdir "$user_bin_dir"
fi

# Create symlink inside user's bin
echo -e "${blue}Creating $user_script_wrapper wrapper...${reset}"
ln -sf "$script_dir/bin/$script_name" "$user_script_wrapper"
chmod +x "$script_dir/bin/$script_name"

# Create symlink inside system's bin
echo -e "${blue}Creating $system_script_wrapper wrapper...${reset}"
sudo ln -sf "$script_dir/bin/$script_name" "$system_script_wrapper"
sudo chmod +x "$script_dir/bin/$script_name"
if [[ $EUID -ne 0 ]]; then
echo -e "${yellow}Warning: Skipping system-wrapper...${reset}"
# Create symlink inside user's bin only
echo -e "${blue}Creating $user_script_wrapper wrapper...${reset}"
ln -sf "$script_dir/bin/$script_name" "$user_script_wrapper"
chmod +x "$script_dir/bin/$script_name"
else
# Create symlink inside both user's and system's bin
echo -e "${blue}Creating $user_script_wrapper and $system_script_wrapper wrappers...${reset}"
ln -sf "$script_dir/bin/$script_name" "$user_script_wrapper"
ln -sf "$script_dir/bin/$script_name" "$system_script_wrapper"
chmod +x "$script_dir/bin/$script_name"
chmod +x "$system_script_wrapper"
fi

echo

# Add $HOME/bin to PATH in .bashrc and .zshrc
# Add $USER_HOME/bin to PATH in .bashrc and .zshrc
for rc_file in .bashrc .zshrc; do
if [[ ! -f "$HOME/$rc_file" ]]; then
echo -e "${blue}Creating $HOME/$rc_file file...${reset}"
touch "$HOME/$rc_file"
if [[ ! -f "$USER_HOME/$rc_file" ]]; then
echo -e "${blue}Creating $USER_HOME/$rc_file file...${reset}"
touch "$USER_HOME/$rc_file"
fi

if ! grep -q "$user_bin_dir" "$HOME/$rc_file"; then
if ! grep -q "$user_bin_dir" "$USER_HOME/$rc_file"; then
echo -e "${blue}Adding $user_bin_dir to $rc_file...${reset}"
echo "export PATH=$user_bin_dir:\$PATH" >> "$HOME/$rc_file"
echo "export PATH=$user_bin_dir:\$PATH" >> "$USER_HOME/$rc_file"
fi
done

local invoking_user
invoking_user=$(get_invoking_user)
if [[ $EUID -eq 0 && -n $SUDO_USER ]]; then
echo -e "${green}Changing ownership of HeavyScript to ${blue}$invoking_user${green}...${reset}"
chown -R "$invoking_user" "$script_dir"
chown "$invoking_user" "$user_bin_dir/$script_name"
fi

echo
echo -e "${green}Successfully installed ${blue}$script_name${reset}"
echo -e "${green}Successfully installed ${blue}HeavyScript${green} to ${blue}$script_dir${reset}"
echo -e "${green}Your example cronjob:${reset}"
echo -e "${blue}bash $script_dir/heavy_script.sh update${reset}"
echo
}

Expand Down
Loading

0 comments on commit e7af3a7

Please sign in to comment.