Switch WhatsApp Accounts - Android Script (rooted)

IMPORTANT: This is not for regular users! Backup your data properly, as I am not responsible for absolutelly nothing you do to your phone, and I'll not provide any kind of support. You are on your own!!!

WhatsApp (and most messengers today) are pretty awful, they bring a torrent of useless information, misinformation and trivia that are not really needed nor wanted by many. The idea of being online and reachable 24/7/365 seems like a nightmare.

To make matters worse businesses are now venturing into these messenger apps thus forcing customers and business partners to use them, whether they like it or not. There is really no limit to this baloney!

Dual WA Messenger by Hack

I've created a little bash script hack to help me with that. This should work on any rooted Android phone (dual-sim or single-sim). With some modifications it could even work on iPhones as well, as long as they are rooted (jailbreak).

The idea is pretty simple: swap WhatsApp databases and media folders for each account as needed. You still need to verify each phone number with the service. Also beware of the PATHs used in the script, as these reflects the paths used on my own Android device, and they may vary from phone to phone, from brand to brand. Because of this some tweak of this script will be needed!

This is not for the ordinary Zap/Droid user!

How

First things first. Let me explain what the script will do, and we can go from there. Considering you already have a WhatsApp account setup on your phone, the app will mainly be divided into 3 things:

A) The app itself.
B) A collection of databases with chats, numbers, certificates and so on.
C) A folder with sent and received media.

On Android devices you can only easily access C, which sits on your phone's internal memory as a folder called WhatsApp. A and B are tucked into other directories. A we won't need to touch, and B is inside the /data/data directory, in a directory called com.whatsapp. Then mainly B is where all the magic happens.

Upon execution the script will then swap both of these folders (B and C) to other ones, allowing the user to use both accounts on a single phone. Note also that you'll not be able to use them both simultaneously. Even with this limitation this is definetly a win for me, might be for you too.

I’ve called my WhatsApp accounts old and new, as one is activated with an old number and another with my new number. I've decided to keep the databases backups (old and new) in the SDCard, as I have more free space there.

When I execute the script it will (in this order) do:

  • Detect whether I am using the old account or the new one
  • Put the phone in Airplane mode (to avoid triggers from bringing WhatsApp back up once I’ve kill it in the next step)
  • Kill WhatsApp process (twice as it is extremely trigger happy and obnoxious)
  • Backup the current WhatsApp database (old or new) to my SDCard into a cozily compressed tar file
  • Move the internal memory's WhatsApp media folder to another directory (WhatsApp.NEW or WhatsApp.OLD)
  • Restore the WhatsApp databases from a tar file (old or new, depending on what account was last active)
  • Remove the cache located at (INTERNAL_MEMORY/Android/data/com.whatsapp)
  • Set the new or old account as active (again depending on what account was last active), so upon next the execution it will swap back to the one that is not active
  • Double kill WhatsApp process once more
  • Kill SystemUIService (like ending Finder or Explorer processes) and bring it back up again)
  • Switch back from Airplane mode into regular networked mode.

The script will “know” which account is which by the creation of a simple empty file in the SDCard. If that file is present the new account is active, if it is not then the currently active is the old one. So, every time the “new account routine” is called it will “touch” this file into existence, and likewise every time the “old account routine” is called it will delete this file. Be careful not to delete this "settings" file or it will mess things up.

Setup

To use the script we first need to setup a few things (some of which I will mostly, definitely, not explain it here).

First, the phone needs to be rooted. Secondly you need a working toolchain installed, busybox or the one that comes with Termux. SSH access to make the initial setup is also very recommended as makes things easier. After that you can use your favorite terminal emulator to call the script. I use Termux.

The programs this script makes use of are the following:

tar (to inflate and deflate compressed backups)
kill and pkill (to assassinate processes around)
tsu (to elevate commands in order to meddle with the /data/data directory)
pm (Comes with every Android phone. Used to wipe databases)
am/settings (Also comes with Android. Used to switch Airplane Mode on and off)

First of all, su yourself into the magical kingdom:

su

Now let's consider you already have one account setup, your main one, activated with phone number say 1. Let's call it NEW. First we create a directory that will hold all our files. F6B8-70DC is my external SDcard:

mkdir /storage/F6B8-70DC/wswap

Then we create a fresh new tar backup for this new account (so we can switch to it later) and save it to this directory:

tar -zcf /storage/F6B8-70DC/wswap/new.tar.gz /data/data/com.whatsapp

Finally let's move the media folder in the internal memory to it's "inactive state":

mv /storage/emulated/0/WhatsApp /storage/emulated/0/WhatsApp.NEW

Let's check how well the mv, tar and touch commands went:


