Pages

09 April 2013

Clean (script)



I really do not know if this can be achieved automatically in a different/more effective way. The reason why I wrote this simple script is that I wanted to clean certain directories in my filesystem. The idea started with /tmp but I decided to include other directories such as the Trash bin.
What the script does is: One after the other, it tests whether the directories are empty or not. If they are empty it does nothing. If they contain files the script removes them, including the dot files.
BUGS: The script has, at least, one bug. Since it is set -e it stops after the very last iteration if there are hidden files in /tmp which cannot be removed if you run the script by hand as normal user. That is why I put /tmp in the last place. When it stops, all the other files are already deleted so it does not really matter whether it stops or not.
Why do I use set -e? Because I think that the rm -rf command toghether with a * wildcard is way too dangerous to let it loose.
Why keeping a log file? Because I can :)
Why to log ${DIR} is empty? This way I can adjust cron/anacron timing. For example, if I run it weekly and after a week the ${DIR} is empty, I may prefer to run it after two weeks or so. Do you get the idea? ;)
Why does the log file not include a list of all the deleted files? Who cares about the contents of Trash or tmp anyway? Especially after they have already been deleted.
Why not write a log function instead of redirecting the output to a log file?Because I'm too lazy to do that? Now seriously, the idea was in mind but the script is too simple to do that. Perhaps one day it will get better and grow bigger, and I do finally write that function then. (Note: That is not likely going to happen).
What is the best way to use this script? To tell you the truth, the best way to use this script is simply NOT using it. I mean that it really is very dangerous to remove files like that, hence the 'Big Fat Warning' on the header of the script. But still, if you want to give it a try, first read it, understand what the script does and adapt it to your needs.
Then, I recommend you add the script to, say, /etc/cron.monthly so that it is run by your system on a monthly basis.
Here is the script:
 #!/bin/sh

 set -e

 # Script to clean directories. The ls -A means that it does not list . & ..
 #
 # BIG FAT WARNING: rm -rf is potentially dangerous. Use this at your own risk!

 # Directories to clean.

 TRASH="/home/chals/.local/share/Trash/files" # Trash in Xfce
 DIR_TO_CLEAN1="/home/chals/tmp"
 DIR_TO_CLEAN2="/tmp"

 # Where to write the log file.

 LOG_FILE="/home/chals/clean.log"

 # Let's do it!

 for DIR in ${TRASH} ${DIR_TO_CLEAN1} ${DIR_TO_CLEAN2}
     do
         if [ "$(ls -A ${DIR})" ]
             then
                 rm -rf ${DIR}/* && rm -rf ${DIR}/.??* > /dev/null 2>&1 # Remove dot files too.
                 echo "$(date) Cleaning ${DIR} :) " >> ${LOG_FILE} 
             else
                 echo "$(date) Nothing to be done, ${DIR} is empty :( " >> ${LOG_FILE}
         fi
     done

07 April 2013

Potato

The first potato plant of the year in the kitchen garden.


Mona 2013


On Easter Monday we celebrate Mona's day. The tradition is going out to the countryside and have a picnic with our families and/or friends.

We have lunch and eat an special cake called The mona, hence the name of the celebration. The cakes often include the traditional chocolate eggs and these beautiful coloured feathers.

This year, we did go out to the country but it was really windy and we had to eat inside the cottage by the fireplace but on the bright side we had two cakes this year, one for each nephew




27 January 2013

Carrying debian in the keyring

I always carry a debian-live system in my keyring, it is an image (built by myself using a git repository) that I installed in a USB pendrive.




The device has three partitions. The first one contains the debian-live system itself, the second one is used for persistence and the third one is an extra fat16-formatted partition to copy and move files around easily. Here is the layout:




It is really handy to to be able to carry your operating system with you everywhere you go. You can boot it on almost any machine you have access to, saving your data in the pendrive. It is also a great tool to test hardware before performing an installation (the image includes an installer).

Using a raspi as a switch


Everybody is asking me why the heck I got a raspi. I proudly show it to all my family and friends and they all say:
 "What's this? What's this for?"
(I do not have the energy to explain them what a gopher server is, and why everbody should have, at least, one.)
I imagine that everyone has a different use for their raspis. I am having so much fun playing with it since I received it that I should write a post every single day. But unfortunately I do not have the time to do it :-(
Today I have yet found a new use for it, perhaps one of the best uses I could think of, even though I didn't have that idea in mind when I bought it.
I have a network server that I only use occasionally. I turn it on when I get home if I need it, but I always turn it off at night. One of the reasons to get my raspi was the possibility of having a machine connected 24/7 and also reachable through the internet, for example, using a smart phone.
Today, I was away from home with my netbook (eeepc 701) and I needed to start a build on my server. I thought it would be excellent if I could start the build before getting home and this way the first test would be done by the time I arrived home. Well, suddenly I got the idea:
I often turn the server on using 'wakeonlan', I have got several aliases to mount devices on my network and do other similar things. So using ssh from my eeepc I logged on to my shell account at sdf-eu, from there I connected to my raspi and installed the wakeonlan package (I didn't have it installed there yet) but I had my.shell_aliases in my $HOME. From there I turned on the server and opened a shell to start the build. When I got home the first build had failed and stopped, but it saved me a lot of time because when I arrived I could start another build right away.
I know that the idea is not the state of the art. Many IT guys do most of their work jumping from one server into the other all day long but I'm happy to have come up with such a nice trick: Using the raspi to turn on and off other machines in my network remotely. From now on my raspi will be one server to rule them all.

26 January 2013

Here comes the squid


I have added a new machine to my personal collection. Its hostname is squid following my naming convention. See my Profile for more information.
It has a 64-bit processor (athlon), 2GB of ram and two hard disks. It is a little bit old but it is really fast. Today I installed debian wheezy with the b4 installer. Everything went fine and smoothly.


Welcome to my network, squid :-p

01 January 2013

curlftpfs


Having a raspi on the network is a fantastic thing. It has so many possibilities that it seems endless. One of its constraints, however, is that it has not got a lot of available space. Well, it can have as much space as you can afford but big sd memory cards are expensive (or at least more expensive than the raspi itself) An sd card of 32 GB costs 39€, way more than the raspi itself.
Solutions:
  1. Attach a pendrive (or several of them using a hub) to have a bit more spacehmm similar situation. This might be expensive and even ugly.
  2. Mount a network drive as if it were a local storage device.
I chose option 2. I normally use sshfs to mount drives on my network or even remotely. I have an ftp-only drive that cannot be mounted using ssh. So I had to install curlftpfs. It is extremely easy to use.
  1. Make sure your user belongs to the fuse group. If not:
    $ sudo adduser < user > fuse
  2. Then make a mount point.
    $ mkdir -p ${HOME}/nas
  3. Mount it.
    $ curlftpfs -o allow_other < user >:< password >@192.168.2.30 ${HOME}/nas
Nice huh?
To unmount it:
$ fusermount -u ${HOME}/nas
You can now either add the mount to fstab to make it permanent (not really necessary in my case since the raspi is up 24/7) or create an alias.
alias nasmount='curlftpfs -o allow_other < user >:< password >@192.168.2.30 $HOME/nas'
alias nasumount='fusermount -u ${HOME}/nas'
Now the best part of it. I mount the ftp server in my raspi but I also mount my raspi over ssh in my laptop so in the end I have my ftp server mounted in my laptop out of the box. Neat!!!