Skip to content

Instantly share code, notes, and snippets.

@atrakic
Last active December 25, 2017 16:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save atrakic/93bc82ff9dec102328bdecdf4dcce2ce to your computer and use it in GitHub Desktop.
Save atrakic/93bc82ff9dec102328bdecdf4dcce2ce to your computer and use it in GitHub Desktop.
#!/bin/sh
set -eo
echo "This script creates rsnapshot_root btrfs enabled. Pls edit me $(basename $0) before proceed."
exit 0
NAME=foo
FILE=/.$NAME.fs
FSIZE=1
DIR=/mnt/$NAME
KEYDIR=/root
logical=crsnapshot
RESTOREDIR=/mnt/restore
###
cryptmount=$(which cryptmount)
if [ -z "$cryptmount" ]; then echo "sorry, no cryptmount installed"; exit; fi
btrfs=$(which btrfs)
if [ -z "$btrfs" ]; then echo "sorry, no btrfs-tools installed"; exit; fi
echo "generating $FILE (takes while) ..."
dd if=/dev/zero of=$FILE bs=1G count=$FSIZE
cp -f /etc/cryptmount/cmtab /etc/cryptmount/cmtab.$(date -I).bak
cat >> /etc/cryptmount/cmtab <<EOF
$NAME {
dev=$FILE
dir=$DIR
fstype=btrfs
mountoptions=noatime
cipher=aes
keyformat=builtin
keyfile=$KEYDIR/$NAME.key
flags=user,nofsck
}
EOF
echo "generating key: $NAME .."
$cryptmount --generate-key 32 $NAME
$cryptmount --prepare $NAME
# exit 0
echo "making btrfs on: $NAME ..."
mkfs.btrfs /dev/mapper/$NAME
mkdir -p $DIR
mount -t btrfs -o noatime /dev/mapper/$NAME $DIR
echo
echo
echo "creating btrfs subvolumes on: $DIR: "
$btrfs subvolume create $DIR/$logical
for vol in hourly.0 hourly.1 hourly.2 hourly.3 hourly.4 weekly.0 weekly.1 monthly.0 monthly.1; do
$btrfs subvolume create $DIR/$logical/$vol
done
$btrfs filesystem label $DIR $NAME
$btrfs fi df $DIR
$btrfs subvolume list $DIR
# exit 0
# /etc/fstab generate:
# btrfs filesystem show /crypt
# btrfs filesystem show
# blkid /dev/mapper/crsnapshot
# $btrfs subvolume list $DIR
# MUUID=$(lsblk --fs /dev/mapper/crsnapshot)
# btrfs subvolume list -t /mnt/foo/rsnapshot
FSTAB_CONF=/etc/fstab
echo "" >> "$FSTAB_CONF"
for vol in $($btrfs subvolume list -t $DIR/$logical/ | awk 'NR>2 { print $4 }');
do
volid=$(btrfs subvolume list -t $DIR/$logical/ | grep $vol| awk '{ print $1 }')
mkdir -p $RESTOREDIR/$vol
cat >> "$FSTAB_CONF" <<EOF
/dev/mapper/$NAME $RESTOREDIR/$vol btrfs noauto,noatime,ro,nospace_cache,subvolid=${volid} 0 0
EOF
mount $RESTOREDIR/$vol
done
echo "" >> "$FSTAB_CONF"
# create rsnapshot config:
BINDIR=/etc/cryptmount/bin
mkdir -p $BINDIR
push $BINDIR
wget -c https://gist.githubusercontent.com/wwerther/1306189/raw/35d00ec6fa4a1b49273bcafdf6cbbe8b37ad0045/rsnapshot_plug_rm_btrfs
wget -c https://gist.githubusercontent.com/wwerther/1306185/raw/5533c429dbbd7f5b703c3ee051ebf8e5f4e6904d/rsnapshot_plug_cp_btrfs
chmod +x /etc/cryptmount/bin/*
popd
RSNAPSHOT_CONF=$DIR/rsnapshot.conf
#cat >> "$RSNAPSHOT_CONF" <<EOF
#snapshot_root $DIR/$logical
#no_create_root 1
#cmd_cp /etc/cryptmount/bin/rsnapshot_plug_cp_btrfs
#cmd_rm /etc/cryptmount/bin/rsnapshot_plug_rm_btrfs
#logfile $DIR/$logicalt/rsnapshot.log
#backup /home/ localhost/
#EOF
# rsnapshot -t -c "$RSNAPSHOT_CONF"
# rsnapshot -v hourly -c "$RSNAPSHOT_CONF"
# exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment