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. #
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. #
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/>. #
16 # Written by and Copyright (C) Francois Fleuret #
17 # Contact <francois.fleuret@idiap.ch> for comments & bug reports #
18 #########################################################################
20 # This script takes two filenames as arguments, mounts them as luks
21 # volumes, and synchronizes their contents.
23 # IT CHANGES THE CONTENT OF THE SECOND ARGUMENT
28 if [ "$1" == "-h" ] || [ "$1" == "--help" ]; then
30 Usage: rsync-luks.sh <source file> <dest file>
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.
36 Comments and bug reports to francois@fleuret.org
42 [ -f "$1" ] && [ -f "$2" ] || (echo "$0 <source> <dest>" >&2 && exit 1)
44 [ -e "/dev/mapper/crypt-src" ] && (echo "/dev/mapper/crypt-src already exists." >&2 && exit 1)
46 [ -e "/dev/mapper/crypt-dst" ] && (echo "/dev/mapper/crypt-dst already exists." >&2 && exit 1)
48 function exit_handler () {
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
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
60 trap exit_handler EXIT SIGHUP SIGINT SIGQUIT SIGABRT SIGTERM
62 ######################################################################
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}"
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}"
77 ######################################################################
78 # First, show the changes
80 echo "**********************************************************************"
83 rsync -n --itemize-changes --delete --progress -axz "${VOL_SRC}/" "${VOL_DST}/"
85 ######################################################################
86 # Ask for confirmation and synchronize
88 echo "**********************************************************************"
89 echo "* Synchronize [y]/N ?"
93 if [ "${KEY}" == "y" ]; then
94 rsync --itemize-changes --delete --progress -axz "${VOL_SRC}/" "${VOL_DST}/"
96 echo "No synchronization."
99 ######################################################################
101 echo "**********************************************************************"