root [~] $ ls /storage/emulated/0/
total 0K
4.0K drwxrwx--x 7 root sdcard_rw 4.0K May 23 11:27 ./
4.0K drwx--x--x 5 root sdcard_rw 4.0K Mar 10 12:11 ../
4.0K drwxrwx--x 4 root sdcard_rw 4.0K May 23 09:15 .face/
4.0K drwxrwx--x 3 root sdcard_rw 4.0K May 23 09:08 Android/
4.0K drwxrwx--x 2 root sdcard_rw 4.0K May 23 09:24 Download/
 28K drwxrwx--x 3 root sdcard_rw  28K May 15 08:41 Music/
 4.0K drwxrwx--x 4 root sdcard_rw 4.0K May 23 09:14 WhatsApp.NEW/

root [~] $ ls /storage/F6B8-70DC/wswap/
total 6.2M
 32K drwxrwx--x 3 root sdcard_rw  32K May 23 09:29 ./
 32K drwxrwx--x 6 root sdcard_rw  32K May 20 12:15 ../
 6.2M -rwxrwx--x 1 root sdcard_rw 6.2M May 23 08:56 new.tar.gz*

Great! Now that we have the new account dbs and media folder backed-up we are ready to wipe it's data, remove the new simcard and insert the old one, and activate this old one with Whatsapp server. And then repeat the process above to create an OLD backup.

Fisrt of all remove the new/current simcard and insert the secondary one that will be used to register a seccond account with WhatsApp.

Then kill the beast (several times if needed). As long as this one-liner do not return anything it means that a WhatsApp process is active. When there is no WhatsApp runnig it will give out an error:

kill $(ps -A | grep 'com.whatsapp' | awk '{print $1}')

Now we wipe the cache inside the phone's internal memory, in the Android folder:

rm -rf /storage/emulated/0/Android/data/com.whatsapp

Next wipe the current (new) WhatsApp databases in order to start fresh:

pm clear com.whatsapp

Now open Whatsapp. You should see the New Account setup screen. If not, reboot your phone and start WhatsApp once more.

Setup the secondary accound as you normally would: phone, sms verification, name, account image, etc. Setup also all preferences in WhatsApp Settings that you might deem necessary for your use.

Once all that is done and the secondary account is active then is time to back it up as we did with the new earlier. Only then we'll be ready to switch accounts.

Create a fresh new tar backup for this old account:

tar -zcf /storage/F6B8-70DC/wswap/old.tar.gz /data/data/com.whatsapp

Now let's take a final look into the stuff we created:


root [~] $ ls /storage/emulated/0/
total 0K
4.0K drwxrwx--x 7 root sdcard_rw 4.0K May 23 11:27 ./
4.0K drwx--x--x 5 root sdcard_rw 4.0K Mar 10 12:11 ../
4.0K drwxrwx--x 4 root sdcard_rw 4.0K May 23 09:15 .face/
4.0K drwxrwx--x 3 root sdcard_rw 4.0K May 23 09:08 Android/
4.0K drwxrwx--x 2 root sdcard_rw 4.0K May 23 09:24 Download/
 28K drwxrwx--x 3 root sdcard_rw  28K May 15 08:41 Music/
4.0K drwxrwx--x 4 root sdcard_rw 4.0K May 23 09:14 WhatsApp.NEW/
4.0K drwxrwx--x 4 root sdcard_rw 4.0K May 23 11:38 WhatsApp/

root [~] $ ls /storage/F6B8-70DC/wswap/
total 8.4M
 32K drwxrwx--x 3 root sdcard_rw  32K May 23 09:29 ./
 32K drwxrwx--x 6 root sdcard_rw  32K May 20 12:15 ../
6.2M -rwxrwx--x 1 root sdcard_rw 6.2M May 23 08:56 new.tar.gz*
6.2M -rwxrwx--x 1 root sdcard_rw 2.2M May 23 08:56 old.tar.gz*

Like I've said before, the "setting" of which account is being used is done by the presence of the file /storage/F6B8-70DC/wswap/newactive. Since we are now at the old one, no need to create it.

Copy (or paste) the script to your phone and make it executable with:

root [~/bin] $ chmod +x was

root [~/bin] $ ls was
4.0K -rwxr-xr-x 1 u0_a151 u0_a151 3.7K May 23 09:30 was*

You can now insert the new simcard into the phone.

Now when we run the script it will (by the absence of that file) detect we are using the old number's WhatsApp, swap the databases and the media folder to the new number's ones, create the newactive file and restart the phone's UI.

That's it! I'll laugh now all the way to my happy, no business allowed, cozy place.

Running

Here is a screenshot of the script running

Was

The Script

You can also download it from here: https://www.mteam7.com/files/was.zip

#!/data/data/com.termux/files/usr/bin/bash

FILE=/storage/F6B8-70DC/wswap/newactive

