Skip to content

Instantly share code, notes, and snippets.

@dev01d
Last active August 16, 2021 11:43

Revisions

  1. dev01d revised this gist Aug 16, 2021. 1 changed file with 27 additions and 26 deletions.
    53 changes: 27 additions & 26 deletions piBak.sh
    Original file line number Diff line number Diff line change
    @@ -27,29 +27,30 @@ read -p 'Disk to be copied?: ' diskVar

    # Main attraction
    echo -e "\e[1;25;33m"
    read -r -p 'Are you sure? [Y/n] ' response
    response=${response,,} # tolower
    if [[ $response =~ ^(yes|y| ) ]] || [[ -z $response ]]; then
    umount /dev/$diskVar* 2>/dev/null
    echo -e "\e[1;25;32m"
    # This because rdisk is amazing
    # Prefer ddrescue if available
    case $arch in
    Darwin*)
    if hash ddrescue 2>/dev/null; then
    ddrescue /dev/r$diskVar $HOME/Downloads/piBak`date +%m-%d-%y`.img
    else
    dd if=/dev/r$diskVar of=$HOME/Downloads/piBak`date +%m-%d-%y`.img bs=100M
    fi
    ;;
    Linux*)
    if hash ddrescue 2>/dev/null; then
    ddrescue /dev/$diskVar $HOME/Downloads/piBak`date +%m-%d-%y`.img
    else
    dd if=/dev/$diskVar of=$HOME/Downloads/piBak`date +%m-%d-%y`.img bs=100M
    fi
    ;;
    esac
    else
    echo 'Ok, maybe later.'
    fi
    read -p 'Are you sure? [Y/n] ' response
    case "${response:-y}" in
    y|Y )
    umount /dev/$diskVar* 2>/dev/null
    echo -e "\e[1;25;32m"
    # This because rdisk is amazing
    # Prefer ddrescue if available
    case $arch in
    Darwin*)
    if hash ddrescue 2>/dev/null; then
    ddrescue /dev/r$diskVar $HOME/Downloads/piBak`date +%m-%d-%y`.img
    else
    dd if=/dev/r$diskVar of=$HOME/Downloads/piBak`date +%m-%d-%y`.img bs=100M
    fi
    ;;
    Linux*)
    if hash ddrescue 2>/dev/null; then
    ddrescue /dev/$diskVar $HOME/Downloads/piBak`date +%m-%d-%y`.img
    else
    dd if=/dev/$diskVar of=$HOME/Downloads/piBak`date +%m-%d-%y`.img bs=100M
    fi
    ;;
    esac
    ;;
    n|N )
    echo 'Ok, maybe later.' ;;
    esac
  2. dev01d revised this gist Apr 11, 2020. No changes.
  3. Jason revised this gist Aug 26, 2018. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions piBak.sh
    Original file line number Diff line number Diff line change
    @@ -50,4 +50,6 @@ if [[ $response =~ ^(yes|y| ) ]] || [[ -z $response ]]; then
    fi
    ;;
    esac
    else
    echo 'Ok, maybe later.'
    fi
  4. Jason created this gist Aug 26, 2018.
    53 changes: 53 additions & 0 deletions piBak.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,53 @@
    #!/usr/bin/env bash

    #Sudo check
    if (( $EUID != 0 )); then
    echo -e "\nPlease run as root or use sudo\n"
    exit
    fi

    # OS checks
    arch=$(uname -s)
    case $arch in
    Darwin*)
    listDisk='diskutil list'
    ;;
    Linux*)
    listDisk='lsblk'
    ;;
    *)
    exit
    ;;
    esac

    # User instruction
    eval $listDisk
    echo -e '\e[1;25;34mExample: disk2 or sdb2\e[0m\n'
    read -p 'Disk to be copied?: ' diskVar

    # Main attraction
    echo -e "\e[1;25;33m"
    read -r -p 'Are you sure? [Y/n] ' response
    response=${response,,} # tolower
    if [[ $response =~ ^(yes|y| ) ]] || [[ -z $response ]]; then
    umount /dev/$diskVar* 2>/dev/null
    echo -e "\e[1;25;32m"
    # This because rdisk is amazing
    # Prefer ddrescue if available
    case $arch in
    Darwin*)
    if hash ddrescue 2>/dev/null; then
    ddrescue /dev/r$diskVar $HOME/Downloads/piBak`date +%m-%d-%y`.img
    else
    dd if=/dev/r$diskVar of=$HOME/Downloads/piBak`date +%m-%d-%y`.img bs=100M
    fi
    ;;
    Linux*)
    if hash ddrescue 2>/dev/null; then
    ddrescue /dev/$diskVar $HOME/Downloads/piBak`date +%m-%d-%y`.img
    else
    dd if=/dev/$diskVar of=$HOME/Downloads/piBak`date +%m-%d-%y`.img bs=100M
    fi
    ;;
    esac
    fi