Commit 0bc16dde authored by Leo Leung's avatar Leo Leung
Browse files

Merge branch 'master' of https://git.steamr.com/leo/zfs-backup

parents 779dacf2 80b0f2b3
......@@ -8,9 +8,21 @@ LABEL=""
# Local config
LocalIP=`ip a | grep inet | awk '{print $2}' | grep -vE '127|::|fd00|fe80' | sed 's/\/[0-9]*//' `
TransferPort=8000
TransferPort=8001
function check_transfer_port() {
while true ; do
# Ensure the port we're using isn't in use
if netstat -lnp | grep -q ":$TransferPort " ; then
TransferPort=$(($TransferPort + 1))
else
break
fi
done
}
function netcat_listen() {
# If you are using gnu netcat
if uname -a|grep -q aarch ; then
......@@ -150,15 +162,17 @@ function backup_volume() {
create_remote_snapshot $Volume "backup-$LABEL-$NextSnapshotId"
fi
# Check ports and adjust if required
check_transfer_port
log "Transferring data on port $TransferPort"
# Initiate the snapshot transfer.
# We replicate all child snapshots as well (-R)
if [ -z "$NextSnapshotId" ] ; then
log "Sending full snapshot for $Volume@backup-$LABEL-$OriginSnapshotId to $LocalVolumeName"
$(sleep 3 && ssh $SSHAUTH "sudo zfs send -R $Volume@backup-$LABEL-$OriginSnapshotId | nc $LocalIP $TransferPort") &
$(sleep 3 && ssh $SSHAUTH "sudo zfs send $Volume@backup-$LABEL-$OriginSnapshotId | nc $LocalIP $TransferPort") &
else
log "Sending incremental snapshot ($OriginSnapshotId to $NextSnapshotId), for $Volume@backup-$LABEL-$OriginSnapshotId to $LABEL/$LocalVolumeName"
$(sleep 3 && ssh $SSHAUTH "sudo zfs send -R -i $Volume@backup-$LABEL-$OriginSnapshotId $Volume@backup-$LABEL-$NextSnapshotId | nc $LocalIP $TransferPort") &
$(sleep 3 && ssh $SSHAUTH "sudo zfs send -i $Volume@backup-$LABEL-$OriginSnapshotId $Volume@backup-$LABEL-$NextSnapshotId | nc $LocalIP $TransferPort") &
fi
# The actual transfer happens here
......@@ -236,8 +250,8 @@ function create_remote_snapshot() {
# Delete the snapshot if it already exists
ssh $SSHAUTH "zfs list -t snapshot $Volume 2>/dev/null | grep -q $Snapshot" && ssh $SSHAUTH "sudo zfs destroy $Volume@$Snapshot"
# Create the new snapshot, -r for recursive
ssh $SSHAUTH "sudo zfs snapshot -r $Volume@$Snapshot"
# Create the new snapshot
ssh $SSHAUTH "sudo zfs snapshot $Volume@$Snapshot"
}
function check_zpool_exists() {
......
......@@ -4,7 +4,7 @@
Disk="$1" # sda
Label="$2" # disk2
if [[ "$Disk" == "-h" ]] || [[ "$Disk" == "--help" ]] || [[ $# -ne 2 ]] ; then
if [[ $# -ne 2 ]] || [[ "$Disk" == "-h" ]] || [[ "$Disk" == "--help" ]] ; then
echo "Usage: $0 device label"
exit
fi
......@@ -19,6 +19,11 @@ if ! modprobe zfs ; then
exit
fi
if [ -z "$Label" ] ; then
echo "Please provide a disk label."
exit
fi
lsblk /dev/$Disk
echo "Everything on this device will be wiped. Hit enter to continue."
......@@ -64,16 +69,28 @@ if [ ! -b /dev/${Disk}1 ] || [ ! -b /dev/${Disk}2 ]; then
exit
fi
# Wait for partitions to show up
sleep 1
echo "Making filesystems..."
echo "Making filesystem on /dev/${Disk}1"
dd if=/dev/zero of=/dev/${Disk}1 bs=4K status=progress
mkfs.vfat /dev/${Disk}1
zpool create -f $Label /dev/${Disk}2
zfs set compress=lz4 $Label
mkdir -p /mnt/$Disk
mount /dev/${Disk}1 /mnt/$Disk
echo mount /dev/${Disk}1 /mnt/$Disk
mount /dev/${Disk}1 /mnt/$Disk || exit
echo "$Label" > /mnt/$Disk/label
echo "nas.home.steamr.com:fast/testing" > /mnt/$Disk/volumes
echo "Making zpool '$Label' on /dev/${Disk}2"
dd if=/dev/zero of=/dev/${Disk}2 bs=4K count=1000 status=progress
zpool create -f $Label /dev/${Disk}2
zfs set compress=lz4 $Label
echo "Leaving /mnt/$Disk mounted."
echo "Edit /mnt/$Disk/volumes with volumes you want to sync."
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment