Little scripts and shortcuts that I use on my Ubuntu systems.

purgeoldkernel

Made for Ubuntu 16.04. Show kernel version, or if one is provided, purge everything.

#!/bin/bash

if [[ $# -eq 0 ]] ; then
    dpkg -l | grep linux-image
    exit 0
fi

declare -a PKGLIST=(
        'linux-signed-image-$version-generic'
        'linux-cloud-tools-$version'
        'linux-cloud-tools-$version-generic'
        'linux-headers-$version'
        'linux-headers-$version-generic'
        'linux-image-$version-generic'
        'linux-image-extra-$version-generic'
        'linux-signed-image-$version-generic'
        'linux-tools-$version'
        'linux-tools-$version-generic'
)

for version in "$@"; do
        for package in ${PKGLIST[@]}; do
                TEMPPKG=$(eval "echo $package")
                {
                        PKGINSTALLED=$(dpkg-query -W --showformat='${Status}\n' $TEMPPKG | egrep " installed|config-files")
                } &> /dev/null
                if [ "$PKGINSTALLED" != "" ]; then
                        PURGELIST="$PURGELIST $TEMPPKG"
                fi
        done
done

echo $PURGELIST
apt purge $PURGELIST
apt -f install

snapshot

Create an lvm snapshot of the root volume. You will need to edit this.

/sbin/lvcreate -L1G -s -n "root-"$(date +"%Y%m%d-%H%M") vg0/lv_root

snapshot-rotate

Create snapshots of all volumes and delete the oldest ones. You will need to edit this.

#!/bin/bash
lvs_array=("root" "mail" "mysql" "www")
vg_postfix="0"

# Create array from list of LVs
lv_array=($(/sbin/lvs ${lvs_array[0]}$vg_postfix --noheadings -o lv_name))

# Sort the array
arr2=( $(
	for el in "${lv_array[@]}"
		do
		echo "$el"
		done | /usr/bin/sort ) )
# Find the oldest snapshot 
for i in "${arr2[@]}"
do
# Insurance that we do not delete the base volume
	if [ "$i" != "${lvs_array[0]}" ]; then
		# Delete the snapshots from each lvs_array using the first as a template
		for working_lv in ${lvs_array[@]}; do
			/sbin/lvremove -f $working_lv$vg_postfix/$working_lv${i#${lvs_array[0]}}
		done
		# Delete old file backup
		#echo rm -f "/root/backups/$i*"
		break
	fi
done

working_timestamp=$(date +"%Y%m%d-%H%M")

# Create a new snapshots
for working_lv in ${lvs_array[@]}; do
        /sbin/lvcreate -L1G -s -n $working_lv"-"$working_timestamp $working_lv"0/"$working_lv
done