Automatically Backup Your Tasker Configuration After A Change To Your Google Drive 1.1

Automatically upload Tasker backups to your Google Drive account the instant you make a change.

  1. crepusculi
    Hello all.

    My daily driver is a Nexus 5x, and I'm actually on my second device. My first went through the ol' bootloop issue, and as a result, I lost a significant amount of my Tasker setup, and had to manually recreate many profiles from an old backup I had, including a new one I was really proud of at the time. Because of this, I decided to create a backup solution that uploads backups to my Google Drive any time a change is made in Tasker, as I've never been able to get Tasker's built in "Android Cloud Backup" feature to work properly. Please keep in mind you'll need to provide the parent folder ID once you import, as there's only a dummy value provided.

    PREREQUISITES
    You will need to have the AutoWeb plugin installed.

    N.B.
    When describing the configuration of any actions, if I don't mention a field, leave it at its default value.

    First, we'll need to ensure Tasker is actually making local backups on your phone. To do this, open the main Tasker menu by tapping the three dots in the upper right hand side of the screen, then select "Preferences." Go to the "MISC" tab, and ensure "Local Auto-Backup Max Age" is set to anything other than "No Auto-Backups." Personally, I have mine set to 2 days.

    Next, we'll create the trigger event to fire this profile. We're going to use the "File Modified" Event trigger, and point it where Tasker stores it's auto-backups. Whenever a new backup is made, this will fire.

    Code (Text):
    Event: File Modified [ File:Tasker/configs/auto/. ]
    On to the actions. First, we'll need to set a couple of variables, so the remaining actions can see the folder where the auto-backups are stored. The first is to set the folder path, and the second is to tell Tasker that the path is a folder as opposed to a file.

    Code (Text):
    A1: Variable Set [ Name:%directory_path To:/sdcard/Tasker/configs/auto Recurse Variables:Off Do Maths:Off Append:Off ]
    A2: Variable Set [ Name:%type To:dir Recurse Variables:Off Do Maths:Off Append:Off ]
    Now we're going to need to get a list of the files in the directory, sorted by creation/modification date, and write that list to an array.

    Code (Text):
    A3: List Files [ Dir:%directory_path Match: Include Hidden Files:Off Use Root:Off Sort Select:Modification Date Variable:%file_list ]

    We're also going to set the "highest time" to 0. This is a placeholder value that will be changed later.

    Code (Text):
    A4: Variable Set [ Name:%highest_time To:0 Recurse Variables:Off Do Maths:Off Append:Off ]
    Next, we need to run through each item in the array we created above, so we'll need a "For" statement.

    Code (Text):
    A5: For [ Variable:%file Items:%file_list() ]
    First, we'll need to get the modified date/time of each file, and then verify it is a file instead or a directory, and store those results in their own variables.

    Code (Text):
    A6: Test File [ Type:Modified Data:%file Store Result In:%mod_date_time Use Root:Off ]
    A7: Test File [ Type:Type Data:%file Store Result In:%file_type Use Root:Off ]
    When creating backups, Tasker always names the file "userbackup.xml". This would be easy, except each backup is stored in its own folder, date/time stamped down to the second, twice. I.E., it looks like this:
    Code (Text):
    20180510.14.40.24-20180510.22.30.06
    What we'll do now is set a variable to that path, with "userbackup.xml" appended to it, ONLY for the most recent backup ~folder~ created. We are also going to set %highest_time to the modified date time of each folder.

    Code (Text):
    A8: Variable Set [ Name:%output To:%file/userbackup.xml Recurse Variables:Off Do Maths:Off Append:Off ] If [ %mod_date_time > %highest_time & %file_type eq %type ]
    A9: Variable Set [ Name:%highest_time To:%mod_date_time Recurse Variables:Off Do Maths:Off Append:Off ] If [ %mod_date_time > %highest_time & %file_type eq %type ]
    As the process runs through each file, it will overwrite the %output variable as it comes across more recent items.

    We'll need to end the for loop now.

    Code (Text):
    A10: End For
    For the remainder of the actions, we're going to use the AutoWeb plugin, so ensure you have it [installed]. Once you do, you'll need to add the "Google Drive" web service. To do this, open the app, and select "Browse Web Services." Scroll down to Google Drive, click it, and select "Import API."

    Back in our task list, we're going to need to list all the files in the folder in your Drive where you want to store the backups. This will allow us to only have the most recent backup in that folder.

    Select AutoWeb from the plugin menu, and go into the configuration. You'll want to select the Google Drive API, and the "Files: list" for the API action.

    Go into filters, and check the "Contains All" box before setting the filters themselves.

    You'll want to set the "Trashed filter" to "false" (without the quotes), and the "Title filter" to "backup.xml" (again, without quotes).

    You will also need to set the "Parents ID filter." This is the ID of the parent folder where you want to store the backups. To find this ID, log into Google Drive on the web, and navigate to the folder where you are going to store the backups. Look at the URL. the final string of characters is the folder ID. For example:
    Code (Text):
    https://drive.google.com/drive/u/0/folders/[PARENT FOLDER ID HERE]
    Now, you'll need to set the Ouput variable. Select "Output" and then "Fields to Get." Scroll through the list and select "File ID." This will create an array of all the files in folder in the %fileid variable name.

    N.B. Moving forward, the assumption is that there is only one file in that folder.

    Code (Text):
    A11: AutoWeb Web Service [ Configuration:API: Google Drive
    API Action: Files: list
    Contains All: true
    Filter Trashed: false
    Filter Parents Id: [FOLDER ID HERE]
    Filter Title: backup.xml Timeout (Seconds):120 ]
    Next, we'll need to delete that old backup file. Again, select AutoWeb from the plugin menu, and choose "Google Drive" as the API. This time, choose "Files: trash" as the API action. All you need to do here is set the File ID of the file to be trashed, which we can get from our previous array. Set the "File ID" input to "%fileid(1)" (without quotes).

    Code (Text):

    A12: AutoWeb Web Service [ Configuration:API: Google Drive
    API Action: Files: trash Timeout (Seconds):120 Continue Task After Error:On ]
    FINALLY, we can upload the local backup file to Google Drive. select AutoWeb from the plugin menu, and choose "Google Drive" as the API. For the API Action, choose "Files: insert." Everything here is done under the "Inputs" section.

    Code (Text):

    File: %output
    File Name: %DATE-%TIME-backup.xml
    Parent ID: [PARENT FOLDER ID HERE]
    Code (Text):

    A13: AutoWeb Web Service [ Configuration:API: Google Drive
    API Action: Files: insert
    Starred: false
    Parent Id: [FOLDER ID HERE] Timeout (Seconds):120 ]
    And there you go! You now have an auto-backup to Google Drive setup, so in case your phone goes kaput for any reason, you don't need to recreate your Tasker setup! Please let me know if you have any questions, and I'll be happy to answer them!

    -Crepusculi

Recent Updates

  1. Forgot to scrub MY parent folder ID.

Recent Reviews

  1. artpc
    artpc
    5/5,
    Version: 1.1
    Good work...