Skip to content

Commit

Permalink
🚀 feat(_utils): add snigdha os utils pkgbuild & files
Browse files Browse the repository at this point in the history
  • Loading branch information
eshanized committed Dec 21, 2024
1 parent e8c3754 commit 542e80f
Show file tree
Hide file tree
Showing 7 changed files with 802 additions and 0 deletions.
38 changes: 38 additions & 0 deletions snigdhaos-utils/PKGBUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# Maintainer: Eshan Roy <[email protected]>

pkgname=snigdhaos-utils
pkgver=1
pkgrel=1
pkgdesc="A set of utility scripts for Snigdha OS"
arch=('any')
url="https://snigdhaos.org/"
license=('MIT')
depends=('bash')
source=(
'snigdhaos-cleanup.sh'
'snigdhaos-uninstall-package.sh'
'snigdhaos-update.sh'
'snigdhaos-backup.sh'
'pacman'
) # Add the pacman file source URL here
sha256sums=(
'SKIP'
'SKIP'
'SKIP'
'SKIP'
)

# The package installation directory
install_dir="/usr/local/bin"

package() {
# Install the update script
install -Dm755 "$srcdir/snigdhaos-update.sh" "$pkgdir$install_dir/snigdhaos-update.sh"
# Install the cleanup script
install -Dm755 "$srcdir/snigdhaos-cleanup.sh" "$pkgdir$install_dir/snigdhaos-cleanup.sh"
# Install the uninstaller script
install -Dm755 "$srcdir/snigdhaos-uinstall-package.sh" "$pkgdir$install_dir/snigdhaos-uinstall-package.sh"

# Install the pacman file
install -Dm755 "$srcdir/pacman" "$pkgdir$install_dir/pacman"
}
22 changes: 22 additions & 0 deletions snigdhaos-utils/README.install
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
Snigdha OS Utilities Installation Instructions
============================================

Thank you for installing Snigdha OS Utilities. Below are the steps and
information regarding this package:

1. The following scripts are installed to /usr/local/bin:
- snigdhaos-update.sh: A script for system update and maintenance.
- snigdhaos-cleanup.sh: A script for cleaning up unused files and caches.
- snigdhaos-backup.sh: A script for backing up important system files.
- pacman: A script that intercepts the `pacman` command to use `snigdhaos-update.sh`.

2. Post-installation:
- Ensure that the scripts in `/usr/local/bin` have the correct permissions.
- If you want to use `snigdhaos-update.sh` as a replacement for `pacman -Syyu`, make sure that the `pacman` script is in place and executable.

3. Optional: You can create aliases or modify shell profiles to make usage more convenient.

Example usage:
- Run `snigdhaos-update.sh` directly for system updates.
- Use `snigdhaos-cleanup.sh` for system cleanup.
- Use `snigdhaos-backup.sh` for backups.
10 changes: 10 additions & 0 deletions snigdhaos-utils/pacman
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#!/bin/bash
# Custom pacman wrapper to run snigdhaos-update.sh

# Check if the user wants to update the system
if [[ "$@" == "-Syyu" ]]; then
sudo /usr/local/bin/snigdhaos-update.sh # Run your custom update script
else
# Call the real pacman for other operations
/usr/bin/pacman "$@"
fi
218 changes: 218 additions & 0 deletions snigdhaos-utils/snigdhaos-backup.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,218 @@
#!/bin/bash

# Author: Eshan Roy
# Description: Backup important Snigdha OS system configurations and settings

set -e # Exit immediately if a command exits with a non-zero status

# Colors
RED="\033[1;31m"
GREEN="\033[1;32m"
YELLOW="\033[1;33m"
BLUE="\033[1;34m"
RESET="\033[0m"

# Emojis for user feedback
INFO="📝"
SUCCESS=""
WARNING="⚠️"
ERROR=""
BACKUP="💾"

# Directories and files to be backed up
BACKUP_DIR="$HOME/snigdhaos_backups"
DATE=$(date +"%Y-%m-%d_%H-%M-%S")
SNIGDHAOS_BACKUP="$BACKUP_DIR/snigdhaos_backup_$DATE"
LOG_FILE="$BACKUP_DIR/backup_log_$DATE.txt"

# List of important configuration directories and files
CONFIG_FILES=(
"/etc/pacman.conf"
"/etc/fstab"
"/etc/locale.conf"
"/etc/vconsole.conf"
"/etc/hostname"
"/etc/hosts"
"/etc/sudoers"
"/etc/systemd/system"
"/etc/X11/xorg.conf.d"
"/etc/udev"
"/etc/NetworkManager"
"/etc/crypttab"
"/etc/mkinitcpio.conf"
"/etc/pm.conf"
"$HOME/.bashrc"
"$HOME/.zshrc"
"$HOME/.config/i3"
"$HOME/.config/sway"
"$HOME/.config/kitty"
"$HOME/.config/nvim"
"$HOME/.config/alacritty"
"$HOME/.config/rofi"
)

# Function to display help message
show_help() {
echo -e "${BLUE}Usage: ${RESET}snigdhaos-backup.sh [OPTIONS]"
echo -e ""
echo -e "${GREEN}This script will back up important Snigdha OS system configuration files and settings.${RESET}"
echo -e ""
echo -e "${YELLOW}Options:${RESET}"
echo -e " ${GREEN}-h, --help${RESET} Show this help message."
echo -e " ${GREEN}-v, --version${RESET} Display the script version."
echo -e " ${GREEN}-b, --backup-dir${RESET} Specify a custom backup directory (default: \$HOME/snigdhaos_backups)."
echo -e " ${GREEN}-l, --list${RESET} List the files that will be backed up."
echo -e " ${GREEN}-e, --encrypt${RESET} Encrypt the backup with GPG."
echo -e " ${GREEN}-c, --compression${RESET} Choose compression format (gzip, xz, bz2)."
echo -e " ${GREEN}-r, --restore${RESET} Restore backup from a specified file."
echo -e ""
echo -e "Example usage:"
echo -e " snigdhaos-backup.sh -e # Create an encrypted backup."
echo -e " snigdhaos-backup.sh -l # List files to be backed up."
echo -e " snigdhaos-backup.sh -b /path/to/dir # Specify a custom backup directory."
}

