layout: post title: "Encrypted remote backup with rsync and dm-crypt: Part 2/2" date: 2013-06-16 14:59 comments: true categories: [server, paranoid, shell] cover: /images/cover/avatar.png keywords: backup, ssh, encrypt, encryption, dm-crypt, luks, dm, linux, security description: Encrypt data safely with dm-crypt
So, we have secure remote incremental backup solution here. What about data saved on our backup media (server)? I use dm-crypt -- the standard device-mapper encryption functionality provided by the Linux kernel. I've encrypted my backup partition with an image from my gallery located on my work machine (passphrases could be weak). Learn more about encrypting partitions with a key here. What I need to do before every backup process is to open the encrypted partition. Obviously, after the backup process I close it.
First modprobe kernel module: modprobe dm_mod
.
We need to create encrypted partition for our sensitive data. Assuming we
already have a spare partition you can simply run the command:
cryptsetup -c aes-xts-plain -s 512 luksFormat <volume_to_encrypt>
<secret_keyfile>
What does it mean?
/dev/sda9
Now, here's my solution how to do this:
{% codeblock open and mount an encrypted partition lang:bash %}{% raw %}
scp
What does this bloody script mean?
/dev/<path-to-encrypted-partition>
(for example /dev/sda9
) and call it
for example "nomoresecrets" (name-of-open-partition
). Use copied keyfile as a key.shred
command)./dev/mapper/<name-of-open-partition> /mnt/somewhere ext4 rw,relatime,data=ordered,barrier=0,user,exec,suid,dev,noauto 0 0
All right, now we can access the encrypted partition, read & write data, whatever.
To sum up, there are two different paths to the encrypted devices. First, e.g.
/dev/sda9
(path-to-encrypted-partition) is used only for "luksOpen" operation.
Opened device is located in /dev/mapper/
directory. This path is in the
script above used for mount, umount and mkfs.
Just run these commands on remote machine:
{% codeblock unmount and close encrypted partition %}
ssh user-with-sufficient-rights@remote-machine
"umount /mnt/somewhere
&& cryptsetup luksClose /dev/mapper/
{% endcodeblock %}
Simple, isn't it?