SD-Card emergency encrypted backup

By: Javier Santacruz  | Category : Linux

Tags : ecryptfs / backup / ext4 / sdcard /

 

I must confess I am a little bit obsessive about data loss. I am not just obsessed, but paranoid. I like backups. I love them. I do it constantly, automatically, distributed and encrypted. I even write my own backup tools. It's never enough.

I usually have a 500GB 2.5" hard disk connected to my laptop when I'm working at home, and use fcron and my backup.py to take a complete snapshot of the working environment every 5h or so. I also backup my backups on another hard disk, on another city, each time I pass by my family house. I use Dropbox too. I love Dropbox. But I do not trust it, I don't rely on it. I want to keep my data myself, so I replicate the 'Dropbox directory' at my own home server, with regular backups of it, and at my family's PC (with its own backup schema as well). Sensible data is encrypted by using ecryptfs because this way, data happens to be scattered across different places and although barely accessible, it wouldn't take much to a geek with physical access to hardware to get it.

But as I said before, it is never enough. This way of making backups isn't bad, It makes it difficult to completely lose everything. But what happens with last-time files and changes?. I often imagine an scenario (a bad nightmare, really) where I'm travelling, carrying all my stuff with me, and for some stupid reason, my bag falls to the road being swiftly smashed and crunched by an evil double-decker. There is not hard-disk anymore, either the laptop's or the backup one. They're magnetics flying fragments now. Dropbox happened to be unsynced since a day or so of no internet connection, my last backup at home is 3 days old, my sister accidentally removed some backups to make room for a movie and I was being quite productive these past blank hours at the airport. Data loss. Horreur. I wake up screaming, soaked in cold sweat.

I know it's not very wise to move around with data and backups in the same bag, and actually I don't. And I also know that the chance for absolutely everything to fail it's quite low, but you got the point, and remember: I'm paranoid.

Then I was at a store and saw one of those 32Gb tiny flash pen-drives. 32Gb, that's a lot. At least for my important data being about 7Gb. Then I thought I could regularly backup my physical disk backups, not only after some weeks, but every few days, into a pen I'll always carry around inserted in my keyring, performing the copies manually.

But after the first moment, I didn't like the idea. Besides of being a backup-freak, I am, as any other conceited programmer, quite lazy about repetitive tasks. Even further, I'm slack. If there is any task that I must perform completely unchanged more than 10 times, I'll start growing annoyed about the 11th time. Actually I'm being self-indulgent here, let's say 5 times. If I have to remember and manually perform each backup, I will screw it at some copy, forget about it, delay doing them or a combination of the previous. I wanted my flash copies, but I wanted them automated.

Next thing I saw was a 32Gb sd-card. They're unlikely to die in an accident, they even survive being washed along clothes. They have this cell worn out issue, but hey, they're also relatively cheap. It would be nice to have one of those inserted into my machine and perform copies to it. My laptop happens to have one of those integrated card-readers, so I just went straight to Amazon and ordered a class-10 32Gb for about £17.

Laptop integrated SD card-reader

This way I could have an absolutely up-to-date copy of all my data with me, automatically done at each system shutdown or every few hours. It won't help me in case of losing my laptop, because it would be gone with it, but it can definitely save my butt if some drastic hardware failure happens. There are a couple of drawbacks for backing up to a SD card:

  1. Flash cells worn out on write. If you constantly write to a flash memory, you will exhaust it soon.
  2. It is exposed. The **** card suddenly pops out when careless manipulating the laptop. Or someone could take it. It may be lost somewhere, with all my data in it.

Fortunately, (1) incremental backups only writes few Mb each time, so it won't be so bad for cells, and (2) encryption exists for a reason, I can always prevent all it's contents from being taken.

Once I got my card, I moved it from vfat (it has issues with +4Gb files), to ext4 and created a ecryptfs private directory on it.

Once the sdcard has been inserted, it is auto mounted by gnome on /media/4785B20834, so let's get the device number from the mountpoint.

$$ df -h /media/4785B20834 
Filesystem            Size  Used Avail Use% Mounted on
/dev/sdb1              30G   44M   28G   1% /media/4785B20834

