Introduction: The Quest for Seamless Custom ROM Transitions
Switching between custom Android ROMs like LineageOS, Pixel Experience, or crDroid often means a fresh start. While a clean flash is often recommended for stability, the thought of reconfiguring every app, losing game progress, or painstakingly restoring chat histories can deter many power users. The core challenge lies in how Android manages app data within its /data partition, and how different ROMs or even different versions of the same ROM can subtly break compatibility when data is simply copied over.
This article dives deep into the anatomy of Android’s data partition, offering an expert-level guide to manually backing up and restoring critical app data across custom ROMs. We’ll explore the filesystem structure, permission intricacies (UID/GID), and SELinux contexts that make simple copy-pasting insufficient, providing a methodology for flawless data persistence.
Understanding the Android Data Partition (/data)
The /data partition is the heart of your Android device’s user experience. It houses everything from your personal photos and videos to app settings, databases, and cached information. Understanding its structure is paramount for effective data migration.
Key Directories within /data:
/data/data/: This directory is where individual application data resides. Each app typically has its own subdirectory named after its package name (e.g.,com.whatsapp,com.google.android.youtube). Inside, you’ll find databases, shared preferences, caches, and app-specific files. This is the most critical area for app data persistence./data/media/0/: On modern Android devices, this directory represents your internal storage (what you see as ‘Internal Storage’ when connected to a PC or in a file manager). It contains user-generated content like photos, videos, downloads, and app-specific directories created by apps to store user files (e.g., WhatsApp media, Telegram files)./data/user/0/: This is a symlink to/data/data/for the primary user. Multi-user setups would have/data/user/10/, etc./data/app/: Contains installed APK files. While not directly data, sometimes app data is tightly coupled with its APK version./data/misc/: Houses miscellaneous system data, including Wi-Fi configurations, VPN settings, and other system-level preferences.
The Challenge: UID/GID Mismatches and SELinux Contexts
When you install a new ROM, even if it’s based on the same Android version, the system often assigns new User IDs (UIDs) and Group IDs (GIDs) to installed applications. Android uses these IDs for granular access control. If you simply copy an app’s data from an old ROM to a new one, the files will retain their original UID/GID ownership. The new ROM’s system will try to access these files with the *new* UID/GID, leading to permission denied errors, app crashes, or unexpected behavior.
Furthermore, Android employs SELinux (Security-Enhanced Linux) for mandatory access control. Every file and process has an SELinux context (e.g., u:object_r:app_data_file:s0). When you flash a new ROM, the default SELinux policies might differ, and simply copying files doesn’t automatically assign the correct context for the new system, potentially blocking access even if UIDs/GIDs are correct.
Prerequisites and Tools
- Unlocked Bootloader & Custom Recovery: TWRP (Team Win Recovery Project) is highly recommended for its advanced file management and backup capabilities.
- ADB (Android Debug Bridge) & Fastboot: Installed on your PC and configured.
- Root Access: Essential for accessing and modifying files within
/data. - Ample Storage: External storage (USB OTG) or sufficient PC storage for backups.
- Time & Patience: This is a meticulous process.
Step-by-Step Guide for Cross-ROM Data Migration
Phase 1: Pre-Migration Backup (Old ROM)
1. Full Nandroid Backup (Mandatory Safety Net):
Boot into TWRP. Select ‘Backup’, choose ‘Boot’, ‘System’, ‘Data’, ‘Cache’, ‘Dalvik/ART Cache’. Store this backup on external storage. This is your ultimate rollback plan.
2. Identify Critical App Data:
Determine which apps hold data you absolutely cannot lose. For example, WhatsApp chat backups (often in /data/data/com.whatsapp/databases), specific game saves, or unique app configurations. For most apps, internal storage data (e.g., photos) is easily moved, but /data/data/ content is tricky.
3. Manual Data Extraction (ADB Shell/TWRP File Manager):
Connect your phone to PC via USB. Boot into TWRP. Open an ADB shell:
adb shell
Navigate to the /data/data directory. We’ll use tar to preserve permissions and ownership for specific directories. Let’s say you want to back up WhatsApp data:
su # Ensure you have root in adb shell if not already root (TWRP usually grants it)cd /data/datatar -cvpf /sdcard/WhatsAppDataBackup.tar com.whatsapp/
Replace com.whatsapp/ with the package name of the app you need. You can create multiple .tar archives for different apps. For /data/media/0 (internal storage), you can use `adb pull` directly:
adb pull /sdcard/DCIM C:ackups
ew_rom_dcim # Example for photosadb pull /sdcard/Documents C:ackups
ew_rom_docs # Example for documents
Once `tar` archives are created on `/sdcard` (internal storage), pull them to your PC:
adb pull /sdcard/WhatsAppDataBackup.tar C:ackups
ew_rom_app_data
emove /sdcard/WhatsAppDataBackup.tar # Clean up internal storage
Phase 2: Flashing New ROM & Initial Setup
1. Wipe Old ROM:
In TWRP, go to ‘Wipe’ -> ‘Advanced Wipe’. Select ‘Dalvik/ART Cache’, ‘System’, ‘Data’, ‘Internal Storage’ (if you don’t need user files like photos), and ‘Cache’. DO NOT WIPE INTERNAL STORAGE if you stored your `tar` backups there and haven’t pulled them to PC yet. Perform the wipe.
2. Flash New ROM:
Flash your new custom ROM, GApps (if needed), Magisk (for root), and any other essential zips. Reboot the system.
3. Initial Boot and Setup:
Crucially, complete the initial setup wizard of your new ROM. Let it boot fully to the home screen. Install the apps whose data you want to restore *from the Play Store* first. This step is vital because installing the apps will create their respective directories under /data/data/ with the correct *new* UID/GID and initial SELinux contexts. Do not launch these apps yet.
Phase 3: Data Restoration & Permission Correction
1. Push Back Tar Archives:
Connect your phone to PC. Push your `tar` backups back to internal storage (/sdcard):
adb push C:ackups
ew_rom_app_dataaker.tar /sdcard/faker.tar # For each app tar file
2. Restore Data and Correct Permissions:
Boot back into TWRP. Open an ADB shell:
adb shell
Navigate to the /data/data directory:
cd /data/data/
Now, extract your `tar` archive. This will overwrite the (empty) directories created by the freshly installed app. Let’s use WhatsApp as an example:
tar -xvpPf /sdcard/WhatsAppDataBackup.tar # This extracts with preserved permissions
After extraction, the files will have the *old* ROM’s UID/GID and SELinux contexts. This is where the magic happens:
- Correcting UID/GID Ownership: The new app installation already created a directory with the correct new ownership. We need to apply that ownership recursively to the restored files. Find the new app’s UID/GID using
ls -n /data/data/com.whatsapp(look at the first column of numbers). Let’s assume the new UID is `10123` and GID is `10123` (often same for app-specific data).
chown -R 10123:10123 com.whatsapp/
If you don’t know the exact UID/GID, you can often infer it from the newly created (empty) directory for that app. For example:
chown -R $(stat -c '%u:%g' com.whatsapp) com.whatsapp/
- Restoring SELinux Contexts: This is critical for security and functionality. Android’s
restoreconutility is designed for this.
restorecon -Rv com.whatsapp/
The -R flag applies it recursively, and -v provides verbose output, showing what changed. This command will scan the directory and apply the correct SELinux contexts based on the new ROM’s policies.
3. Clean Up:
Remove the `tar` archives from `/sdcard` to free up space:
rm /sdcard/WhatsAppDataBackup.tar
4. Reboot System:
Reboot your phone. Launch the restored applications. They should now recognize their data and function correctly. Repeat this process for all apps you backed up.
Advanced Considerations
- App-Specific Quirks: Some apps (especially banking or security-focused ones) might employ additional anti-tampering measures, making this process more complex.
- Magisk Modules: If you use Magisk, some modules might help with app data migration or providing specific permissions.
- System Settings: For system-wide settings (Wi-Fi, Bluetooth pairings, accessibility), you might need to export/import specific files from
/data/misc/wifior other locations, though this is generally more error-prone due to deeper system integration.
Conclusion
Migrating app data between custom ROMs is not for the faint of heart, but by understanding the underlying filesystem, permission model, and SELinux contexts, it’s entirely achievable. This detailed, manual approach provides granular control, ensuring your most precious app data transitions seamlessly to your new Android environment. Always remember to perform a full Nandroid backup as your first step, and proceed with caution. Happy ROM flashing!
Android Mobile Specs & Compare Directory
Are you researching mobile hardware properties, processor SoCs, GPU chipsets, or RAM configurations? Access our complete specs catalog to compare up to 5 devices side-by-side!
Compare Devices Specs →