Showing posts with label Backup. Show all posts
Showing posts with label Backup. Show all posts

Nov 22, 2011

TrueCrypt under the command line

I have an external hard drive (LG XD3, 500 GB) broken up into a couple of partitions, 450 and 50 GB respectively. The first partition is public and formatted with NTFS. The second one is formatted with ext4 and encrypted by means of TrueCrypt, and it is where I store my private data.

So far, I used TrueCrypt into graphical mode, but over time, I realize that it is more comfortable to handle the command line version (aside from I tend to rule out any kind of graphical tool whenever possible).

TrueCrypt is a powerful program which may cypher partitions, logical volumes, whole hard drives or even installed operating systems. The encryption is transparently and automatically carried out, and on top of all that, on real time (that is to say, on the fly). Another plus is the option to hide volumes and its performance, which is excellent.

One practical detail of TrueCrypt is that is not necessary to install it on the system. To that end, you have to download the Console-only-32-bit file (in my case, the 32-bit version), decompress the included binary and run it. Then, you will have to choose the second option: Extract package file truecrypt_7.1_console_i386.tar.gz and place it to /tmp. Within this tgz file is located the executable file of TrueCrypt.

I get used to drop off this binary file into the public partition of the external hard drive. Thereby, when I have to use it, I just have to get it from there.

javi@javi-ubuntu:/tmp$ cp /media/public/truecrypt/truecrypt . ; chmod +x truecrypt

javi@javi-ubuntu:/tmp$ ./truecrypt --version
TrueCrypt 7.1

First of all, I had to encrypt the partition. This is a long process and depends on the size of your partition. Below you may appreciate that the average speed was 26 MB/s.

In the next output, you can see that in order to create the cyphered partition (sdb2), I followed the text wizard provided by TrueCrypt. Other choice would have been to pass the parameters through the command line (--encryption, --size, etc.).

javi@javi-ubuntu:/tmp$ sudo ./truecrypt -c
Volume type:
 1) Normal
 2) Hidden
Select [1]: 1

Enter volume path: /dev/sdb2

Encryption algorithm:
 1) AES
 2) Serpent
 3) Twofish
 4) AES-Twofish
 5) AES-Twofish-Serpent
 6) Serpent-AES
 7) Serpent-Twofish-AES
 8) Twofish-Serpent
Select [1]: 1

Hash algorithm:
 1) RIPEMD-160
 2) SHA-512
 3) Whirlpool
Select [1]: 1

Filesystem:
 1) None
 2) FAT
 3) Linux Ext2
 4) Linux Ext3
 5) Linux Ext4
Select [2]: 5

Enter password: 
Re-enter password: 

Enter keyfile path [none]: 

Please type at least 320 randomly chosen characters and then press Enter:


Done: 100.000%  Speed:   26 MB/s  Left: 0 s                

The TrueCrypt volume has been successfully created.

Once you have created the encrypted partition (remember that my example is based on a partition, but you can also cypher a file or logical volume), the procedure is pretty easy. When you want to work with that safe area, you only have to mount it by means of TrueCrypt.

javi@javi-ubuntu:/tmp$ mkdir /mnt/truecrypt

javi@javi-ubuntu:/tmp$ sudo ./truecrypt /dev/sdb2 /mnt/truecrypt
Enter password for /dev/sdb2: 
Enter keyfile [none]: 
Protect hidden volume (if any)? (y=Yes/n=No) [No]:

javi@javi-ubuntu:/tmp$ ./truecrypt --list
1: /dev/sdb2 /dev/mapper/truecrypt1 /mnt/truecrypt

By running the following command, you may collect more details about a mounted volume.

javi@javi-ubuntu:/tmp$ ./truecrypt --volume-properties /dev/sdb2
Slot: 1
Volume: /dev/sdb2
Virtual Device: /dev/mapper/truecrypt1
Mount Directory: /mnt/truecrypt
Size: 50.0 GB
Type: Normal
Read-Only: No
Hidden Volume Protected: No
Encryption Algorithm: AES
Primary Key Size: 256 bits
Secondary Key Size (XTS Mode): 256 bits
Block Size: 128 bits
Mode of Operation: XTS
PKCS-5 PRF: HMAC-RIPEMD-160
Volume Format Version: 2
Embedded Backup Header: Yes

You can dismount it by executing the next order.

javi@javi-ubuntu:/tmp$ sudo ./truecrypt --dismount /mnt/truecrypt

