Created
June 29, 2012 13:55
-
-
Save alab1001101/3018086 to your computer and use it in GitHub Desktop.
Backup on encrypted lvm partitions with external HD
This file contains hidden or 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/bash | |
# | |
# Credits go to | |
# http://earlruby.org/2010/02/adding-an-external-encrypted-drive-with-lvm-to-ubuntu-linux/ | |
if [[ root != $(whoami) ]] ; then | |
sudo $0 $@ | |
exit 0 | |
fi | |
DISKID=$(find /dev/disk/by-id -name *WD_My_Book*part1) | |
DISK=$(readlink -e $DISKID) | |
LUKSNAME=crypt-backup-wd | |
VG=backup | |
LV=suse | |
MP=/mnt/backup | |
export BUPPATHS="/etc /home /opt /root /srv /usr/local" | |
export BUPINDEX=$MP/${LV}.bupindex | |
export BUPDIR=$MP/$LV | |
#function backup_setup() { | |
# cryptsetup --verbose --verify-passphrase luksFormat $DISK | |
# cryptsetup luksOpen $DISK $LUKSNAME | |
# | |
# pvcreate /dev/mapper/$LUKSNAME | |
# vgcreate $VG /dev/mapper/$LUKSNAME | |
# lvcreate -L 1T -n $LV /dev/$VG | |
# mkfs.ext4 /dev/$VG/$LV | |
#} | |
backup_mount() { | |
cryptsetup luksOpen $DISK $LUKSNAME | |
vgchange -ay $VG | |
mount /dev/$VG/$LV $MP | |
return $? | |
} | |
backup_umount() { | |
sync | |
umount /dev/$VG/$LV | |
vgchange -an $VG | |
cryptsetup luksClose $LUKSNAME | |
return $? | |
} | |
ensure_mounted() { | |
mount --fake /dev/$VG/$LV $MP >/dev/nul 2&>1 | |
[[ 0 = $? ]] && backup_mount | |
} | |
backup_bup_init() { | |
ensure_mounted | |
if [[ ! -d $BUPDIR ]] ; then | |
mkdir -p $BUPDIR | |
fi | |
bup -d $BUPDIR init | |
bup -d $BUPDIR index -uf $BUPINDEX $BUPPATHS | |
} | |
backup_print_modified() { | |
ensure_mounted | |
bup -d $BUPDIR index -vmf $BUPINDEX $BUPPATHS | |
} | |
backup_full() { | |
ensure_mounted | |
bup -d $BUPDIR index -uf $BUPINDEX $BUPPATHS | |
bup -d $BUPDIR save -t -f $BUPINDEX $BUPPATHS | |
} | |
# ffbf4ea03f2ad9b41f71d7d8f748bdd667c19d15 | |
backup_ftp() { | |
ensure_mounted | |
bup -d $BUPDIR ftp | |
} | |
backup_bupdir() { | |
echo $BUPDIR | |
} | |
if [[ 0 = $# ]] ; then | |
RUN="print_modified" | |
else | |
RUN="$@" | |
fi | |
for cmd in $RUN; do | |
backup_$cmd | |
ret=$? | |
if [[ ! 0 = $ret ]] ; then | |
exit $ret | |
fi | |
done | |
exit 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment