Initial commit.
[scripts.git] / rsync-luks.sh
1 #!/bin/bash
2
3 #########################################################################
4 # This program is free software: you can redistribute it and/or modify  #
5 # it under the terms of the version 3 of the GNU General Public License #
6 # as published by the Free Software Foundation.                         #
7 #                                                                       #
8 # This program is distributed in the hope that it will be useful, but   #
9 # WITHOUT ANY WARRANTY; without even the implied warranty of            #
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU      #
11 # General Public License for more details.                              #
12 #                                                                       #
13 # You should have received a copy of the GNU General Public License     #
14 # along with this program. If not, see <http://www.gnu.org/licenses/>.  #
15 #                                                                       #
16 # Written by and Copyright (C) Francois Fleuret                         #
17 # Contact <francois.fleuret@idiap.ch> for comments & bug reports        #
18 #########################################################################
19
20 # This script takes two filenames as arguments, mounts them as luks
21 # volumes, and synchronizes their contents.
22
23 # IT CHANGES THE CONTENT OF THE SECOND ARGUMENT
24
25 set -e
26 set -o pipefail
27
28 if [ "$1" == "-h" ] || [ "$1" == "--help" ]; then
29     cat <<EOF
30 Usage: rsync-luks.sh <source file> <dest file>
31
32 Mounts both files as LUKS volume and rsync the source to the dest. It
33 first performs a dry-run and then asks for interactive confirmation
34 before synchronizing for real.
35
36 Comments and bug reports to francois@fleuret.org
37
38 EOF
39     exit 0
40 fi
41
42 [ -f "$1" ] && [ -f "$2" ] || (echo "$0 <source> <dest>" >&2 && exit 1)
43
44 [ -e "/dev/mapper/crypt-src" ] && (echo "/dev/mapper/crypt-src already exists." >&2 && exit 1)
45
46 [ -e "/dev/mapper/crypt-dst" ] && (echo "/dev/mapper/crypt-dst already exists." >&2 && exit 1)
47
48 function exit_handler () {
49
50     [ -n "${VOL_SRC+yes}" ] && umount "${VOL_SRC}" && rmdir "${VOL_SRC}" && unset VOL_SRC
51     [ -e "/dev/mapper/crypt-src" ] && cryptsetup luksClose crypt-src
52     [ -n "${LOOP_SRC+yes}" ] && losetup -d "${LOOP_SRC}" && unset LOOP_SRC
53
54     [ -n "${VOL_DST+yes}" ] && umount "${VOL_DST}" && rmdir "${VOL_DST}" && unset VOL_DST
55     [ -e "/dev/mapper/crypt-dst" ] && cryptsetup luksClose crypt-dst
56     [ -n "${LOOP_DST+yes}" ] && losetup -d "${LOOP_DST}" && unset LOOP_DST
57
58 }
59
60 trap exit_handler EXIT SIGHUP SIGINT SIGQUIT SIGABRT SIGTERM
61
62 ######################################################################
63 # Mount the volumes
64
65 LOOP_SRC="$(losetup -f)"
66 losetup "${LOOP_SRC}" "$1"
67 cryptsetup luksOpen "${LOOP_SRC}" crypt-src
68 VOL_SRC="$(mktemp -d /tmp/sync-luks.XXXXXX)"
69 mount /dev/mapper/crypt-src "${VOL_SRC}"
70
71 LOOP_DST="$(losetup -f)"
72 losetup "${LOOP_DST}" "$2"
73 cryptsetup luksOpen "${LOOP_DST}" crypt-dst
74 VOL_DST="$(mktemp -d /tmp/sync-luks.XXXXXX)"
75 mount /dev/mapper/crypt-dst "${VOL_DST}"
76
77 ######################################################################
78 # First, show the changes
79
80 echo "**********************************************************************"
81 echo "* Dry-run"
82
83 rsync -n --itemize-changes --delete --progress -axz "${VOL_SRC}/" "${VOL_DST}/"
84
85 ######################################################################
86 # Ask for confirmation and synchronize
87
88 echo "**********************************************************************"
89 echo "* Synchronize [y]/N ?"
90
91 read -n 1 KEY
92
93 if [ "${KEY}" == "y" ]; then
94     rsync --itemize-changes --delete --progress -axz "${VOL_SRC}/" "${VOL_DST}/"
95 else
96     echo "No synchronization."
97 fi
98
99 ######################################################################
100
101 echo "**********************************************************************"