if test -f "$FILE"; then

  echo "============================="
  echo "= WhatsApp Account Switcher ="
  echo "============================="
  echo
  echo " Switching to old number..."
  echo
  echo "  - Disabling connections"
  tsu -c "settings put global airplane_mode_on 1 > /dev/null 2>&1"
  tsu -c "am broadcast -a android.intent.action.AIRPLANE_MODE > /dev/null 2>&1"
  echo "  - Killing WhatsApp"
  tsu -c "kill $(ps -A | grep 'com.whatsapp' | awk '{print $1}') > /dev/null 2>&1"
  sleep 3
  tsu -c "kill $(ps -A | grep 'com.whatsapp' | awk '{print $1}') > /dev/null 2>&1"
  echo "  - Backing up new databases"
  tsu -c "rm -rf /storage/emulated/0/Android/data/com.whatsapp && rm /storage/F6B8-70DC/wswap/new.tar.gz && tar -zcf /storage/F6B8-70DC/wswap/new.tar.gz /data/data/com.whatsapp > /dev/null 2>&1"
  echo "  - Switching to old databases"
  tsu -c "pm clear com.whatsapp > /dev/null 2>&1 && tar -zxf /storage/F6B8-70DC/wswap/old.tar.gz -C / > /dev/null 2>&1"
  echo "  - Switching to old media storage cache"
  tsu -c "mv /storage/emulated/0/WhatsApp /storage/emulated/0/WhatsApp.NEW > /dev/null 2>&1 && mv /storage/emulated/0/WhatsApp.OLD /storage/emulated/0/WhatsApp > /dev/null 2>&1"
  echo "  - Setting old WhatsApp as active"
  tsu -c "rm /storage/F6B8-70DC/wswap/newactive"
  tsu -c "kill $(ps -A | grep 'com.whatsapp' | awk '{print $1}') > /dev/null 2>&1"
  sleep 3
  tsu -c "kill $(ps -A | grep 'com.whatsapp' | awk '{print $1}') > /dev/null 2>&1"
  tsu -c "pkill -TERM -f com.android.systemui; am startservice --user 0 -n com.android.systemui/.SystemUIService > /dev/null 2>&1"
  echo "  - Enabling connections"
  tsu -c "settings put global airplane_mode_on 0 > /dev/null 2>&1"
  tsu -c "am broadcast -a android.intent.action.AIRPLANE_MODE > /dev/null 2>&1"
  echo
  echo " Old WhatsApp is now active."

else

  echo "============================="
  echo "= WhatsApp Account Switcher ="
  echo "============================="
  echo
  echo " Switching to new number..."
  echo
  echo "  - Disabling connections"
  tsu -c "settings put global airplane_mode_on 1 > /dev/null 2>&1"
  tsu -c "am broadcast -a android.intent.action.AIRPLANE_MODE > /dev/null 2>&1"
  echo "  - Killing WhatsApp"
  tsu -c "kill $(ps -A | grep 'com.whatsapp' | awk '{print $1}') > /dev/null 2>&1"
  sleep 3
  tsu -c "kill $(ps -A | grep 'com.whatsapp' | awk '{print $1}') > /dev/null 2>&1"
  echo "  - Backing up old databases"
  tsu -c "rm -rf /storage/emulated/0/Android/data/com.whatsapp && rm /storage/F6B8-70DC/wswap/old.tar.gz && tar -zcf /storage/F6B8-70DC/wswap/old.tar.gz /data/data/com.whatsapp > /dev/null 2>&1"
  echo "  - Switching to new databases"
  tsu -c "pm clear com.whatsapp > /dev/null 2>&1 && tar -zxf /storage/F6B8-70DC/wswap/new.tar.gz -C / > /dev/null 2>&1"
  echo "  - Switching to new media storage cache"
  tsu -c "mv /storage/emulated/0/WhatsApp /storage/emulated/0/WhatsApp.OLD > /dev/null 2>&1 && mv /storage/emulated/0/WhatsApp.NEW /storage/emulated/0/WhatsApp > /dev/null 2>&1"
  echo "  - Setting new WhatsApp as active"
  tsu -c "touch /storage/F6B8-70DC/wswap/newactive"
  tsu -c "kill $(ps -A | grep 'com.whatsapp' | awk '{print $1}') > /dev/null 2>&1"
  sleep 3
  tsu -c "kill $(ps -A | grep 'com.whatsapp' | awk '{print $1}') > /dev/null 2>&1"
  tsu -c "pkill -TERM -f com.android.systemui; am startservice --user 0 -n com.android.systemui/.SystemUIService > /dev/null 2>&1"
  echo "  - Enabling connections"
  tsu -c "settings put global airplane_mode_on 0 > /dev/null 2>&1"
  tsu -c "am broadcast -a android.intent.action.AIRPLANE_MODE > /dev/null 2>&1"
  echo
  echo " New WhatsApp is now active."

fi
{{ message }}

{{ 'Comments are closed.' | trans }}