TrueCrypt has got many more options through the command line. I invite you to take a look at them by checking its help.

And finally, I would like to conclude this article by writing down the order (based on rsync) that I usually run to back up my data into the private partiton.

javi@javi-ubuntu:~$ rsync -altgvb --delete /data /mnt/truecrypt/


Jul 1, 2009

Copias de seguridad con rsync

Rsync es una herramienta muy utilizada en el mundo Linux para hacer copias de seguridad o backups, tanto de ficheros como de directorios.

Una de las principales ventajas de esta aplicación es que permite realizar copias incrementales: cada uno de los ficheros sobre los que se aplica un backup es dividido por rsync en diferentes bloques, obteniendo de cada uno de ellos una suma. Este checksum agilizará enormemente las ejecuciones de rsync, ya que cuando tenga que hacer un backup por segunda vez sobre un mismo conjunto de archivos, lo que hará será comprobar las sumas de los diferentes bloques (operación muy rápida en tiempos de cómputo), y aquellos bloques de datos cuyo checksum no coincida serán los que realmente se copien, es decir, realizará una copia de seguridad por incrementos.

Vamos a suponer que se quiere hacer un backup de todo el directorio de configuración de Apache y depositarlo en /backup/ (las pruebas se realizarán sobre una distribución CentOS 5.3).
# tree /etc/httpd/
/etc/httpd/
|-- conf
| |-- httpd.conf
| `-- magic
|-- conf.d
| |-- README
| |-- logserver.conf
| |-- proxy_ajp.conf
| `-- welcome.conf
|-- logs -> ../../var/log/httpd
|-- modules -> ../../usr/lib64/httpd/modules
`-- run -> ../../var/run

5 directories, 6 files

Para realizar la copia de seguridad se ejecutará rsync con las siguientes opciones:
  • -a, --archive: aplicar recursión a los directorios y conservar la mayoría de los permisos.
  • -l, --links: conservar los enlaces simbólicos de los archivos en el destino.
  • -t, --times: conservar la fecha de los archivos en el destino.
  • -g, --group: conservar el grupo de los archivos en el destino.
  • -v, --verbose: incrementar la información mostrada por pantalla durante la transferencia.
  • -b, --backup: realizar copias de seguridad incrementales.
  • --no-whole-file: no copiar el fichero entero. Para archivos locales, rsync usa por defecto la opción -W (--whole-file), ya que considera que los recursos empleados para calcular la diferencia son mucho mayores que el hecho de copiar el fichero entero.
Otra opción bastante interesante es --delete, que borra aquellos ficheros ya existentes en el destino pero que no se encuentren en el origen.
# rsync -altgvb /etc/httpd/ /backup/
building file list ... done
logs -> ../../var/log/httpd
modules -> ../../usr/lib64/httpd/modules
run -> ../../var/run
conf.d/
conf.d/README
conf.d/logserver.conf
conf.d/proxy_ajp.conf
conf.d/welcome.conf
conf/
conf/httpd.conf
conf/magic

sent 49565 bytes received 182 bytes 99494.00 bytes/sec
total size is 48979 speedup is 0.98

Como puede observarse en la salida anterior, se han copiado un total de 49565 bytes.

Ahora añadiremos cuatro caracteres al fichero httpd.conf y a continuación volveremos a aplicar la copia de seguridad:
# echo "abcd" >> /etc/httpd/conf/httpd.conf

# rsync -altgvb --no-whole-file /etc/httpd/ /backup/
building file list ... done
conf/httpd.conf

sent 617 bytes received 342 bytes 1918.00 bytes/sec
total size is 48984 speedup is 51.08

Puede observarse que no se han enviado todos los archivos, sino simplemente el bloque o incremento que se ha visto afectado: 617 bytes.

Al realizar las copias hay que tener en cuenta si el directorio origen se finaliza o no con la barra:
# rsync -altgvb --no-whole-file /etc/httpd /backup/
# rsync -altgvb --no-whole-file /etc/httpd/ /backup/

La primera opción copia el directorio httpd y la segunda el contenido del directorio httpd.

Y ya por último, decir que tenemos la posibilidad de hacer backups en una máquina remota:
# rsync -altgvb --no-whole-file /etc/httpd root@192.168.1.20:/backup/

Para que el comando anterior no nos pida la clave cada vez que se ejecute, es conveniente configurar el cliente y el servidor SSH implicados a través de claves RSA.