Once we know it's /dev/sdb1, (1) unmount it and (2) format it. I will format it using ext4 with no journaling. Journaling helps to keep data integrity, by keeping an index of last recently written files, but it means constantly write a list on the device. We don't want that, remember?

$$ eject /media/4785B20834
# -O ^has_journal disables journaling
# -L sets the volume label
$$ sudo mkfs.ext4 -O ^has_journal /dev/sdb1 -L sdbk

mke2fs 1.41.12 (17-May-2010)
Filesystem label=sdbk
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
1957888 inodes, 7817984 blocks
390899 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=4294967296
239 block groups
32768 blocks per group, 32768 fragments per group
8192 inodes per group
Superblock backups stored on blocks: 
32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208, 
4096000

Writing inode tables: dataone                            
Writing superblocks and filesystem accounting information: 
done

$$ udisks --mount /dev/sdb1
Mounted /org/freedesktop/UDisks/devices/sdb1 at /media/sdbk

Well, we already have the card, let's go for the encryption. ecryptfs allows you to encrypt a certain directory on the fly, storing the data encrypted, and mounting it on at some point on 'clear', so you can work with it normally, while still ciphering file contents underneath one by one and changing its names. ecryptfs can be found in the ecryptfs-utils package.

So let's go for it, first of all (1) create the ecrypt directory by mounting it for the first time, (2) answer the questions that the program raises: I use the aes algorithm, with a 32 bytes key, no to plaintext passthrough and yes to filename encryption. You can see the settings for your new partition when it is created, and (3) take a look to your password's signature (8e85340a483cb3ce) because you'll recognize it in further steps.

    mkdir /media/sdbk/bk
    sudo mount -t ecryptfs /media/sdbk/bk /media/sdbk/bk

    Passphrase: 
    Select cipher: 
->  1) aes: blocksize = 16; min keysize = 16; max keysize = 32 (not loaded)
    2) blowfish: blocksize = 16; min keysize = 16; max keysize = 56 (not loaded)
    3) des3_ede: blocksize = 8; min keysize = 24; max keysize = 24 (not loaded)
    4) twofish: blocksize = 16; min keysize = 16; max keysize = 32 (not loaded)
    5) cast6: blocksize = 16; min keysize = 16; max keysize = 32 (not loaded)
    6) cast5: blocksize = 8; min keysize = 5; max keysize = 16 (not loaded)
    Selection [aes]: 1
    Select key bytes: 
    1) 16
->  2) 32
    3) 24
    Selection [16]: 2
    Enable plaintext passthrough (y/n) [n]: n    <- Nope
    Enable filename encryption (y/n) [n]: y      <- Yep
    Filename Encryption Key (FNEK) Signature [8e85340a483cb3ce]:  
    Attempting to mount with the following options:   ^-- Look!
    ecryptfs_unlink_sigs
    ecryptfs_fnek_sig=8e85340a483cb3ce
    ecryptfs_key_bytes=32
    ecryptfs_cipher=aes
    ecryptfs_sig=8e85340a483cb3ce
    Mounted eCryptfs

Once you have it mounted, let's put some sensible data in it, unmount it and see what happens.

# ex-girlfriend pic
cp ~/Photos/DSC0069.jpg /media/sdbk/bk
ls /media/sdbk/bk
DSC0069.jpg

# umount it
sudo umount /media/sdbk/bk
ls /media/sdbk/bk
ECRYPTFS_FNEK_ENCRYPTED.FWaCVHE8G1mnnUZhhyZe.Z6vRrz42cu8a5gt8p.H7pcoIB8nbl3brC-QfE--

Oh! so when we do not set the password and mount the directory, all we have in there are weird files. Just perfect. If you mount it again with mount -t ecryptfs DIR DIR, you'll have to go through the whole set-up process again and answer algorithms, sizes and options to be able to mount. Second step is to make it mount automatically without having to type our password.

We'll use the gnome-keyring for this. The gnome-keyring will pop out at system start-up asking for the keyring password, and once you have supplied it, applications can request passwords by its signature (ahh! 8e85340a483cb3ce) and use it without user interaction.

First, we'll (1) add the partition to /etc/fstab, so it will mount automatically and configure it so the user can mount it without being root. Once we've got that, we (2) add our password to the gnome-keyring and (3) enjoy mounting/unmounting as we please.

