DD is the way to go. BUT if you expended your filesystem to the max of the SD card you will have a large image file for nothing.
So to do a backup of only the used space you need to do a clever DD command.
first you need to shrink the last partition on the sd card. (I wont tell you how to do it . it is far from the scope of the this message.) but take a look at Gparted. or just Parted if you have no GUI on the machine.
then you go into command line and do: fdisk -l as root (or sudo)
here is the output of one of my PI
Disk /dev/mmcblk0: 29 GiB, 31117541376 bytes, 60776448 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0xdeb2fbcd
Device Boot Start End Sectors Size Id Type
/dev/mmcblk0p1 8192 96453 88262 43.1M c W95 FAT32 (LBA)
/dev/mmcblk0p2 98304 60776447 60678144 29G 83 Linux
From there you need 2 info the block size (512 in my case) and the last used sector number. (
60776447 for that exemple)
you then use that command:
DD if="your source device" of =" your destination" bs=512 count=60776448
that would extract the image file and it would stop 1 block more then the used size of the last partition.
that way the image will be the exact maximum size it need to and if you then use another SD card that dont have exactly the same size as the original you wont run into problem copying it.