Scripts

Wallpaper Rotation

This is going to be a bit of a shorter article than I normally write. Basically, I’m showing off a random wallpaper switcher. I’ve tried several tools that purport to do the job. None of which were particularly flexible and seemed to be more trouble than they’re worth. Prior to this, I had actually used tools like hsetroot and feh to either set the background to a pixmap or just a solid color. Neither of these were really a decent solution sets in my mind.

So, I did what a lot of Linux users do. Scratch their own itch. In this case I take feh’s random background image function, marry that to systemd’s timer tools and let it go. Here’s the result:

The Script

#!/bin/fish

set -l WALLPAPERS $HOME/Pictures/Wallpaper
set -l BGOPTIONS  fill
set -l URGENCY    low
set -l EXPIRE     5000
set -l TITLE      "Wallpaper Change"
set -l MESSAGE    "Hey, your wallpaper changed!"
set -l ICONPATH   /usr/share/feh/images/feh.png

feh --randomize --no-fehbg --bg-$BGOPTIONS $WALLPAPERS
notify-send --icon=$ICONPATH --app-name=feh --urgency=$URGENCY --expire-time=$EXPIRE $TITLE $MESSAGE

This script requires feh, though you could swap in any sort of tool that can set the root pixmap. If you choose to change it, you’ll need to come up with your own randomization. I set the –no-fehbg option because I don’t need it generating a script in my $HOME. Once set, I have notify send tell me about it. This is more for debugging purposes so that I know when it kicks off.

The Service

[Unit]
Description=Wallpaper changer

[Service]
Type=simple
ExecStart=/home/ndegruchy/.local/bin/wallpaper

[Install]
WantedBy=timers.target

A pretty much bog-standard systemd service file. I place this into $HOME/.config/systemd/user. Make sure you update the ExecStart with the location of the script above.

The Timer

[Unit]
Description=Changes wallpaper hourly

[Timer]
OnBootSec=10min
OnUnitActiveSec=1h

[Install]
WantedBy=timers.target

Another pretty bog-standard systemd unit file, this one is a timer. Like above, go ahead and drop it in $HOME/.config/systemd/user. Use the same name as the service file, otherwise it wont work. You can change the time interval, too.

Put it All Together

Now that you’ve got all the files in place, you can enable it with:

$ systemctl --user enable wallpaper.timer
$ systemctl --user start wallpaper.timer

This will begin the countdown. If you want to see the status of the timer, you can check on it with:

$ systemctl --user list-timers wallpaper.timer
$ # OR
$ systemctl --user list-timers --all

This will give you some information about the timer in a fairly understandable table. Hope this helps someone!

Useful Scripts

I have a small collection of scripts/Fish Shell functions that I use to ease the burden of getting things done. Not all of them are worth of a post, but I’d like to share what I have, because if it can help someone over a hurdle, then all the better.

Fetch from AUR

function aur -d "Fetch a package from AUR4"
    set -l projdir "/home/ndegruchy/Documents/AUR/"
    set -l appldir $projdir/$argv
    git clone https://aur.archlinux.org/$argv.git $projdir/$argv
    cd $appldir
end

This particular snippet simply creates a new directory in my documents/AUR folder, then clones the specified AUR program to that directory, changing directory into it to make it faster to get up and building. I don’t have much use for this now, since I use pikaur to manage my AUR packages.

Blurred Screen Locker

#!/bin/bash

IMAGE=/tmp/i3lock.png
RES=1920x1080

ffmpeg -loglevel quiet -f x11grab -video_size $RES -y -i $DISPLAY -filter_complex "boxblur=10" -vframes 1 $IMAGE

i3lock --image=$IMAGE --ignore-empty-password --show-failed-attempts

rm $IMAGE

This bash script uses ffmpeg to take an X11 screenshot, and apply some blur to it before invoking my screen locker (i3lock), using the image as the image. Looks pretty nice and it doesn’t require much in the way of resources/tooling to get going.

Resume Builder

function -d "Compresses and retitles my resume PDF"
	exiftool -Title="Nathan DeGruchy's Resume" main.pdf
	gs -sDEVICE=pdfwrite -dCompatibilityLevel=1.4 -dPDFSETTINGS=/printer -dNOPAUSE -dQUIET -dBATCH -sOutputFile=ndegruchy-resume.pdf main.pdf
end

I often have several steps after I rebuild my resume out of LaTeX to make it ready for sending out. This Fish Function takes my resume, fixes the ‘title’ field, uses ghostscript to compress the whole thing and optimize it for screen reading. I should probably generate a Makefile for this to be honest…

Backups

