Announcement

Collapse
No announcement yet.

A new backup script for my BTRFS

Collapse
This topic is closed.
X
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    A new backup script for my BTRFS

    Earlier I merged my @home subvolume with the @ subvolume, which makes snapshotting and incremental backup up 1/2 the work, since I am creating a snapshot only for @. This also increases the number of snapshots I can have on my SSD without running into balance problems as quicklly.

    I wrote a script to make the snapshot process faster and less error prone, given my shaky typing. It worked well but one weakness became apparent. I listed the existing snapshots and chose which snapshot I wanted to use as the parent for the incremental backup. It became apparent that 99.99999999% of the time is was the snapshot I made previously. By chance today I happened upon this website. It had the answer to the problem of how to pick out the first and second to last snapshot. With that information in hand I rewrote my script, which I saved as:

    make_snapshot.sh
    Code:
    #!/bin/bash
    #
    # script to create backup snapshots to /mnt/snapshots and a differential backup to /backup
    # To be run as root from /
    #
    echo "Mounting drives"
    eval "mount /dev/disk/by-uuid/ce2b5741-c01e-4b3d-b6ba-401ad7f7fcdf /mnt"
    eval "mount /dev/disk/by-uuid/e84e2cdf-d635-41c5-9f6f-1d0235322f48 /backup"
    
    NOW=$(date +%Y%m%d%H%M)
    echo "Making today's snapshot"
    MKSNAP='btrfs su snapshot -r /mnt/@ /mnt/snapshots/@'$NOW
    eval $MKSNAP
    eval 'sync'
    eval 'sync'
    
    echo "Finding previous snapshot as parent "
    PREVSNAP=""
    list=($(ls /mnt/snapshots/))
    PREVSNAP=${list[-2]}
    NOW=${list[-1]}
    echo "Attempting incremental backup"
    if [[ -s "/mnt/snapshots/"$PREVSNAP ]]; 
    then
        MKINC='btrfs send -p /mnt/snapshots/'$PREVSNAP
        MKINC=$MKINC' /mnt/snapshots/'$NOW
        MKINC=$MKINC' | btrfs receive /backup'
        echo $MKINC
        eval $MKINC
        eval 'sync'
        eval 'sync'
        eval "sync"
        DELSNAP='btrfs subvol delete -C /mnt/snapshots/'${list[0]}
        eval $DELSNAP
        eval 'sync'
        eval 'sync'
        eval 'sync'
        list=''
        list=($(ls /backup))
        DELSNAP='btrfs subvol delete -C /backup/'${list[0]}
        eval $DELSNAP
        eval 'sync'
        eval 'sync'
        eval 'sync'
        echo "Snapshots completed, oldest snapshots deleted"
        eval 'umount /backup'
        eval 'umount /mnt'
        echo "Drives unmounted"
    else
        echo 'Incremental backup failed using '$PREVSNAP' and '$NOW
        echo 'Drives still mounted, ready to clean up'
    fi
    You may want to comment out the DELSNAP commands until your number of archive snapshots hits somewhere between 5 and 10. Then, the script will maintain the number when you remove the comment flag.

    If you see any errors or ways of improving it let me know.
    Last edited by GreyGeek; Oct 17, 2020, 08:17 PM. Reason: fix some coding problems
    "A nation that is afraid to let its people judge the truth and falsehood in an open market is a nation that is afraid of its people.”
    – John F. Kennedy, February 26, 1962.

    #2
    GG

    woody
    sigpic
    Love Thy Neighbor Baby!

    Comment

    Working...
    X