Misc scripts
Renaming daily/weekly/monthly rsnapshot folders
If something went wrong with rsnapshot, it might be neccessary to remove the last created backup folder. But then you'd have to rename the whole lots again, so that the new last one is called 0. For instance, you have 29 daily backups and remove daily.0. This command will rename 1 to 0, 2 to 1, 3 to 2, etc:
for i in `seq 1 29`; do mv daily.$i daily.`expr $i - 1`; done
Removing old kernels and headers
Because you automatically upgrade your kernels, your VMs get clogged up with old kernels and headers. Example:
visser@cajones:~$ ls -la /lib/modules total 32 drwxr-xr-x 8 root root 4096 Mar 15 06:47 . drwxr-xr-x 21 root root 4096 Sep 3 2014 .. drwxr-xr-x 5 root root 4096 Jan 16 02:50 3.13.0-71-generic drwxr-xr-x 5 root root 4096 Jan 16 02:51 3.13.0-74-generic drwxr-xr-x 5 root root 4096 Jan 20 06:53 3.13.0-76-generic drwxr-xr-x 5 root root 4096 Feb 2 06:37 3.13.0-77-generic drwxr-xr-x 5 root root 4096 Feb 23 06:35 3.13.0-79-generic drwxr-xr-x 5 root root 4096 Mar 15 06:48 3.13.0-83-generic
You should do some house cleaning by removing all old kernels, headers, and manually compiled modules. Make sure that you are running the latest kernel.
To list all installed kernels and accompanying headers:
root@cajones:~# apt-show-versions -b | grep -E 'linux-(headers|image(-extra)?)-[0-9]+' linux-headers-3.13.0-71:all/trusty-security linux-headers-3.13.0-71-generic:amd64/trusty-security linux-headers-3.13.0-74:all/trusty-security linux-headers-3.13.0-74-generic:amd64/trusty-security linux-headers-3.13.0-76:all/trusty-security linux-headers-3.13.0-76-generic:amd64/trusty-security linux-headers-3.13.0-77:all/trusty-security linux-headers-3.13.0-77-generic:amd64/trusty-security linux-headers-3.13.0-79:all/trusty-security linux-headers-3.13.0-79-generic:amd64/trusty-security linux-headers-3.13.0-83:all/trusty-security linux-headers-3.13.0-83-generic:amd64/trusty-security linux-image-3.13.0-71-generic:amd64/trusty-security linux-image-3.13.0-74-generic:amd64/trusty-security linux-image-3.13.0-76-generic:amd64/trusty-security linux-image-3.13.0-77-generic:amd64/trusty-security linux-image-3.13.0-79-generic:amd64/trusty-security linux-image-3.13.0-83-generic:amd64/trusty-security linux-image-extra-3.13.0-71-generic:amd64/trusty-security linux-image-extra-3.13.0-74-generic:amd64/trusty-security linux-image-extra-3.13.0-76-generic:amd64/trusty-security linux-image-extra-3.13.0-77-generic:amd64/trusty-security linux-image-extra-3.13.0-79-generic:amd64/trusty-security linux-image-extra-3.13.0-83-generic:amd64/trusty-security
You can't just purge all of these because you will trash your current kernel. So you need to exclude that, preferably using the uname command:
visser@cajones:~$ uname -r 3.13.0-83-generic
So this will yield:
visser@cajones:~$ apt-show-versions -b | grep -E 'linux-(headers|image(-extra)?)-[0-9]+' | grep -v `uname -r` linux-headers-3.13.0-71:all/trusty-security linux-headers-3.13.0-71-generic:amd64/trusty-security linux-headers-3.13.0-74:all/trusty-security linux-headers-3.13.0-74-generic:amd64/trusty-security linux-headers-3.13.0-76:all/trusty-security linux-headers-3.13.0-76-generic:amd64/trusty-security linux-headers-3.13.0-77:all/trusty-security linux-headers-3.13.0-77-generic:amd64/trusty-security linux-headers-3.13.0-79:all/trusty-security linux-headers-3.13.0-79-generic:amd64/trusty-security linux-headers-3.13.0-83:all/trusty-security linux-image-3.13.0-71-generic:amd64/trusty-security linux-image-3.13.0-74-generic:amd64/trusty-security linux-image-3.13.0-76-generic:amd64/trusty-security linux-image-3.13.0-77-generic:amd64/trusty-security linux-image-3.13.0-79-generic:amd64/trusty-security linux-image-extra-3.13.0-71-generic:amd64/trusty-security linux-image-extra-3.13.0-74-generic:amd64/trusty-security linux-image-extra-3.13.0-76-generic:amd64/trusty-security linux-image-extra-3.13.0-77-generic:amd64/trusty-security linux-image-extra-3.13.0-79-generic:amd64/trusty-security
However this will also remove the headers of the current kernel, so we have to strip the -generic part from the uname output:
visser@cajones:~$ apt-show-versions -b | grep -E 'linux-(headers|image(-extra)?)-[0-9]+' | grep -v $(uname -r | sed -E 's/-[a-z]+$//') linux-headers-3.13.0-71:all/trusty-security linux-headers-3.13.0-71-generic:amd64/trusty-security linux-headers-3.13.0-74:all/trusty-security linux-headers-3.13.0-74-generic:amd64/trusty-security linux-headers-3.13.0-76:all/trusty-security linux-headers-3.13.0-76-generic:amd64/trusty-security linux-headers-3.13.0-77:all/trusty-security linux-headers-3.13.0-77-generic:amd64/trusty-security linux-headers-3.13.0-79:all/trusty-security linux-headers-3.13.0-79-generic:amd64/trusty-security linux-image-3.13.0-71-generic:amd64/trusty-security linux-image-3.13.0-74-generic:amd64/trusty-security linux-image-3.13.0-76-generic:amd64/trusty-security linux-image-3.13.0-77-generic:amd64/trusty-security linux-image-3.13.0-79-generic:amd64/trusty-security linux-image-extra-3.13.0-71-generic:amd64/trusty-security linux-image-extra-3.13.0-74-generic:amd64/trusty-security linux-image-extra-3.13.0-76-generic:amd64/trusty-security linux-image-extra-3.13.0-77-generic:amd64/trusty-security linux-image-extra-3.13.0-79-generic:amd64/trusty-security
This looks good. Now we can purge these packages:
visser@cajones:~$ sudo apt-get purge `apt-show-versions -b | grep -E 'linux-(headers|image(-extra)?)-[0-9]+' | grep -v $(uname -r | sed -E 's/-[a-z]+$//')` Reading package lists... Done Building dependency tree Reading state information... Done Selected version '3.13.0-71.114' (Ubuntu:14.04/trusty-updates [all]) for 'linux-headers-3.13.0-71' Selected version '3.13.0-71.114' (Ubuntu:14.04/trusty-updates [amd64]) for 'linux-headers-3.13.0-71-generic' Selected version '3.13.0-74.118' (Ubuntu:14.04/trusty-updates [all]) for 'linux-headers-3.13.0-74' Selected version '3.13.0-74.118' (Ubuntu:14.04/trusty-updates [amd64]) for 'linux-headers-3.13.0-74-generic' Selected version '3.13.0-76.120' (Ubuntu:14.04/trusty-updates [all]) for 'linux-headers-3.13.0-76' Selected version '3.13.0-76.120' (Ubuntu:14.04/trusty-updates [amd64]) for 'linux-headers-3.13.0-76-generic' Selected version '3.13.0-77.121' (Ubuntu:14.04/trusty-updates [all]) for 'linux-headers-3.13.0-77' Selected version '3.13.0-77.121' (Ubuntu:14.04/trusty-updates [amd64]) for 'linux-headers-3.13.0-77-generic' Selected version '3.13.0-79.123' (Ubuntu:14.04/trusty-updates [all]) for 'linux-headers-3.13.0-79' Selected version '3.13.0-79.123' (Ubuntu:14.04/trusty-updates [amd64]) for 'linux-headers-3.13.0-79-generic' Selected version '3.13.0-71.114' (Ubuntu:14.04/trusty-updates [amd64]) for 'linux-image-3.13.0-71-generic' Selected version '3.13.0-74.118' (Ubuntu:14.04/trusty-updates [amd64]) for 'linux-image-3.13.0-74-generic' Selected version '3.13.0-76.120' (Ubuntu:14.04/trusty-updates [amd64]) for 'linux-image-3.13.0-76-generic' Selected version '3.13.0-77.121' (Ubuntu:14.04/trusty-updates [amd64]) for 'linux-image-3.13.0-77-generic' Selected version '3.13.0-79.123' (Ubuntu:14.04/trusty-updates [amd64]) for 'linux-image-3.13.0-79-generic' Selected version '3.13.0-71.114' (Ubuntu:14.04/trusty-updates [amd64]) for 'linux-image-extra-3.13.0-71-generic' Selected version '3.13.0-74.118' (Ubuntu:14.04/trusty-updates [amd64]) for 'linux-image-extra-3.13.0-74-generic' Selected version '3.13.0-76.120' (Ubuntu:14.04/trusty-updates [amd64]) for 'linux-image-extra-3.13.0-76-generic' Selected version '3.13.0-77.121' (Ubuntu:14.04/trusty-updates [amd64]) for 'linux-image-extra-3.13.0-77-generic' Selected version '3.13.0-79.123' (Ubuntu:14.04/trusty-updates [amd64]) for 'linux-image-extra-3.13.0-79-generic' The following packages will be REMOVED: linux-headers-3.13.0-71* linux-headers-3.13.0-71-generic* linux-headers-3.13.0-74* linux-headers-3.13.0-74-generic* linux-headers-3.13.0-76* linux-headers-3.13.0-76-generic* linux-headers-3.13.0-77* linux-headers-3.13.0-77-generic* linux-headers-3.13.0-79* linux-headers-3.13.0-79-generic* linux-image-3.13.0-71-generic* linux-image-3.13.0-74-generic* linux-image-3.13.0-76-generic* linux-image-3.13.0-77-generic* linux-image-3.13.0-79-generic* linux-image-extra-3.13.0-71-generic* linux-image-extra-3.13.0-74-generic* linux-image-extra-3.13.0-76-generic* linux-image-extra-3.13.0-77-generic* linux-image-extra-3.13.0-79-generic* 0 upgraded, 0 newly installed, 20 to remove and 35 not upgraded. After this operation, 1,357 MB disk space will be freed. Do you want to continue? [Y/n]
Removing set of e-mail address from another set of email address
List of address with optional names, separated by whitespace:
visser@rasputin:~$ cat addresses.txt dick@TIENHUIS.nl Dick Visser dnmvisser@Gmail.CoM Dick "Gmail" Visser dnmvisser@yahoo.com dick.visser@geant.org Dick Visser visser@terena.org Dick's old address...
Similar for a blacklist:
visser@rasputin:~$ cat blacklist.txt Dnmvisser@gmail.com Evil guy
You can 'subtract' the blacklist using this Ruby one line:
visser@rasputin:~$ ruby -e 'a = File.readlines("addresses.txt").map { |x| x.split.first.downcase } - File.readlines("blacklist.txt").map { |x| x.split.first.downcase }; puts a' dick@tienhuis.nl dnmvisser@yahoo.com dick.visser@geant.org visser@terena.org