• Introducing XDA Computing: Discussion zones for Hardware, Software, and more!    Check it out!

[GUIDE][Difficulty: Intermediate] Weekly TWRP Backups

Search This thread

MishaalRahman

Retired Editor in Chief
Nov 2, 2015
1,042
2,133
www.xda-developers.com
It's good practice to run a full backup of your ROM and user data every once in awhile, in case you flash something and get into a bootloop. Thanks to TWRP's scripting capabilities, we can trigger a weekly backup.

Prerequisites

  1. Root Access

jfH9PiZ.jpg

Instructions

  1. Create a new Profile named TWRP Backup.
  2. Create a Time context, and choose a time at night you want the TWRP backup to take place. For me, I made it at 4:00AM. To pick a single time, check both "From" and "To" and put the exact same time in both.
  3. No need to name the task anything, just enter its configuration.
  4. Code --> Run Shell. This is the code you'll need:
    Code:
    chmod 0777 /cache/recovery
    echo "wipe cache\nwipe dalvik\nbackup SDBOM weeklybackup \ncmd reboot" > /cache/recovery/openrecoveryscript
    Be VERY sure that this is typed exactly as such! Tick "Use Root" and then exit.
  5. Files --> Delete Directory. Delete the TWRP/BACKUPS/YOURDEVICEUNIQUEID/weeklybackup. Check "Recurse." In order to get your device's unique ID, you need to simply navigate to TWRP/BACKUPS using any file explorer. You should have already made a previous backup, as TWRP will automatically create this folder. (Note: since you likely don't have a weeklybackup folder already here, it will likely fail the first time. You can easily just skip this action and make a backup once with this name, and then it'll work from then onwards).
  6. Plugin --> Secure Settings. Go to Root Settings --> Reboot Options --> Reboot Recovery.
  7. Exit out the task editing, and now add a few more contexts to make the backup run at an appropriate time. I added a power context so it only occurs when my phone is charging; I added a date context so it only runs on Sunday; and I also added a WiFi connected context so it only occurs when I'm at home.
qzAJeR0.jpg


That's it! This should satisfy the average TWRP user. MAKE THOSE BACKUPS!
 
Last edited:

rambo69

Senior Member
Apr 13, 2011
282
49
Tilburg
For me it doesn't work
It's boots to recovery
Then it bootloops with message
"formatting cache using ext4"

Edit* I'm on 6.0.1 with TWRP 3.0
 
Last edited:

Giblet-dono

Senior Member
Mar 24, 2009
1,465
675
OnePlus 7T Pro
OnePlus Nord
No idea.. what I DO know is that this script gave me a hang on boot :<
Make a complete backup before running the script for the first time!
I am not sure yet why, but maybe someone (OP?) can help me out here..
I'm using an HTC One M8 with Marshmallow rom (ViperOneM8) which is stock HTC Sense based.
I use TWRP 2.7.8.0
 

Follow_and_Feel

Senior Member
Apr 7, 2014
528
216
Montabaur
So as I stated above this worked pretty well for me (it made the backup, rebooted me), but if I now go into tasker the "make-backup"-profile is gone?! This seems pretty pointless, if I have to set up the whole thing every time I use it ^^
 
Last edited:

Arstoien

Senior Member
Oct 31, 2012
1,437
1,508
So as I stated above this worked pretty well for me (it made the backup, rebooted me), but if I now go into tasker the "make-backup"-profile is gone?! This seems pretty pointless, if I have to set up the whole thing every time I use it ^^

Obviously you need to exit Tasker once, to save the profile. If you enter all actions in the task and run the task manually, it'll restart your phone without Tasker having saved what you've done.
 

bagarwa

Senior Member
Feb 18, 2012
1,178
2,745
Bothell
play.google.com
I do my backup to a usb-otg drive though. What do I need to modify in this profile to make sure that the backup gets stored only on the otg drive and skips entirely if no otg drive is mounted?
 

Contivity

