If you have one Raspberry Pi always on or running several hours every day, probably you also had some SD card faults.
Unfortunately SD cards have a limited number of writings allowed. After that number the internal electronics is no more reliable and there could be read/write errors.
Debian usually do several writings on the os partitions, so the maximum writing cycles is rapidly reached.
So, what could we do to make the sd last longer with our RPi?
Actually we can really do something. Here I will just list the most useful tricks, but there's more that could be done, like using an usb disk for the whole system and leaving the SD just for booting up the Raspberry.
First of all we need space. Latest debian versions have a nice feature to reduce the number of writing cycles to the same storage address, by simply selecting different address to be written every time.
With few space available, every address in the SD will be rewritten more often than having much space.
So one good thing to do is using big SD cards.
Now let's go to the software part of this post. Note that before proceeding you should really backup the SD, just in case something goes wrong. Actually it would be good to make regular backups of your cards, even after these mods as we are making cards last longer, not forever!
Disabling disk swapping is quite useful, especially if you do not use the gui. To disable it just type:
sudo swapoff --all
If you wish, you can also remove the swap file manager by typing
sudo apt-get remove dphys-swapfile
sudo update-rc.d -f dphys-swapfile stop
sudo update-rc.d -f dphys-swapfile remove
Now open fstab (this file is quite critical, so be careful when modifying it as you can hang the system):
sudo nano /etc/fstab
You should see something like this:
proc /proc proc defaults 0 0
/dev/mmcblk0p1 /boot vfat defaults 0 2
/dev/mmcblk0p2 / ext4 defaults,noatime 0 1
It's important to have the noatime flag in the /dev/mmcblk0p2 device. This means that when you access a file (even if just reading) the system will not update the last accessed time field.
If your system is up to date, then you should already have that flag. Otherwise you can just add it.
Now you can add the following at the bottom of the file:
tmpfs /tmp tmpfs defaults,noatime,nosuid,size=100m 0 0
tmpfs /var/tmp tmpfs defaults,noatime,nosuid,size=30m 0 0
tmpfs /var/run tmpfs defaults,noatime,nosuid,mode=0755,size=2m 0 0
tmpfs /var/log tmpfs defaults,noatime,nosuid,mode=0755,size=100m 0 0
Then you can save the file and reboot.
These added lines just mean to use the ram for those folders instead of the SD card. The specified folders are heavily used by the system, so this way we have a huge cut of the writings to the card and this is exactly what we need.
There is one important thing to note for the latest row above. The /var/log folder is use for storing all the logs. Simple logs, warnings and errors are all stored here, but if you use the ram to store them, you loose all the logs at every boot.
This is bad if you are developing something as you could need to look inside the logs to find errors or something even after several reboots, so you could choose to not add this line, at least until your system is ready to be used.
Finally, if you do not use the gui, you can just get rid of it. To completely remove it you can type:
sudo apt-get remove --dry-run --auto-remove --purge libx11-.*
This will free a lot of space and as we saw it could help to make the SD last a bit longer.