Made some cosmetic changes. The script works. As already stated, it just makes full snapshots of my @ and @home subvolumes and then sends them to my external USB HDD. But that's enough for now. Here is the 'latest version' of the script:
The DURATION variable almost certainly can be written better, but I spent a reasonable amount of time trying to do so, and failed. Using the TIMESTART, TIMESTOP, STARTCLOCK, and STOPCLOCK variables was the only way I could get it to work. Maybe smarter folk here can see how to do it 'cleaner'.
#!/bin/bash
# snapshot.sh
#
# Variable declarations
#
NOW=$(date +%Y%m%d%H%M)
MKSNAP1="btrfs su snapshot -r /mnt/@ /mnt/snapshots/@$NOW"
MKSNAP2="btrfs su snapshot -r /mnt/@home /mnt/snapshots/@home$NOW"
SNDSNAP1="btrfs send /mnt/snapshots/@$NOW | btrfs receive /backup"
SNDSNAP2="btrfs send /mnt/snapshots/@home$NOW | btrfs receive /backup"
TIMESTART=$( date '+%H:%M:%S' )
STARTCLOCK=$(date -u -d "$TIMESTART" +"%s")
# To be run as root from /
#
# Before executing this script:
# Plug in external HDD. Ignore Disk & Devices pop-up.
# External drive will be powered and identified by system but not yet mounted.
#
echo "Mounting drives"
# mount internal HDD to /mnt
eval "mount /dev/disk/by-label/LAPTOP /mnt"
echo "Internal HDD mounted to /mnt"
# mount external HDD to /backup
eval "mount /dev/disk/by-label/USBHDD /backup"
echo "External USB HDD mounted to /backup"
echo ""
# capture current date and time
echo "Timestamp: $NOW"
echo ""
# Create snapshots
echo "Making today's snapshot of @"
eval $MKSNAP1
eval 'sync'
eval 'sync'
echo "@$NOW successfully created"
echo ""
echo "Making today's snapshot of @home"
eval $MKSNAP2
eval 'sync'
eval 'sync'
echo "@home$NOW successfully created"
echo ""
# Send snapshots
echo "Sending @$NOW and @home$NOW to external USB HDD"
eval $SNDSNAP1
eval 'sync'
eval 'sync'
eval $SNDSNAP2
eval 'sync'
eval 'sync'
TIMESTOP=$( date '+%H:%M:%S' )
STOPCLOCK=$(date -u -d "$TIMESTOP" +"%s")
DURATION=$(date -u -d "0 $STOPCLOCK sec - $STARTCLOCK sec" +"%H:%M:%S" )
echo "@$NOW and @home$NOW successfully sent to external USB HDD"
echo ""
echo "Creating and Sending Snapshots took: $DURATION"
echo ""
# Show snapshots on Internal and External USB HDD
echo "Contents of /mnt/snapshots"
vdir /mnt/snapshots
echo ""
echo "Contents of /backup"
vdir /backup
# echo ""
echo "Unmounting drives"
eval 'umount /backup'
eval 'umount /mnt'
eval 'sync'
eval 'sleep 1'
eval 'sync'
eval 'sleep 1'
eval 'sync'
eval 'sleep 1'
echo ""
echo "Drives unmounted and 3 syncs done"
# snapshot.sh
#
# Variable declarations
#
NOW=$(date +%Y%m%d%H%M)
MKSNAP1="btrfs su snapshot -r /mnt/@ /mnt/snapshots/@$NOW"
MKSNAP2="btrfs su snapshot -r /mnt/@home /mnt/snapshots/@home$NOW"
SNDSNAP1="btrfs send /mnt/snapshots/@$NOW | btrfs receive /backup"
SNDSNAP2="btrfs send /mnt/snapshots/@home$NOW | btrfs receive /backup"
TIMESTART=$( date '+%H:%M:%S' )
STARTCLOCK=$(date -u -d "$TIMESTART" +"%s")
# To be run as root from /
#
# Before executing this script:
# Plug in external HDD. Ignore Disk & Devices pop-up.
# External drive will be powered and identified by system but not yet mounted.
#
echo "Mounting drives"
# mount internal HDD to /mnt
eval "mount /dev/disk/by-label/LAPTOP /mnt"
echo "Internal HDD mounted to /mnt"
# mount external HDD to /backup
eval "mount /dev/disk/by-label/USBHDD /backup"
echo "External USB HDD mounted to /backup"
echo ""
# capture current date and time
echo "Timestamp: $NOW"
echo ""
# Create snapshots
echo "Making today's snapshot of @"
eval $MKSNAP1
eval 'sync'
eval 'sync'
echo "@$NOW successfully created"
echo ""
echo "Making today's snapshot of @home"
eval $MKSNAP2
eval 'sync'
eval 'sync'
echo "@home$NOW successfully created"
echo ""
# Send snapshots
echo "Sending @$NOW and @home$NOW to external USB HDD"
eval $SNDSNAP1
eval 'sync'
eval 'sync'
eval $SNDSNAP2
eval 'sync'
eval 'sync'
TIMESTOP=$( date '+%H:%M:%S' )
STOPCLOCK=$(date -u -d "$TIMESTOP" +"%s")
DURATION=$(date -u -d "0 $STOPCLOCK sec - $STARTCLOCK sec" +"%H:%M:%S" )
echo "@$NOW and @home$NOW successfully sent to external USB HDD"
echo ""
echo "Creating and Sending Snapshots took: $DURATION"
echo ""
# Show snapshots on Internal and External USB HDD
echo "Contents of /mnt/snapshots"
vdir /mnt/snapshots
echo ""
echo "Contents of /backup"
vdir /backup
# echo ""
echo "Unmounting drives"
eval 'umount /backup'
eval 'umount /mnt'
eval 'sync'
eval 'sleep 1'
eval 'sync'
eval 'sleep 1'
eval 'sync'
eval 'sleep 1'
echo ""
echo "Drives unmounted and 3 syncs done"
Comment