Senior Member
Jan 8, 2008
488
126
Anaheim
lifein0and1.com
No idea.. what I DO know is that this script gave me a hang on boot :<
Make a complete backup before running the script for the first time!
I am not sure yet why, but maybe someone (OP?) can help me out here..
I'm using an HTC One M8 with Marshmallow rom (ViperOneM8) which is stock HTC Sense based.
I use TWRP 2.7.8.0

Did you try to wait longer? I thought mine also bootloop-ed until I remember that the wipe cache & wipe dalvik command would cause the phone to take a while to load. I waited around 10 minutes.

---------- Post added at 05:18 PM ---------- Previous post was at 05:18 PM ----------

I do my backup to a usb-otg drive though. What do I need to modify in this profile to make sure that the backup gets stored only on the otg drive and skips entirely if no otg drive is mounted?
Did you try: http://wiki.rootzwiki.com/OpenRecoveryScript

---------- Post added at 05:22 PM ---------- Previous post was at 05:18 PM ----------

@MishaalRahman I found that you don't have to chmod 0777 in the beginning and you don't have to use "Secure Settings".

I tested to touch the file in tasker and it created the file just fine.

In regards to Secure Settings, I added the task code -> run shell -> 'reboot recovery'
 

bagarwa

Senior Member
Feb 18, 2012
1,178
2,745
Bothell
play.google.com

That was very educational. Thanks for the link.

Do you know what would be the path for usb-otg?? Would the command be mount usb-otg or something different?

Also, I don't see an option of backing up vendor partition. I do need to backup that, because without that, a restore causing unlocking issue. (Nexus 6P)

Lastly, does Tasker have a way to detect whether a usb drive is plugged in? I would like to add that as one of the trigeers.
 

Contivity

Senior Member
Jan 8, 2008
488
126
Anaheim
lifein0and1.com
That was very educational. Thanks for the link.

Do you know what would be the path for usb-otg?? Would the command be mount usb-otg or something different?

Also, I don't see an option of backing up vendor partition. I do need to backup that, because without that, a restore causing unlocking issue. (Nexus 6P)

Lastly, does Tasker have a way to detect whether a usb drive is plugged in? I would like to add that as one of the trigeers.

OTG Path - I won't know and can't test right now
Tasker detect USB - you can do a file test and set the variable. then you can use that as trigger or if clause.
 
Lastly, does Tasker have a way to detect whether a usb drive is plugged in? I would like to add that as one of the trigeers.

Try using a file explorer to view the path with the usb plugged in. That's what I did to get the full path of my sd card for this to work properly. So far, so good.

---------- Post added at 01:29 AM ---------- Previous post was at 12:33 AM ----------

[*]Code --> Run Shell. This is the code you'll need:
Code:
chmod 0777 /cache/recovery
echo "wipe cache\nwipe dalvik\nbackup SDBOM weeklybackup \ncmd reboot" > /cache/recovery/openrecoveryscript

I don't have much experience with running scripts. Is it safe to leave out the "/nwipe dalvik" part? I assume that would just skip wiping Dalvik?
 
  • Like
Reactions: bagarwa

Arstoien

Senior Member
Oct 31, 2012
1,437
1,508
FYI here are my additions to the Profile:
• Instead of only Sunday, run it every day but execute the backup only if the last backup is at least 1 week old. This removes the chance of having a backup too old because you weren't home Sunday, or you weren't charging that night, etc.
• When booting (only if rebooted from the profile) enter the sim pin code, so you wouldn't miss any emergency phone calls/sms
• If next clock alarm is too soon (say closer than 3 hrs), then don't start a backup
• Log the time and date of backups to a custom log file.
• If by any chance the screen is on while Profile conditions are met, delay the backup by 15 min, so no work is accidentally lost.

If anyone's interested, I'll leave more detailed info on each of the points.
 