So, it’s been another year and I’m getting better settled in to Linux. I’ve run through a lot of different window managers and desktop environments, like KDE, i3 and WindowMaker, before settling on DWM. Add to that more than a fair share of auxiliary programs to flesh out the experience and make things customized for me.

To that end, I’ve been trying to ensure that if anything should fail, I have a good backup to restore from. Enter Borg. It’s a command-line driven backup tool that not only compresses and encrypts the contents, but it de-duplicates it. This is fantastic for lots of little text files and the occasional photo set. The problem for me was, it was a lot of hassle feeding it all the options needed every time. Sure, I could alias it to make it simpler, but that still left a bit of work for me to do to get everything sorted, including un/mount the backup share, putting in a myriad of passwords, checking for number of incremental backups and pruning them so that I’m not dropping hundreds of gigabytes of useless, mostly similar backups (dedup’d or not) on my server, and on my offsite storage.

So, like most things on Linux, I glued all this together in a simple Fish Shell function. This allows me to just run a command (or add it as a user systemd timer) that backs everything I care about up, prunes and does all the drudgery of making a nice secure backup. Here’s my script/function that makes this all easier.

function my-backup -d "Mounts backup and runs borg"

	# Check for borg, first, bail out if it's not found
	if [ ! -x /usr/bin/borg ]
	    echo "Borg not found, please install before running this script."
	    exit 1
	end

	# Check to see if the repo pass is available. We use this to avoid
	# typing passwords all the time
	set -x BORG_PASSCOMMAND ""
	set -x CURRENT_DIR (pwd)

	# What to back up
	# Home Dir Top Level
	set -x BACKUP_FILES Documents Pictures .config .local .gnupg .xsession
	# Home misc
	set -a BACKUP_FILES /home/ndegruchy/.local/share/Steam/userdata/9974372/
	# Etc
	set -a BACKUP_FILES /etc/pacman.conf /etc/makepkg.conf
	# Emacs
	set -a BACKUP_FILES /usr/share/emacs/site-lisp/default.el
	# Package lists
	set -a BACKUP_FILES $MY_AUR_LIST $OFFICIAL_PACKAGE_LIST

	# Pacman package list backups, these are mine, the system will
	# (eventually) generate a full list at /etc/pkglist.txt
	echo "Exporting Pacman package lists"
	pacman -Qqem > $MY_AUR_LIST

	# Go home, for context
	cd ~

	# Check to see if the backup share is mounted, if not mount it
	if [ ! -d /mnt/backups/degruchy ]
	    echo "Mounting backup share to /mnt/backups/"
	    echo "You will need your password to do this."
	    sudo mount ...
	end

	echo "Trying to create a backup in /mnt/backups/degruchy with today's date and hostname."

	borg create --stats --progress --comment "New automated backup of critical stuff" --compression auto,lzma,6 /mnt/backups/degruchy::(date +%F)-(hostname) $BACKUP_FILES --exclude-from ~/.config/borg/exclude-file

	set -x IS_PHONE_PLUGGED (lsusb | grep -i "Apple" | wc -l)

	if [ $IS_PHONE_PLUGGED -gt 0 ]
	    ## TODO: The phone data is copied locally, first, probably a
	    ## good idea to check for available space, first.
	    echo "Looks like your phone is plugged in, backing that up, too..."
	    set -x PHONETEMP $HOME/.cache/temp/(date +%F)-nathans_phone
	    mkdir $PHONETEMP
	    idevicebackup2 backup $PHONETEMP
	    borg create --stats --progress --comment "Nathan's Phone backup" --compression auto,lzma,6 /mnt/backups/iPhoneBackups/::(date +%F)-nathans_phone $PHONETEMP
	    rm -rf $PHONETEMP
	end
	
	echo "Checking for backup prunability..."
	
	# Clean up old backups
	echo "Pruning..."
	borg prune --list --keep-daily 7 --keep-weekly 4 --keep-monthly 6 /mnt/backups/degruchy/
	borg prune --list --keep-daily 7 --keep-weekly 4 --keep-monthly 6 /mnt/backups/iPhoneBackups/

	echo "Unmounting backup. You may need your password again."
	sudo umount /mnt/backups
	echo "All done!"

		# Go back to where you were, if not already there
	if [ -d $CURRENT_DIR ]
	    cd $CURRENT_DIR
	else
	    cd ~
	end

	# Dump all the variables
	set -e CURRENT_DIR
	set -e BORG_PASSCOMMAND
	set -e BACKUP_FILES
	set -e IS_PHONE_PLUGGED
	set -e PHONETEMP
end