3 updated script
source | link

I use the following script to backup SD cards on OS X:-

#!/bin/bash
# script to backup Pi SD card
# 20152017-06-05
# 2018-11-2829    optional name
#DSK='disk4'# DSK='disk4'   # manual set disk
OUTDIR=~/temp/Pi

# Find disk with LInuxLinux partition (works for Raspbian)
# Modified for PINN/NOOBS
export DSK=`diskutil list | grep "Linux" | cutsed 's/.*\(disk[0-c9]\).*/\1/' 69-73`| uniq`
if [ $DSK ]; then
    echo $DSK
    echo $OUTDIR
else
    echo "Disk not found"
    exit
fi

if [ $# -eq 0 ] ; then
    BACKUPNAME='Pi'
else
    BACKUPNAME=$1
fi
BACKUPNAME+="back"
echo $BACKUPNAME

diskutil unmountDisk /dev/$DSK
echo please wait - This takes some time
echo Ctl+T to show progress!
time sudo dd if=/dev/r$DSK bs=4m | gzip -9 > ~/temp/Pi$OUTDIR/Piback.img.gz 

#rename to current date
echo compressing completed - now renaming
mv -n ~/temp/Pi$OUTDIR/Piback.img.gz ~/temp/Pi$OUTDIR/Piback`date$BACKUPNAME`date +%Y%m%d`.img.gz

I use the following script to backup SD cards on OS X:-

#!/bin/bash
# script to backup Pi SD card
# 2015-11-28
#DSK='disk4'
# Find disk with LInux partition (works for Raspbian)
export DSK=`diskutil list | grep "Linux" | cut -c 69-73`
if [ $DSK ]; then
    echo $DSK
else
    echo "Disk not found"
    exit
fi
diskutil unmountDisk /dev/$DSK
echo please wait - This takes some time
echo Ctl+T to show progress!
time sudo dd if=/dev/r$DSK bs=4m | gzip -9 > ~/temp/Pi/Piback.img.gz
#rename to current date
echo compressing completed - now renaming
mv -n ~/temp/Pi/Piback.img.gz ~/temp/Pi/Piback`date +%Y%m%d`.img.gz

I use the following script to backup SD cards on OS X:-

#!/bin/bash
# script to backup Pi SD card
# 2017-06-05
# 2018-11-29    optional name
# DSK='disk4'   # manual set disk
OUTDIR=~/temp/Pi

# Find disk with Linux partition (works for Raspbian)
# Modified for PINN/NOOBS
export DSK=`diskutil list | grep "Linux" | sed 's/.*\(disk[0-9]\).*/\1/' | uniq`
if [ $DSK ]; then
    echo $DSK
    echo $OUTDIR
else
    echo "Disk not found"
    exit
fi

if [ $# -eq 0 ] ; then
    BACKUPNAME='Pi'
else
    BACKUPNAME=$1
fi
BACKUPNAME+="back"
echo $BACKUPNAME

diskutil unmountDisk /dev/$DSK
echo please wait - This takes some time
echo Ctl+T to show progress!
time sudo dd if=/dev/r$DSK bs=4m | gzip -9 > $OUTDIR/Piback.img.gz 

#rename to current date
echo compressing completed - now renaming
mv -n $OUTDIR/Piback.img.gz $OUTDIR/$BACKUPNAME`date +%Y%m%d`.img.gz
2 added syntax highlighting
source | link

I use the following script to backup SD cards on OS X:-

#!/bin/bash
# script to backup Pi SD card
# 2015-11-28
#DSK='disk4'
# Find disk with LInux partition (works for Raspbian)
export DSK=`diskutil list | grep "Linux" | cut -c 69-73`
if [ $DSK ]; then
    echo $DSK
else
    echo "Disk not found"
    exit
fi
diskutil unmountDisk /dev/$DSK
echo please wait - This takes some time
echo Ctl+T to show progress!
time sudo dd if=/dev/r$DSK bs=4m | gzip -9 > ~/temp/Pi/Piback.img.gz
#rename to current date
echo compressing completed - now renaming
mv -n ~/temp/Pi/Piback.img.gz ~/temp/Pi/Piback`date +%Y%m%d`.img.gz
#!/bin/bash
# script to backup Pi SD card
# 2015-11-28
#DSK='disk4'
# Find disk with LInux partition (works for Raspbian)
export DSK=`diskutil list | grep "Linux" | cut -c 69-73`
if [ $DSK ]; then
    echo $DSK
else
    echo "Disk not found"
    exit
fi
diskutil unmountDisk /dev/$DSK
echo please wait - This takes some time
echo Ctl+T to show progress!
time sudo dd if=/dev/r$DSK bs=4m | gzip -9 > ~/temp/Pi/Piback.img.gz
#rename to current date
echo compressing completed - now renaming
mv -n ~/temp/Pi/Piback.img.gz ~/temp/Pi/Piback`date +%Y%m%d`.img.gz

I use the following script to backup SD cards on OS X:-

#!/bin/bash
# script to backup Pi SD card
# 2015-11-28
#DSK='disk4'
# Find disk with LInux partition (works for Raspbian)
export DSK=`diskutil list | grep "Linux" | cut -c 69-73`
if [ $DSK ]; then
    echo $DSK
else
    echo "Disk not found"
    exit
fi
diskutil unmountDisk /dev/$DSK
echo please wait - This takes some time
echo Ctl+T to show progress!
time sudo dd if=/dev/r$DSK bs=4m | gzip -9 > ~/temp/Pi/Piback.img.gz
#rename to current date
echo compressing completed - now renaming
mv -n ~/temp/Pi/Piback.img.gz ~/temp/Pi/Piback`date +%Y%m%d`.img.gz

I use the following script to backup SD cards on OS X:-

#!/bin/bash
# script to backup Pi SD card
# 2015-11-28
#DSK='disk4'
# Find disk with LInux partition (works for Raspbian)
export DSK=`diskutil list | grep "Linux" | cut -c 69-73`
if [ $DSK ]; then
    echo $DSK
else
    echo "Disk not found"
    exit
fi
diskutil unmountDisk /dev/$DSK
echo please wait - This takes some time
echo Ctl+T to show progress!
time sudo dd if=/dev/r$DSK bs=4m | gzip -9 > ~/temp/Pi/Piback.img.gz
#rename to current date
echo compressing completed - now renaming
mv -n ~/temp/Pi/Piback.img.gz ~/temp/Pi/Piback`date +%Y%m%d`.img.gz
1
source | link

I use the following script to backup SD cards on OS X:-

#!/bin/bash
# script to backup Pi SD card
# 2015-11-28
#DSK='disk4'
# Find disk with LInux partition (works for Raspbian)
export DSK=`diskutil list | grep "Linux" | cut -c 69-73`
if [ $DSK ]; then
    echo $DSK
else
    echo "Disk not found"
    exit
fi
diskutil unmountDisk /dev/$DSK
echo please wait - This takes some time
echo Ctl+T to show progress!
time sudo dd if=/dev/r$DSK bs=4m | gzip -9 > ~/temp/Pi/Piback.img.gz
#rename to current date
echo compressing completed - now renaming
mv -n ~/temp/Pi/Piback.img.gz ~/temp/Pi/Piback`date +%Y%m%d`.img.gz