-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit f89a90c
Showing
10 changed files
with
214 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
# Environment | ||
.env | ||
|
||
# Private Scripts | ||
private-*.sh |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
# System Repo | ||
My collection of utilities used on servers. | ||
|
||
## Bin | ||
Scripts and binaries that can be used in PATH for making life easier. | ||
|
||
## Lib | ||
All libaries("modules") that can be used for creating your system utility scripts like backups and etc. | ||
|
||
### Script.sh | ||
The base library of each script. Containing several useful functions. | ||
|
||
##### load {LIB_NAME} | ||
Loads a library from the lib folder. | ||
```bash | ||
load "use-env" | ||
``` | ||
|
||
##### is_root | ||
Will check if the executing user is a root user. | ||
```bash | ||
is_root | ||
``` | ||
|
||
##### rid | ||
Generates a random string with 8 characters on default. | ||
```bash | ||
RANDOM_SIZE=8 # Default in script.sh. Add to script to override | ||
|
||
MY_ID=$(rid) | ||
``` | ||
|
||
##### tz | ||
Spits out the current timestamp based on the `DATE_FORMAT` | ||
```bash | ||
BACKUP_TIME=$(tz) | ||
``` | ||
|
||
## Scripts | ||
All the scripts are assumed to be living in the root directory of the repository. Therefore you could also fork this repo to add your own scripts. If you want scripts not to be public(added to repo), prefix them with `private-`. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
#!/bin/bash | ||
# Backup: Haproxy and LXD | ||
|
||
source lib/script.sh | ||
load "use-env" | ||
load "backup-haproxy" | ||
load "backup-lxd" | ||
|
||
env "SERVER_NAME" | ||
is_root | ||
|
||
RUN_ID=$(rid) | ||
RUN_TIME=$(tz) | ||
|
||
write_yel "Script::started(${RUN_ID})" | ||
RUN_DIR="/tmp/${RUN_ID}" | ||
mkdir -p $RUN_DIR | ||
|
||
BCK_HAPROXY="${RUN_DIR}/${RUN_TIME}-${SERVER_NAME}-haproxy.tar.gz" | ||
bck_haproxy "$BCK_HAPROXY" | ||
|
||
BCK_LXD="${RUN_DIR}/${RUN_TIME}-${SERVER_NAME}-lxd.tar.gz" | ||
bck_lxd "$RUN_DIR" "$BCK_HAPROXY" | ||
|
||
bundle "${RUN_DIR}/${RUN_TIME}-${SERVER_NAME}-bck.tar.gz" $BCK_HAPROXY $BCK_LXD | ||
|
||
# rm -rf $RUN_DIR | ||
write_gre "Script::done(${RUN_ID})" |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
#!/bin/bash | ||
# Backup: Full - Haproxy | ||
|
||
HAPROXY_DIR="/etc/haproxy" | ||
|
||
function bck_haproxy() { | ||
write_yel "Backup::haproxy" | ||
local DIR=$1 | ||
sudo tar -zcf "$DIR" "$HAPROXY_DIR" >/dev/null 2>&1 | ||
|
||
if [ $? -ne 0 ]; then | ||
write_red "Backup::failed" | ||
exit 1 | ||
else | ||
write_gre "Backup::created(${DIR})" | ||
fi | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
#!/bin/bash | ||
# Backup: Full - LXD | ||
|
||
LXD_DIR="/var/snap/lxd/common/lxd" | ||
|
||
function bck_lxd() { | ||
write_yel "Backup::lxd" | ||
local TMP_DIR=$1 | ||
local BACKUP_FILE=$2 | ||
sudo cp -r "$LXD_DIR" "$TMP_DIR" | ||
sudo tar -zcf "$BACKUP_FILE" "$TMP_DIR/lxd" >/dev/null 2>&1 | ||
|
||
if [ $? -ne 0 ]; then | ||
write_red "Backup::failed" | ||
exit 1 | ||
else | ||
write_gre "Backup::created(${BACKUP_FILE})" | ||
fi | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
#!/bin/bash | ||
# Utilties | ||
|
||
COLOR_GREEN='\033[0;32m' | ||
COLOR_RED='\033[0;31m' | ||
COLOR_YELLOW='\033[0;33m' | ||
COLOR_RESET='\033[0m' | ||
DATE_FORMAT="%m-%d-%H%M" | ||
RANDOM_SIZE=8 | ||
|
||
function load() { | ||
source lib/$1.sh | ||
} | ||
|
||
function rid() { | ||
local random=$(head /dev/urandom | tr -dc A-Za-z0-9 | head -c $RANDOM_SIZE) | ||
echo $random | ||
} | ||
|
||
function tz() { | ||
local timestamp=$(date +$DATE_FORMAT) | ||
echo $timestamp | ||
} | ||
|
||
function write_gre() { | ||
local time=$(date +'%H:%M:%S') | ||
echo -e "${COLOR_GREEN}[+] ${time} - $1${COLOR_RESET}" | ||
} | ||
|
||
function write_red() { | ||
local time=$(date +'%H:%M:%S') | ||
echo -e "${COLOR_RED}[-] ${time} - $1${COLOR_RESET}" | ||
} | ||
|
||
function write_yel() { | ||
local time=$(date +'%H:%M:%S') | ||
echo -e "${COLOR_YELLOW}[+] ${time} - $1${COLOR_RESET}" | ||
} | ||
|
||
function bundle() { | ||
local BUNDLE_FILE="$1" | ||
shift | ||
|
||
if [ "$#" -lt 2 ]; then | ||
write_red "Bundle::failed(EXPECTED_MIN_TWO_TARS)" | ||
return 1 | ||
fi | ||
|
||
write_yel "Bundle::started" | ||
|
||
local BUNDLE_TEMP | ||
BUNDLE_TEMP=$(mktemp -d) || { echo "Failed to create temporary directory"; return 1; } | ||
trap 'rm -rf "$BUNDLE_TEMP"' EXIT | ||
|
||
for tarball in "$@"; do | ||
if [ -f "$tarball" ]; then | ||
cp "$tarball" "$BUNDLE_TEMP/" | ||
else | ||
echo "Warning: File $tarball not found, skipping." | ||
fi | ||
done | ||
|
||
tar -zcf "$BUNDLE_FILE" -C "$BUNDLE_TEMP" . >/dev/null 2>&1 | ||
rm -rf $BUNDLE_TEMP | ||
|
||
if [ $? -ne 0 ]; then | ||
write_red "Bundle::failed" | ||
exit 1 | ||
else | ||
write_gre "Bundle::created(${BUNDLE_TEMP})" | ||
fi | ||
} | ||
|
||
function is_root() { | ||
if [ "$(id -u)" -ne 0 ]; then | ||
write_red "Script::This script must be run as root." | ||
exit 1 | ||
fi | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
#!/bin/bash | ||
# Use/Env - Make use of .env files on shell scripts | ||
|
||
function env() { | ||
if [ -f .env ]; then | ||
export $(grep -v '^#' .env | xargs) &> /dev/null | ||
fi | ||
|
||
local vars="$1" | ||
local missing_vars=() | ||
IFS=', ' read -r -a vars_array <<< "$vars" | ||
for var in "${vars_array[@]}"; do | ||
if [ -z "${!var}" ]; then | ||
missing_vars+=("$var") | ||
fi | ||
done | ||
|
||
if [ ${#missing_vars[@]} -ne 0 ]; then | ||
write_red "Environment::missing(${missing_vars[*]})" | ||
exit 1 | ||
else | ||
write_gre "Environment::loaded" | ||
fi | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
#!/bin/bash | ||
# Use/Smb - Connect to SMB and be able to upload to it |