# Function to display version information
show_version() {
echo -e "${BLUE}Version: 1.1.0${RESET}"
}

# Parse command line options
CUSTOM_BACKUP_DIR=false
LIST_FILES=false
ENCRYPT_BACKUP=false
COMPRESSION_FORMAT="gz"
RESTORE=false
while [[ "$#" -gt 0 ]]; do
case "$1" in
-h|--help)
show_help
exit 0
;;
-v|--version)
show_version
exit 0
;;
-b|--backup-dir)
CUSTOM_BACKUP_DIR=true
BACKUP_DIR="$2"
shift
;;
-l|--list)
LIST_FILES=true
shift
;;
-e|--encrypt)
ENCRYPT_BACKUP=true
shift
;;
-c|--compression)
COMPRESSION_FORMAT="$2"
shift
;;
-r|--restore)
RESTORE=true
BACKUP_FILE="$2"
shift
;;
*)
echo -e "${ERROR} Unknown option: $1${RESET}"
show_help
exit 1
;;
esac
done

# Ensure backup directory exists
mkdir -p "$BACKUP_DIR"

# Function to display a user warning
user_warning() {
echo -e "${WARNING} WARNING: You are about to proceed with the backup or restoration process.${RESET}"
echo -e "${YELLOW}This action may overwrite existing backup data or restore files from a previous backup.${RESET}"
read -p "Do you want to continue? (y/n): " user_input
if [[ ! "$user_input" =~ ^[Yy]$ ]]; then
echo -e "${ERROR} Operation aborted by user.${RESET}"
exit 1
fi
}

# Function to list files to be backed up
list_files() {
echo -e "${INFO} The following files and directories will be backed up:${RESET}"
for item in "${CONFIG_FILES[@]}"; do
echo -e "${GREEN} - $item${RESET}"
done
}

# Function to backup a file or directory
backup_item() {
local item="$1"
if [ -e "$item" ]; then
echo -e "${INFO} Backing up $item...${RESET}" | tee -a "$LOG_FILE"
cp -r "$item" "$SNIGDHAOS_BACKUP" | tee -a "$LOG_FILE"
else
echo -e "${WARNING} Warning: $item not found. Skipping.${RESET}" | tee -a "$LOG_FILE"
fi
}

# Function to create a compressed backup tarball
create_tarball() {
echo -e "${INFO} Creating $COMPRESSION_FORMAT tarball of backup...${RESET}" | tee -a "$LOG_FILE"
case "$COMPRESSION_FORMAT" in
gz) tar -czf "$BACKUP_DIR/$TAR_NAME" -C "$BACKUP_DIR" . | tee -a "$LOG_FILE" ;;
xz) tar -cJf "$BACKUP_DIR/$TAR_NAME" -C "$BACKUP_DIR" . | tee -a "$LOG_FILE" ;;
bz2) tar -cjf "$BACKUP_DIR/$TAR_NAME" -C "$BACKUP_DIR" . | tee -a "$LOG_FILE" ;;
*) echo -e "${ERROR} Invalid compression format! Using gzip by default." | tee -a "$LOG_FILE" ;;
esac
}

# Function to encrypt the backup
encrypt_backup() {
if $ENCRYPT_BACKUP; then
echo -e "${INFO} Encrypting the backup...${RESET}" | tee -a "$LOG_FILE"
gpg --symmetric --cipher-algo AES256 "$BACKUP_DIR/$TAR_NAME" | tee -a "$LOG_FILE"
rm "$BACKUP_DIR/$TAR_NAME"
echo -e "${SUCCESS} Backup encrypted successfully!" | tee -a "$LOG_FILE"
fi
}

# Function to restore a backup
restore_backup() {
echo -e "${INFO} Restoring backup from $BACKUP_FILE...${RESET}" | tee -a "$LOG_FILE"
tar -xvf "$BACKUP_FILE" -C "$HOME" | tee -a "$LOG_FILE"
echo -e "${SUCCESS} Restore completed successfully!" | tee -a "$LOG_FILE"
}

# Backup process
backup_process() {
echo -e "${INFO} Starting backup process...${RESET}" | tee -a "$LOG_FILE"

# Loop through the list of configuration files and directories
for item in "${CONFIG_FILES[@]}"; do
backup_item "$item"
done

# Create a tarball for easy backup storage
TAR_NAME="snigdhaos_backup_$DATE.tar.$COMPRESSION_FORMAT"
create_tarball
encrypt_backup
echo -e "${SUCCESS} Backup completed successfully! Backup is located at: $BACKUP_DIR/$TAR_NAME" | tee -a "$LOG_FILE"
}

# List files to be backed up if --list is passed
if $LIST_FILES; then
list_files
exit 0
fi

# Start backup or restore process
if $RESTORE; then
user_warning
restore_backup
else
user_warning
echo -e "${INFO} Preparing backup...${RESET}" | tee -a "$LOG_FILE"
SNIGDHAOS_BACKUP="$BACKUP_DIR/snigdhaos_backup_$DATE"
backup_process
fi
Loading

0 comments on commit 542e80f

Please sign in to comment.