Shell script to download backups

Bonjour,

In order to keep a copy of the backups uploaded by enough in the cloud on my own 8TB hard drive, I wrote this script:

#!/bin/bash                                                                                                                                    

export BACKUPS=$(dirname $0)
export SCRIPT=$0

function run() {
    local name=$1
    local d=${BACKUPS}/$name
    local locked=72

    flock --conflict-exit-code $locked --nonblock $d $SCRIPT run_no_lock $name

    if test $? = $locked ; then
        echo $d lock says another process is running, abort
    fi
}

function run_no_lock() {
    local name=$1
    local d=${BACKUPS}/$name

    source $d/openrc.sh

    rm -f $d/*.tmp

    download $d |& tee -a $d/$(date +%Y-%m-%d).log
}

function download() {
    local d=$1

    openstack image list --limit 2000 --status=active --private -c ID -c Name -f value | grep 20..-..-..- | while read id name ; do
        if test -f $d/$name.lz4 ; then
           continue
        fi
        echo downloading $d/$name
        set -o pipefail
        if timeout --foreground 3h glance image-download $id | lz4 > $d/$name.tmp ; then
            mv $d/$name.tmp $d/$name.lz4
            ls -lh $d/$name.lz4
        else
            echo glance terminated with non zero status, abort
            return 1
        fi
    done
}

$1 $2

Which runs from the following crontab:

SHELL=/bin/bash
# .---------------- minute (0 - 59)
# |  .------------- hour (0 - 23)
# |  |  .---------- day of month (1 - 31)
# |  |  |  .------- month (1 - 12) OR jan,feb,mar,apr ...
# |  |  |  |  .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
# |  |  |  |  |
0   2  *  *  * /media/tiny-backup-2022/backup.sh run enough.community
1 Like