Top Liked Posts

  • There are no posts matching your filters.
  • 30
    It's good practice to run a full backup of your ROM and user data every once in awhile, in case you flash something and get into a bootloop. Thanks to TWRP's scripting capabilities, we can trigger a weekly backup.

    Prerequisites

    1. Root Access

    jfH9PiZ.jpg

    Instructions

    1. Create a new Profile named TWRP Backup.
    2. Create a Time context, and choose a time at night you want the TWRP backup to take place. For me, I made it at 4:00AM. To pick a single time, check both "From" and "To" and put the exact same time in both.
    3. No need to name the task anything, just enter its configuration.
    4. Code --> Run Shell. This is the code you'll need:
      Code:
      chmod 0777 /cache/recovery
      echo "wipe cache\nwipe dalvik\nbackup SDBOM weeklybackup \ncmd reboot" > /cache/recovery/openrecoveryscript
      Be VERY sure that this is typed exactly as such! Tick "Use Root" and then exit.
    5. Files --> Delete Directory. Delete the TWRP/BACKUPS/YOURDEVICEUNIQUEID/weeklybackup. Check "Recurse." In order to get your device's unique ID, you need to simply navigate to TWRP/BACKUPS using any file explorer. You should have already made a previous backup, as TWRP will automatically create this folder. (Note: since you likely don't have a weeklybackup folder already here, it will likely fail the first time. You can easily just skip this action and make a backup once with this name, and then it'll work from then onwards).
    6. Plugin --> Secure Settings. Go to Root Settings --> Reboot Options --> Reboot Recovery.
    7. Exit out the task editing, and now add a few more contexts to make the backup run at an appropriate time. I added a power context so it only occurs when my phone is charging; I added a date context so it only runs on Sunday; and I also added a WiFi connected context so it only occurs when I'm at home.
    qzAJeR0.jpg


    That's it! This should satisfy the average TWRP user. MAKE THOSE BACKUPS!
    8
    /me raises his hand

    I've been meaning to get around to creating my own version of this profile to suit my needs. Some of the things you mentioned are of interest; however they are more advanced than my current knowledge. I've gotten pretty good at using If, Else and Stop If statements involving simple variables, though. Bring it on!

    Sent from my LG-D850 using XDA Labs
    Ok, but I'll be a little shorter in description, hope it's still clear:

    Every Day, if last backup is older than one week
    – if you want to save more of your last backups, look at my previous post. My current setup deletes the last backup and creates the new one (if older than a week), as described in op.
    – Here are the changes:

    1. Remove Sunday context from profile. That way the profile is only triggered by the time context, and it'll trigger every day.
    2. Test File: type Modified data TWRP/BACKUPS/YOURUNIQUEID/weeklybackup/ store result in %last_backup_mod_date
    3. Variable set: name %days_since to (%TIMES-%last_backup_mod_date) /60/60/24 do maths checked
    4. Stop: if %days_since < 6

    Enter PIN on reboot (only from this profile, not always)
    This needs some more setup. We'll simulate the touches of the consecutive numbers of your SIM PIN, so first we need to see what these coordinates are. Turn on Pointer Location in Developer settings. Now write down the X and Y coordinates of your numbers (you'll see them on the top of the screen).

    In the profile task you'll only have to add one action before the Reboot action:
    Variable set: name %TaskerReboot to 1

    Then make a new profile with contexts:
    1. Event: Device Boot

    2. Variable Value %TaskerReboot~1

    and the following actions in the task:

    1. Run Shell: command input keyevent KEYCODE_POWER use root checked if %SCREEN ~ off
    (this simulates a power button click, if the screen is off. So in other words turns on the screen. I'm not sure how many devices this will work on, but if it doesn't work, try SecureSetting Wake screen.

    2. Run Shell: command
    Code:
    input tap 540 910
    input tap 540 910
    input tap 540 910
    input tap 540 910
    use root checked
    (Each line simulates one tap. So these 4 lines simulate 4 taps of the number 3 on my screen. Change the numbers for your SIM PIN coordinates. The first is X and the second – Y.)

    3. Run Shell: command input keyevent KEYCODE_POWER use root checked if %SCREEN ~ off
    (that's just to turn off the screen. You can also wait for the screen time out.)

    4. Variable clear: name %TaskerReboot

    Don't trigger if too close to clock alarm
    Note that, if you are using "Use Reliable Alarms" in Tasker preferences, that may interfere, so you may have to turn it off with an action in this profile and back on after the alarm check. You'll need a plugin to read the time of next alarm. A good and free one is called ClockTask. After having the plugin installed, in the very beginning of the task, enter these actions:

    1. Next alarm change (ClockTask plugin): configuration Get next

    2. Variable Set: name %hrs_to_next_alarm to (%ctnext-%TIMES)/60/60 do maths checked

    3. Stop: if %hrs_to_next_alarm < 3.5 and %hrs_to_next_alarm > 0
    (change the values to your preference)

    Log the time and date of backup to a custom file
    Not needed for the profile to work, but if you're interested, that's easy. Just create an action "write file" and use date, time, and whatever you want there.

    Avoid rebooting if screen is on
    You can play with some scenes asking you what to do, if the screen is on when the profile triggers but I simply made it wait 15 minutes if the screen is on. Somewhere in the beginning of the task add the following actions:

    1. If: %SCREEN ~ on label Recheck Screen
    2. Flash: text TWRP backup pushed back 15 mins
    3. Wait: 15 mins
    4. Goto: type Action Label label Recheck Screen
    5. End if

    Hope that helps. Cheers!
    8
    I just made a modified version of the task myself with a few added functionality like:
    • Performs the backup on specific days of week
    • Notifies you when the backup process is about to take place
    • Gives you options to proceed with, cancel or postpone(by a few minutes) the backup process.
    • Proceeds with the backup process anyway if the user remains unresponsive for greater than 1 minute (like when you are sleeping)
    It handles only one backup (it deletes the old one before it starts taking the latest one).
    By default, the backup is taken on Tuesdays and Fridays at 3:30 AM. You can obviously change that.

    Screenshot_20161029-191017.jpg Screenshot_20161029-191025.jpg
    View attachment nandroid-backup.zip

    Edit : Fixed bugs related to profile contexts and repetitive executions of the task on selected day(s).
    5
    Are you saying that you know how to do this with tasker? If so, how? That's the real question.
    K, sure. Turns out the names method is simpler but requires more setup in the backup naming. I'll add in the end of the post the other method.

    Method 1 – Check the Name Backup Folders
    First you need to add the date to the backup dir's name. But it has to be formatted in a way that, when sorted alphabetically, it'll sort the backups chronologically. So the format I'm showing is yyyy-mm-dd (same as %DATE but reversed, so years are in front). Here's the actions to set up that name:
    Code:
    1. Variable Set: [i]name[/i] [B]%date[/B] [i]to[/i] [B]%DATE[/B]
    2. Variable Split: [i]name[/i] [B]%date[/B] [i]splitter[/i] - [COLOR="DimGray"](dash)[/COLOR]
    3. Variable Set: [i]name[/i] [B]%date[/B] [i]to[/i] [B]%date3-%date2-%date1[/B]
    Following that, instead of Tasker's built-in %DATE, you can use %date, and it'll represent the current date in yyyy-mm-dd format (so today is 2016-03-11).
    After that definition, add the tasks, described in the OP with the following corrections:

    1. instead of backup SDBOM weeklybackup use backup SDBOM Backup-%date – adding the %date variable, set up above
    2. instead of the Files --> Delete Directory action, you need to add several actions, which will delete all the backup folders that are not the 3 latest. The logic behind the loop is get the names of all directories in alphabetical order (which arranges them from oldest to newest), and deleting all folders except the last 3 (which are the 3 latest). Here are the actions:
    Code:
    1. List Files: [i]dir[/i] [B]TWRP/BACKUPS/YOURDEVICEUNIQUEID[/B] [i]variable[/i] [B]%dirs[/B]
         [COLOR="DimGray"] (where YOURDEVICEUNIQUEID needs to be set appropriately, as described in OP)[/COLOR]
    
    2. Variable Set: [i]name[/i] [B]%end[/B] [i]to[/i] [B]%dirs(#)-3[/B] [i]do maths[/i] [B]checked[/B]
         [COLOR="DimGray"](That'll gives the number of directories-3. Change that number to the amount of backups you want to save)[/COLOR]
    
    3. For: [i]variable[/i] [B]%del[/B] [i]items[/i] [B]%dirs(:%end)[/B]
         [COLOR="DimGray"](For each of the directories from the first to the n-th last, defined in the action above, perform the following actions)[/COLOR]
    
    4. Delete Directory: [i]dir[/i] [B]%del[/B] [i]recurse[/i] [B]checked[/B]
         [COLOR="DimGray"](this deletes the directory taken from the action above)[/COLOR]
    
    5. End For
         [COLOR="DimGray"](ends the loop)[/COLOR]
    And then you can set the SecureSettings Reboot to Recovery action, as described in OP.

    Hope that helps. Haven't tested the whole thing with TWRP backups and reboot – only quickly test with some made-up directories. If you want to use this, it's not a bad idea to test yourself in a separate task before going all-in and incorporating everything in the main task. Though, that said, it should work fine.

    PS: A quick note: TWRP will make a backup after that, so if you leave 3 backups, then you'll end up with 4. So keep that in mind, because you may not have enough space and in that case TWRP wont make the backup.

    Edit: Method 2 – Check the Modified Date of Backup Folders
    So the other way is to just go through each of the folders and see if their date of modification is older than X days. If so, then delete them. This method doesn't require that you've set up the names in any specific way; only that they're in separate folders, so you can just add %DATE without the other steps in the post above. This method goes as follows:

    Instead of the Files --> Delete Directory action, you need to add several actions, which will delete all the backup folders that are not the 3 latest. Here are the actions:
    Code:
    1. List Files: [i]dir[/i] [B]TWRP/BACKUPS/YOURDEVICEUNIQUEID[/B] [i]variable[/i] [B]%dirs[/B]
         [COLOR="DimGray"](where YOURDEVICEUNIQUEID needs to be set appropriately, as described in OP)[/COLOR]
    
    2. For: [i]variable[/i] [B]%processed_item[/B] [i]items[/i] [B]%dirs()[/B]
         [COLOR="DimGray"](starting the loop – going through each of the backup directories)[/COLOR]
    
    3. Test File: [i]type[/i] [B]Modified[/B] [i]data[/i] [B]%processed_item[/B] [i]store result in[/i] [B]%mod_date[/B]
         [COLOR="DimGray"](for each of the directories check the modified date)[/COLOR]
    
    4. Variable Set: [i]name[/i] [B]%days_old[/B] [i]to[/i] [B](%TIMES - %mod_date) / (60*60*24)[/B] [i]do maths[/i] [B]checked[/B]
         [COLOR="DimGray"](the result is in seconds, so here we convert it to days)[/COLOR] 
    
    5. Delete Directory: [i]dir[/i] [B]%processed_item[/B] [i]recurse[/i] [B]checked[/B] [i]if[/i] [B]%days_old > 14[/B]
         [COLOR="DimGray"](Delete the directory, if the modified date is from more than 14 days ago. Change the days to whatever you want)[/COLOR]
    
    6. End For

    Not all that thoroughly tested but should work fine. Again – test yourself first before using it.
    3
    No idea.. what I DO know is that this script gave me a hang on boot :<
    Make a complete backup before running the script for the first time!
    I am not sure yet why, but maybe someone (OP?) can help me out here..
    I'm using an HTC One M8 with Marshmallow rom (ViperOneM8) which is stock HTC Sense based.
    I use TWRP 2.7.8.0

    Did you try to wait longer? I thought mine also bootloop-ed until I remember that the wipe cache & wipe dalvik command would cause the phone to take a while to load. I waited around 10 minutes.

    ---------- Post added at 05:18 PM ---------- Previous post was at 05:18 PM ----------

    I do my backup to a usb-otg drive though. What do I need to modify in this profile to make sure that the backup gets stored only on the otg drive and skips entirely if no otg drive is mounted?
    Did you try: http://wiki.rootzwiki.com/OpenRecoveryScript

    ---------- Post added at 05:22 PM ---------- Previous post was at 05:18 PM ----------

    @MishaalRahman I found that you don't have to chmod 0777 in the beginning and you don't have to use "Secure Settings".

    I tested to touch the file in tasker and it created the file just fine.

    In regards to Secure Settings, I added the task code -> run shell -> 'reboot recovery'