# 1. Mount it and get the config from /etc/mtab
mount -t ecryptfs /media/sdbk/bk /media/sdbk/bk
Passphrase: 
Select cipher: 
1) aes: blocksize = 16; min keysize = 16; max keysize = 32 (not loaded)
2) blowfish: blocksize = 16; min keysize = 16; max keysize = 56 (not loaded)

(... yes, all this again ...)
Mounted eCryptfs
$$ ls /media/sdbk/bk
DSC0069.jpg
$$ grep ecryptfs /etc/mtab
/media/sdbk/bk /media/sdbk/bk ecryptfs rw,ecryptfs_sig=8e85340a483cb3ce,ecryptfs_cipher=aes,
        ecryptfs_key_bytes=32,ecryptfs_fnek_sig=8e85340a483cb3ce,ecryptfs_unlink_sigs 0 0
$$ sudo vim /etc/fstab

We add user to the options, so the user can mount/umount as he wishes. Leaving the file like this

# sdbk
/media/sdbk/bk /media/sdbk/bk ecryptfs user,rw,ecryptfs_sig=8e85340a483cb3ce,ecryptfs_cipher=aes,
        ecryptfs_key_bytes=32,ecryptfs_fnek_sig=8e85340a483cb3ce,ecryptfs_unlink_sigs 0 0

Well, we have it on the /etc/fstab, ready to mount at startup and at anytime we do mount -i. Lets add the key to the keyring to avoid typing it each time.

# 2. add key to the keyring
$$ ecryptfs-manager
eCryptfs key management menu
-------------------------------
    1. Add passphrase key to keyring  <--- this one
    2. Add public key to keyring
    3. Generate new public/private keypair
    4. Exit

Make selection: 1

    Mount-wide passphrase:   <-- type your password
    Confirm passphrase:      <-- do it again
    Using the default salt value      
                                      v---- Look ma! same signature!
Added key to keyring with signature [8e85340a483cb3ce].

Now it's time to test that everything works as expected. Lets (1) unmount it and then (2) mount it with no password annoyance, and finally (3) unmount it the same way.

# umount it as root
$$ sudo umount /media/sdbk/bk
# check whether we can mount it
$$ mount -i /media/sdbk/bk
$$ ls /media/sdbk/bk
DSC0069.jpg
$$ umount /media/sdbk/bk

Got it! This way is easy enough to get encrypted contents in the sdcard. All we have to do now it's automate the backups via fcron. I use fcron and not just cron because its for a laptop. fcron works similar to anacron and remembers where it was at system shutdown. So if I set it to make a backup each 5h, work for 2 and then shut it down, next time it will keep the count and wait just 3h for the next backup.

Options frequency command
2 # if lavg5 <= 0.5 wait 1h max, start inmediately if it had to happen whilst off
3 @lavg5(0.5),until(1h),bootrun(true) 5h python2.6 backup.py --plan .hdbk.conf backup
4 @lavg5(0.5),until(1h),bootrun(true) 4h python2.6 backup.py --plan .sdbk.conf backup

I have it one each 5h and another each 4h. Starting only when system average load is less than 0.5. Backup!

Comments

Other articles

Change email in multiple git commits

Mon 07 May 2012By Javier Santacruz

Translations for this article: es

 

I wanted to replicate an old git repo, in which I used as author my old university-work email, but using my current gmail account as email. Changing it for both commiter and author for all commits involves rewriting the whole repo. It's not difficult at all, but better work ...

Read more...

Activate color in git

Wed 11 April 2012By Javier Santacruz

 

I've got sick of adding the --color flag to each git command I type so I went through the git config documentation and found several color.* options. Those options can be changed by using the git config VAR VALUE command, so initially I ran:

$ for CONF in branch grep ...
Read more...

Recover a lost stashed commit in git

Wed 21 March 2012By Javier Santacruz

Translations for this article: es

 

If it happens that you confuse git stash pop (apply and remove an stashed commit) with git stash drop (simply remove it), do not cry yet. git stash drop yells the hash of the dropped commit, so you still have a chance to recover it.

