Skip to content

Commit

Permalink
made the script faster and more user friendly
Browse files Browse the repository at this point in the history
  • Loading branch information
CreativeCodeCat committed Sep 19, 2021
1 parent 1f2757d commit 87da2c6
Show file tree
Hide file tree
Showing 13 changed files with 299 additions and 43 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
build
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ include config.mk

install: all
mkdir -p $(DESTDIR)$(PREFIX)/bin
cp -f repomenu_snapper $(DESTDIR)$(PREFIX)/bin
cp -f repomenue_snapshots $(DESTDIR)$(PREFIX)/bin
mkdir -p $(DESTDIR)$(LOCALPREFIX)/bin
cp -f snap* $(DESTDIR)$(LOCALPREFIX)/bin
mkdir -p $(DESTDIR)$(SYSTEMD)/system
Expand All @@ -15,7 +15,7 @@ install: all


uninstall:
rm -rf $(DESTDIR)$(PREFIX)/bin/repomenu_snapper
rm -rf $(DESTDIR)$(PREFIX)/bin/repomenue_snapshots
rm -rf $(DESTDIR)$(LOCALPREFIX)/bin/snap*
rm -rf $(DESTDIR)$(SYSTEMD)/system/auto-snapshot*
rm -rf $(DESTDIR)$(PACMAND)/hooks/*make_snapshot*
Expand Down
13 changes: 13 additions & 0 deletions build-aux/meson/post-install.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#!/usr/bin/env python3

import os
import subprocess
import sys

datadir = sys.argv[1]

# Packaging tools define DESTDIR and this isn't needed for them
if 'DESTDIR' not in os.environ:
print('Compiling GSettings schemas...')
subprocess.call(['glib-compile-schemas',
os.path.join(datadir, 'glib-2.0', 'schemas')])
50 changes: 50 additions & 0 deletions meson.build
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
project(
'repomenu-schemas', 'c',
version: '3.38.0',
default_options: [
'warning_level=2'
],
license: 'LGPL',
meson_version: '>= 0.50.0'
)

gsettings_desktop_schemas_name = meson.project_name()
gsettings_desktop_schemas_version = meson.project_version()

message('Building ' + gsettings_desktop_schemas_name + ' ' + gsettings_desktop_schemas_version)

cc = meson.get_compiler('c')
host_system = host_machine.system()
arch = host_machine.cpu_family()

prefix = get_option('prefix')
bindir = join_paths(prefix, get_option('bindir'))
libdir = join_paths(prefix, get_option('libdir'))
libexecdir = join_paths(prefix, get_option('libexecdir'))
includedir = join_paths(prefix, get_option('includedir'))
datadir = join_paths(prefix, get_option('datadir'))
sysconfdir = join_paths(prefix, get_option('sysconfdir'))
schemasdir = join_paths(datadir, 'glib-2.0/schemas')

gnome = import('gnome')

# Generate pc file
# Can't use pkgconfig helper https://github.com/mesonbuild/meson/issues/2253
conf = configuration_data()
conf.set('prefix', prefix)
conf.set('VERSION', meson.project_version())
conf.set('includedir', includedir)

pkg = configure_file(configuration: conf,
input: 'repomenu-snapshots-schemas.pc.in',
install: true,
install_dir: join_paths(datadir, 'pkgconfig'),
output: 'repomenu-schemas.pc')

glib_mkenums = find_program('glib-mkenums')

subdir('schemas')

# Keep this in sync with post-install.py expected arguments
meson.add_install_script('build-aux/meson/post-install.py',
datadir)
6 changes: 6 additions & 0 deletions repomenu-snapshots-schemas.pc.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
prefix=@prefix@

Name: repomenu-snapshots-schemas
Description: Shared GSettings schemas for the desktop, including helper headers
Version: @VERSION@
Cflags: -I@includedir@/repomenu-snapshots-schemas
File renamed without changes.
29 changes: 29 additions & 0 deletions schemas/meson.build
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
schemas = [
'org.therepoclub.repomenu.snapshots.preferences.gschema.xml',
]

conf = configuration_data()
conf.set('prefix', prefix)
conf.set('exec_prefix', prefix)
conf.set('libdir', libdir)
conf.set('bindir', bindir)
conf.set('libexecdir', libexecdir)
conf.set('datarootdir', datadir)
conf.set('datadir', datadir)
conf.set('includedir', includedir)

generated_schemas = []
foreach schema: schemas
generated_schemas += configure_file(
output: schema,
input: schema + '.in',
configuration: conf,
)
endforeach

install_data(generated_schemas, install_dir: schemasdir)

install_data(
'repomenu-snapshots-schemas.convert',
install_dir: join_paths(datadir, 'GConf', 'gsettings'),
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?xml version="1.0" encoding="UTF-8"?>
<schemalist>
<schema path="/org/therepoclub/repomenu/snapshots/preferences/" id="org.therepoclub.repomenu.snapshots.preferences">
<key type="s" name="pacman">
<default>'5'</default>
<summary>Number of pacman snapshots to save</summary>
<description>This is the number of pacman snapshots to save.</description>
</key>
<key type="s" name="root">
<default>'15'</default>
<summary>Number of root snapshots to save</summary>
<description>This is the number of root snapshots to save.</description>
</key>
<key type="s" name="home">
<default>'15'</default>
<summary>Number of home snapshots to save</summary>
<description>This is the number of home snapshots to save.</description>
</key>
</schema>
</schemalist>
5 changes: 5 additions & 0 deletions schemas/repomenu-snapshots-schemas.convert
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# From org.therepoclub.repomenu.snapshots.preferences
[org.therepoclub.repomenu.snapshots.preferences]
pacman = 10
root = 10
home = 10
31 changes: 22 additions & 9 deletions snaphome
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
#!/usr/bin/env bash
set -e

SNAP_HOME=$(gsettings get org.therepoclub.repomenu.snapshots.preferences home | sed s/\'//g)
SNAP_PACMAN=$(gsettings get org.therepoclub.repomenu.snapshots.preferences pacman | sed s/\'//g)

## Script metadata
SCRIPTNAME=${0##*/}
VERSION="2021.09.19"
Expand Down Expand Up @@ -37,7 +40,7 @@ error() {

main() {
#number of snapshots allowed
sn=15
sn=$SNAP_HOME

runas=$(whoami)

Expand Down Expand Up @@ -68,9 +71,14 @@ main() {
mnt='/mnt/750GB/.snapshots'

if [ -e "$hsvdv" ]; then
printf "${yellow} mounted $hsvdv to $mnt${nc}\n"
if [ -d "$mnt" ]; then
printf "${YELLOW}Mounted $hsvdv to $mnt${ALL_OFF}\n"
else
printf "${RED}No btrfs drives found${ALL_OFF}\n"
exit 1
fi
else
printf "${red} no btrfs drives found${nc}\n"
printf "${RED}No btrfs drives found${ALL_OFF}\n"
exit 1
fi

Expand Down Expand Up @@ -108,7 +116,7 @@ main() {
fi

printf "\n"
read -rsn1 -p"Press any key to continue";echo
read -rsn1 -p "Press any key to continue";printf "\n"
}

#=== FUNCTION =================================================================
Expand All @@ -118,7 +126,7 @@ main() {

pacman() {
#number of snapshots allowed
sn=5
sn=$SNAP_PACMAN

runas=$(whoami)

Expand Down Expand Up @@ -149,9 +157,14 @@ pacman() {
mnt='/mnt/750GB/.snapshots'

if [ -e "$hsvdv" ]; then
printf "${yellow} mounted $hsvdv to $mnt${nc}\n"
if [ -d "$mnt" ]; then
printf "${YELLOW}Mounted $hsvdv to $mnt${ALL_OFF}\n"
else
printf "${RED}No btrfs drives found${ALL_OFF}\n"
exit 1
fi
else
printf "${red} no btrfs drives found${nc}\n"
printf "${RED}No btrfs drives found${ALL_OFF}\n"
exit 1
fi

Expand Down Expand Up @@ -189,7 +202,7 @@ pacman() {
fi

printf "\n"
read -rsn1 -p"Press any key to continue";echo
read -rsn1 -p "Press any key to continue";printf "\n"
}

#=== FUNCTION =================================================================
Expand Down Expand Up @@ -255,4 +268,4 @@ while true; do
break
;;
esac
done
done
142 changes: 123 additions & 19 deletions snaplist
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
#!/usr/bin/env bash
cmd=$(basename $0)
## Script metadata
SCRIPTNAME=${0##*/}
VERSION="2021.09.19"
DESCRIPTION="A bach BTRFS Sanpshot wrapper"
AUTHOR="The-Repo-Club <[email protected]>"

## Set Colors (copied from makepkg)
ALL_OFF="\e[1;0m"
Expand All @@ -12,30 +16,130 @@ CYAN="\e[1:36m"

readonly ALL_OFF RED GREEN YELLOW BLUE PURPLE CYAN

runas=$(whoami)
main(){
runas=$(whoami)

if [ $runas != 'root' ]; then
printf ${GREEN}"%s ${RED}command must be ran as root\n" "$cmd"
exit
fi
if [ $runas != 'root' ]; then
printf ${GREEN}"%s ${RED}command must be ran as root\n" "$SCRIPTNAME"
exit
fi

IndexOf() {
x="${1%%$2*}"
[ "$x" = "$1" ] || printf '%d' "${#x}"
}

declare -A PathCounts
declare -A Paths
NL='\n'

while IFS= read -r Line; do

set -- $Line

# Path is after the first "path "
Path=$9

# Get the index of the first /
PathL2Index=$( IndexOf "$Path" '/' )

# Ignore paths that don't have a 2nd level
[ -z "$PathL2Index" ] && continue

Id=$2

#number of root snapshots
rsl="$(btrfs subvolume list / | awk '/snap/ && /@root/' | wc -l)"
# Path is after the first / in Path
PathL2=${Path:$(( PathL2Index + 1 ))}

#number of home snapshots
hsl="$(btrfs subvolume list / | awk '/snap/ && /@home/' | wc -l)"
# Remove the 10 char date from the end to isolate the name
NameL2=${PathL2%????????????????????}

printf ${GREEN}"list of root snapshots:${YELLOW} $rsl ${ALL_OFF}\n"
# Remove the 1st character "@"
NameL2=${NameL2:1}

#list of root snapshots by name
btrfs subvolume list / | awk '/snap/ && /@root/ {print $1"="$2" "$9}'
# If -pacman exists in the NameL2, it's category is pacman
if [ "${NameL2##*-pacman}" = "$1" ]; then
Category='pacman'
else
Category=$NameL2
fi

# Add NameL2 to appropriate category
Paths["$Category"]="${Paths["$Category"]}ID=$Id $Path$NL"

# Increment count in appropriate category
(( PathCounts["$Category"]++ ))

done <<< $(btrfs subvolume list /)

FirstRun=1
for Key in "${!Paths[@]}"; do
[ -n "$FirstRun" ] || printf '\n'
printf "${GREEN}list of %s snapshots: ${YELLOW}%s${ALL_OFF}\n" "$Key" "${PathCounts[$Key]}"
printf "${Paths[$Key]}";
FirstRun=
done

printf "\n"
read -rsn1 -p "Press any key to continue";printf "\n"
}

#=== FUNCTION =================================================================
# Name: version
# Description: Prints version information
#==============================================================================

version() {
printf "
%s v%s - %s
This program may be freely redistributed under
the terms of the GNU General Public License v3.0.
%s
\n" "$SCRIPTNAME" "$VERSION" "$DESCRIPTION" "$AUTHOR"
exit
}

printf "\n"
#=== FUNCTION =================================================================
# Name: usage
# Description: Prints the usage
#==============================================================================

printf ${GREEN}"list of home snapshots:${YELLOW} $hsl ${ALL_OFF}\n"
usage() {

#list of home snapshots by name
btrfs subvolume list / | awk '/snap/ && /@home/ {print $1"="$2" "$9}'
printf "
Snapshot the @ and @home btrfs subvolumes.
Usage:
%s...
%s [options]...
Options:
-h, --help Display help.
-v, --version Print version information and exits.
\n" "$SCRIPTNAME" "$SCRIPTNAME"
}

printf "\n"
read -rsn1 -p"Press any key to continue";echo
while true; do
case $1 in
-h|--help|-\?)
usage
exit 0
;;
-v|--version)
version
exit 0
;;
--)
shift
break
;;
-*)
error "Unknown option: $1"
usage
exit 1
;;
*)
main
break
;;
esac
done
Loading

0 comments on commit 87da2c6

Please sign in to comment.