How backup partition on Linux with progress bar from command line

On GNU/Linux you can do everything from command line. The same goes to copy whole partition to a file. You can use powerful dd command to save your partition to a file. You can also use pv to show progress bar. Here is useful function that will backup your partition (device) to a file with progress bar (you need sudo to access device):

function backup() {
    sudo dd if=$1 | pv -s $(sudo blockdev --getsize64 $1) | bzip2 -9f > $2
}

and you can use this function like this:

backup /dev/sda1 /home/me/part.img.bz2

You can create similar function for restoring the partition:

function restore() {
    bunzip2 -dc $1 | pv -s $(ls -l $1 | cut -d' ' -f5) | dd of=$2
}

and you can use this function similar to backup (but the file is first and device second):

restore /home/me/part.img.bz2 /dev/sda1

NOTE: using dd can damage your partition, use it with caution.