It's easy to achieve on a Linux PC:
- Attach both SD cards to PC.
- Dump 2nd partition from old SD card to new one. It's also possible directly from Android device. See Creating Partition Dump.
- Resize filesystem size to 30GB (the new partition size) using
COPY WHOLE FILESYSTEM:
The above approach works on Android too if you are able to attach new SD card through USB OTG SD card reader, while the old one is inside phone.
Or you can do it in 2 steps, as you are already trying:
Create partition dump:
~# gzip -9ck /dev/block/mmcblk1p2 >/storage/sdcard1/sdcard2root.img.gz
You can use
cat or even
cp. Make sure you have enough space on primary external (physically internal) storage to save dumped (and optionally compressed) image of SD card's 2nd partition.
Compression is useful only with sparseness. To avoid copying freed filesystem blocks (of deleted files) use
e2image -rap. Or discard logical to physical block mapping using
fstrim (on mounted filesystem) or
e2fsck -E discard, and drop page cache using
sync; echo -n 1 >/proc/sys/vm/drop_caches.
Insert new SD card and do partitioning using
Write partition image to new SD card:
~# gzip -dck /storage/sdcard1/sdcard2root.img.gz >/dev/block/mmcblk1p2
Maximize filesystem size to partition size:
~# resize2fs /dev/block/mmcblk1p2
COPY FILESYSTEM CONTENTS:
Another method is to only copy/move filesystem contents (files, directories etc.) and not the whole filesystem structure. But files' metadata needs to be preserved, or things may break (since the partition is to be mounted inside
/data). It includes
extended attributes, particularly SELinux contexts. Some executable binaries (in
/system) might also have file capabilities set (saved as extended attributes) but that's usually not required for user data. Other metadata like ACLs (also saved as extended attributes) and file attributes (inode flags) are not generally used on Android devices.
On Linux tools like
rsync (or even
cp) can preserve the required metadata. For reference see this and this answer. On Android (to move the contents in 2 steps)
tar is a good choice:
~$ cd /path/to/old_sdcard/mountpoint/
~$ tar --create --preserve-permissions --selinux --verbose --gzip --file=/storage/sdcard1/sdcard2root.tar.gz .
Insert new SD card, do partitioning (if required), and create filesystem using
mkfs.ext4 (by default it takes whole partition). To restore backup:
~$ cd /path/to/new_sdcard/mountpoint/
~$ tar --extract --preserve-permissions --selinux --verbose --gunzip --file=/storage/sdcard1/sdcard2root.tar.gz