Created
March 9, 2015 12:39
-
-
Save Nilhcem/6f00b08f7c5cda269759 to your computer and use it in GitHub Desktop.
Automatically sync your data into truecrypt containers
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
# Synchronize files into truecrypt containers using tcplay | |
DROPBOX_FOLDER="/home/USER_NAME/Dropbox/" | |
CONTAINER_PASSWORD="YOUR_TRUECRYPT_PASSWORD" | |
MOUNT_DIR="/tmp/backup_dropbox.tc" | |
MOUNT_OPTS="nodev,nosuid,rw,noatime,umask=000,uid=0,gid=0" | |
MAPPING_NAME="dropbox.tc" | |
RSYNC_BIN="/usr/bin/rsync" | |
RSYNC_OPTS="-rptqz --delete-before --force --ignore-errors" | |
ERROR_FILE=${DROPBOX_FOLDER}"ERRORS.txt" | |
ERROR_MORE_MSG="\nCheck /root/scripts/backup/dropbox/backup_dropbox.sh for more information." | |
ERROR_MOUNT_MSG="Failed mounting file."${ERROR_MORE_MSG} | |
ERROR_SIZE_MSG="File limit exceeded."${ERROR_MORE_MSG} | |
# Must be run as root | |
if [[ $EUID != 0 ]]; then | |
printf "%s\n" "You must be root to run this." | |
exit 1 | |
fi | |
mkdir -p ${MOUNT_DIR} | |
dropbox_sync () { | |
# Get parameters | |
CONTAINER=${DROPBOX_FOLDER}$1 | |
shift | |
SYNC_SRCS=$* | |
# Mount encrypted container | |
LOOP_DEV=$(losetup -f) | |
losetup ${LOOP_DEV} ${CONTAINER} | |
expect -c "spawn tcplay -m ${MAPPING_NAME} -d ${LOOP_DEV} | |
set timeout 2 | |
expect Passphrase | |
send \"$CONTAINER_PASSWORD\r\" | |
expect eof | |
" 1>/dev/null | |
mount -o ${MOUNT_OPTS} /dev/mapper/${MAPPING_NAME} ${MOUNT_DIR} | |
# Synchronize data (making sure the device is already mounted) | |
if [ "$(mount | grep ${MOUNT_DIR})" ]; then | |
# Synchronize files in container | |
for SYNC_SRC in ${SYNC_SRCS}; do | |
${RSYNC_BIN} ${RSYNC_OPTS} ${SYNC_SRC} ${MOUNT_DIR} | |
if [ $? -ne 0 ]; then | |
echo -n ${CONTAINER}": " >> ${ERROR_FILE} | |
echo -e ${ERROR_SIZE_MSG} >> ${ERROR_FILE} | |
fi | |
done | |
# Umount container | |
umount ${MOUNT_DIR} | |
else | |
echo -n ${CONTAINER}": " >> ${ERROR_FILE} | |
echo -e ${ERROR_MOUNT_MSG} >> ${ERROR_FILE} | |
fi | |
# Umount devices (waiting before to make sure device is not busy) | |
sleep 1 | |
dmsetup remove ${MAPPING_NAME} --retry | |
losetup -d ${LOOP_DEV} | |
} | |
create_container () { | |
# Get parameters | |
CONTAINER=${DROPBOX_FOLDER}$1 | |
SIZE=$2 | |
echo "Creating container: "${CONTAINER} | |
# Create directory | |
mkdir -p $(dirname ${CONTAINER}) | |
# Allocate file and create encrypted container | |
echo "> Allocate file..." | |
LOOP_DEV=$(losetup -f) | |
fallocate -l ${SIZE} ${CONTAINER} | |
losetup ${LOOP_DEV} ${CONTAINER} | |
echo "> Create truecrypt container..." | |
expect -c "spawn tcplay -c -d ${LOOP_DEV} -a whirlpool -b AES-256-XTS | |
set timeout 2 | |
expect Passphrase | |
send \"$CONTAINER_PASSWORD\r\" | |
expect Repeat | |
send \"$CONTAINER_PASSWORD\r\" | |
expect proceed | |
send y\r | |
interact | |
" | |
# Mount container and create filesystem | |
echo "> open truecrypt container..." | |
expect -c "spawn tcplay -m ${MAPPING_NAME} -d ${LOOP_DEV} | |
set timeout 2 | |
expect Passphrase | |
send \"$CONTAINER_PASSWORD\r\" | |
expect eof | |
" | |
echo "> create filesystem (ntfs)..." | |
mkfs.ntfs -Q /dev/mapper/${MAPPING_NAME} | |
echo "> mount container..." | |
mount /dev/mapper/${MAPPING_NAME} ${MOUNT_DIR} | |
# Umount device | |
if [ "$(mount | grep ${MOUNT_DIR})" ]; then | |
echo "> umount container..." | |
umount ${MOUNT_DIR} | |
fi | |
sleep 1 | |
echo "> free loop device" | |
dmsetup remove ${MAPPING_NAME} --retry | |
losetup -d ${LOOP_DEV} | |
echo "done!" | |
} | |
display_errors_if_any () { | |
if [ -f ${ERROR_FILE} ]; then | |
cat ${ERROR_FILE} | |
exit 1 | |
else | |
exit 0 | |
fi | |
} | |
# The first time you can create a container with: | |
#create_container "my_container.tc" "200M" | |
# Then you can simply run the following: | |
#FROM=" | |
# /home/user/dir1 | |
# /home/user/dir2 | |
# " | |
# TO="my_container.tc" | |
# dropbox_sync ${TO} ${FROM} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment