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:

 

bofh@web:~$ ls -la /lib/modules/
total 44
drwxr-xr-x 11 root root 4096 Jun 14 06:34 .
drwxr-xr-x 19 root root 4096 May 31 06:50 ..
drwxr-xr-x  4 root root 4096 Feb  8 01:58 3.2.0-37-virtual
drwxr-xr-x  4 root root 4096 Feb 22 06:54 3.2.0-38-virtual
drwxr-xr-x  4 root root 4096 Mar 19 06:42 3.2.0-39-virtual
drwxr-xr-x  4 root root 4096 Apr  9 06:45 3.2.0-40-virtual
drwxr-xr-x  4 root root 4096 May  2 06:45 3.2.0-41-virtual
drwxr-xr-x  4 root root 4096 May 16 06:54 3.2.0-43-virtual
drwxr-xr-x  4 root root 4096 May 24 06:26 3.2.0-44-virtual
drwxr-xr-x  4 root root 4096 May 31 06:50 3.2.0-45-virtual
drwxr-xr-x  4 root root 4096 Jun 14 06:35 3.2.0-48-virtual

You should do some house cleaning, by removing all old kernels, headers, and manually compiled modules. Given that you run 48, and the oldest one is 37, this one liner does it for you:

for i in `seq 37 47`; do apt-get -yy purge linux-image-3.2.0-$i-virtual linux-headers-3.2.0-$i; rm -rfv /lib/modules/3.2.0-$i-virtual; done

 

 

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