git stash drop
Dropped refs ...
Read more...

List directories within a dir in C

Tue 06 March 2012By Javier Santacruz

 

When you just only want to list directories within a given directory in C, things can be not so obvious.

Using the non-standard function scandir its quite easy to get all files in a directory. The prototype looks like this:

int scandir(const char *dirp, struct dirent ***namelist,
    int (*filter ...
Read more...

Migrar de gmail a gmail

Sun 19 February 2012By Javier Santacruz

 

Intento migrar mi cuenta de gmail, de una antigua a otra más seria, llevándome todos los contactos, feeds, filtros, calendarios, chats y, por supuesto, los correos. Los correos deben mantener:

  • Leidos
  • Preferidos
  • fechas
  • Origen y destino (no ser un reenvio)
  • Etiquetas (múltiples)

Migrar correo

Probé 4 opciones, gyb, imapsync, gmail-backup ...

Read more...

Generate LaTeX Equations

Sat 18 February 2012By Javier Santacruz

Translations for this article: es

 

latex-logo

While rebuilding this blog with pelican from a xml export of the previous Wordpress version, I had to face how to regenerate the LaTeX equations inserted within some posts.

Wordpress used to take care of the equation generation through the use of a plugin, taking as input the code between ...

Read more...

Renombrar etiqueta usb linux

Wed 08 February 2012By Javier Santacruz

 

En este caso es una tarjeta SD en FAT32 que no tiene label y al montarla Ubuntu la llama 9016-4EF8, lo cual no me dice mucho y me gustaría cambiarlo. La herramienta para cambiar labels de FAT32 se llama mlabel y viene en el paquete mtools.

  1. Averiguamos que dispositivo es ...

Read more...

Generar CSS con Pygmentize

Tue 07 February 2012By Javier Santacruz

 

Para el coloreado de código de este blog empleo la herramienta pygmentize, la cual es capaz de generar un fichero css con el estilo con el que quiero que salga el texto de los programas.

Para generar un estilo usar:

pygmentize -f html -S borland -a .codehilite > pygment.css

Donde ...

Read more...

Exportación de m3u en Python y Bash

Sun 05 June 2011By Javier Santacruz

 

image

Tengo un reproductor MP3 muy sencillo que empleo para correr. No tiene ná, la música se carga en una carpeta interna llamada 'MUSIC' y eso es todo lo que hay que hacer.
El problema es que a Banshee no le da la gana de manejarlo, lo que me obliga a ...

Read more...

Rootear e instalar CyanogenMod en HTC Nexus One

Fri 27 May 2011By Javier Santacruz

 

Rootear e instalar CyanogenMod en un HTC Nexus One

Desde hace casi un año disfruto de un Nexus One con el que me va genial. Sin embargo, hasta ahora no había trasteado demasiado con él, y aprovechando las vacaciones he decidido rootearlo y cambiarle la ROM emigrando hacia un lugar un poco mejor. La ROM original que traía de ...

Read more...

Calcular la nota media ponderada Universidades Españolas.

Wed 09 February 2011By Javier Santacruz

 

image

Los alumnos de la universidad de Cádiz (y andaluces en general) podemos consultar este dato en la sección de información académica de la plataforma para prácticas de empresa Ícaro o acudiendo al Vicerrectorado de Alumnos donde pueden calculárnosla y extendernos un certificado de nota.

Sin embargo, aunque lo podamos consultar ...

Read more...

Edición de PDF

Tue 08 February 2011By Javier Santacruz

 

A la hora de lidiar con burocracia y rellenar formularios, lo ideal es escribir directamente sobre el fichero, de manera que al imprimirlo sea perfecto y no tengamos siquiera un machón de bolígrafo. Es mucho más cómodo, ya que de esta manera pueden editarse en caso de error y volver ...

Read more...

Android Screencast

Thu 03 February 2011By Javier Santacruz

 

image

Estas navidades para una asignatura, "Mobile Technology" desarrollé un pequeño videojuego de prueba para Android. Desde hace unos días vengo buscando la manera de grabar un vídeo del juego sin necesidad de emplear una cámara externa (cutre y baja calidad) o grabar el emulador (el cual va lentísimo). Aún no ...

Read more...

Screencast de aplicación 3D en Linux

Wed 02 February 2011By Javier Santacruz

 

Tras realizar un trabajo para una asignatura en la que escribí una pequeña aplicación en 3D, quería hacer un pequeño video de demo de su funcionamiento. El objetivo era grabar la aplicación mientras corría y añadirle al video algún que otro ...

Read more...

Impresiones sobre la charla de Judi Spiers "The business of making games, the deal and publishing operations"

Thu 18 November 2010By Javier Santacruz

 

En la Universidad de Kingston realizan charlas destinadas a los alumnos que cursan asignaturas de desarrollo de videojuegos, invitando a trabajadores de La Industria a hablar sobre su experiencia en ella.

Hoy venía a hablar a Judi Spiers, desarrolladora que ha trabajado en títulos para PC, Xbox 360 y varias ...

Read more...

Establecer entorno de desarrollo JAVA ME en Linux

Sat 16 October 2010By Javier Santacruz

 

Ahora que estoy metiéndome poco a poco en el mundo Java, estoy aprendiendo a programar para plataformas móviles usando Java Mobile Edition con la versión 2.5 (la 3.0 no es compatible con Linux).
Aunque es muy sencillote, me ha costado un poco encontrar información completa y clara sobre ...

Read more...

Mover el último archivo...

Mon 11 October 2010By Javier Santacruz

 

Una de las cosas tontas en las que perdía más tiempo era a la hora de rescatar un archivo descargado con el navegador. La situación típica era la de estar trabajando en consola, ver que ha terminado una descarga en la barra de tareas (usualmente música) y no poder mover ...

Read more...

Problema con Guake en Ubuntu Maverick 10.10

Thu 07 October 2010By Javier Santacruz

 

image

Ayer me fijé que mi consola de Guake no estaba igual que siempre, sino que fallaba al ejecutar ciertas aplicaciones y la pantalla se desdibujaba cuando empleaba el comando man. En concreto recibía el siguiente error:

WARNING: terminal is not fully functional

Trasteando, comparé la variable de entorno TERM de ...

Read more...

Problemas con syslinux y usb-creator-gtk

Wed 06 October 2010By Javier Santacruz

 

El causante de mis penas

El causante de mis penas

Hoy decidí liarme la manta a la cabeza e instalar la beta de ubuntu 10.10, y para ello intenté hacer un usb autoarrancable empleando usb-creator-gtk, con el que es muy sencillo crear uno a partir de una imagen .iso de una distribución.

Una vez ...

Read more...

Backups en imágenes simuladas mediante el uso de enlaces duros

Sat 04 September 2010By Javier Santacruz

 

Disco Duro

Enfrentado una vez a la situación de tener que hacer backups de una aplicación con muchos gigas en datos importantes, acabé adoptando una manera interesante pero no excesivamente conocida de backup. Antes de meterme a ello, para comprender mejor el problema que resuelve y las ventajas que aporta, haré un ...

Read more...

Cosas que nadie te cuenta del Nexus One

Fri 03 September 2010By Javier Santacruz

 

Nexus One

HTC Nexus One

Hace un par de meses, decidí darme un regalazo y obtuve a buen precio un HTC Nexus One en una portabilidad a Vodafone. Es un teléfono muy potente (Procesador 1ghz) corriendo el sistema operativo Android en su última versión Froyo 2.2.

Cuando lo elegí no pensaba ...

Read more...

I want my vim back.

Mon 30 August 2010By Javier Santacruz

 

Desde hace unos dias en el trabajo debo editar ficheros situados en un servidor antiguo, en concreto la máquina corre una RedHat de 2002 con un Kernel viejuno atrasado unas cuantas versiones... y sin vim instalado.

Ya está, la hemos liado. ¿Como voy a trabajar bien con ficheros remotos sin ...

Read more...

Hello world!

Sun 29 August 2010By Javier Santacruz

 

Mierda, debería tener un blog y poder publicar esto.

La frase anterior ha llegado a ser casi un mantra para mi durante estos últimos meses.

Cada semana, una anécdota o un comentario, una idea feliz o un problema solucionado me ha llevado a sentir que debería escribir un poco sobre ...

Read more...