• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Root Nandroid Backup Script

9to5cynic

Android Expert
Feb 20, 2011
4,872
1,767
/home/
Okay, so this is a script to backup the nandroids on your phone.
Requirements:

  • Linux (it's a bash script)
  • USB mode for your phone

I thought of this as kinda an automated process, so a cronjob.

It seems to be working with both AMON RA and CLOCKWORKMOD recoveries. I sent it off to marc12868 to see if clockwork works with someone who actually *has* clockwork installed [if he says it doesn't work, then i'll fix it]. I just set up a folder to mimic the folder structures. It should work.

If you want to run it each time, which works, then you only need to change the two variables phone and backup. $phone is just where your device is mounted. Mine was /media/0A00-1234 or something like that. $backup is the (you guessed it) backup directory.

Steps

  • Save this script somewhere ($HOME/nandroid.sh)
  • grant it execute permissions (chmod +x nandroid.sh)
  • either run it (./nandroid.sh) or
  • get it working with cron (below)
CRON:
Code:
crontab -e
Okay, so if that's your first cronjob, it'll ask you to pick an editor, 2 (default) is fine (nano), but if you're hardcore go with vi

then add this line to it (at the bottom)
Code:
* * * * * /home/YOURNAME/nandroid.sh


Nandroid Backup Script:
Code:
#!/bin/bash
#############
#9to5cynic
#provided as is and all that shit.
#####################################################
# Nandroid Backup Script
# This will copy, tar.gz and remove nandroids from your phone's SD card
# and place them in your backup directory.
######################################################
#Okay, to set this up, just change your phone variable to the what your device is
# mine was something like /media/A001-3214 or something similar.  
# backup is just your backup directory.
######################################################
phone='/media/1gb'
backup='/home/kyle/nandroid/'
######################################################
df -k | grep $phone &>/dev/null
grep_phone=$?

if [[ $grep_phone -eq 0  ]]
then
    # phone lock file, see if this is running alread.
    ls /tmp | grep "backup.lock" 1>/dev/null
    grep_lock=$?
        if [[ $grep_lock -eq 0 ]]
        then
            echo ":::: FILE LOCK FOUND ::::"
            exit 10
        else
            touch /tmp/backup.lock
            # looking for amon-ra style recoveries - /sdcard/nandroid/SERIAL-NO/
            ls $phone | grep 'nandroid' 1>/dev/null
            type=$?
            if [[ $type -eq 0 ]]
            then
                uid=$( ls ${phone}/nandroid/ )
                phonepath="${phone}/nandroid/${uid}"
                unset type
            fi
            # looking for clockworkmod recoveries - /sdcard/clockworkmod/backup/
            ls $phone | grep "clockwork" 1>/dev/null
            type=$?
            if [[ $type -eq 0 ]]
            then
                phonepath="${phone}/clockworkmod/backup/"
                unset type
            fi
            mkdir /tmp/nand
            #checks the contents of the backup folder, so it doesn't copy already backed up files 
            ls ${backup} > /tmp/backup.list
            # copy files from sd card to temp folder
            content=$( ls $phonepath )
            for line in $content
            do
                grep ${line}.tar.gz /tmp/backup.list 1>/dev/null
                grep_check=$?
                if [[ $grep_check -eq 1 ]]
                then
                    cp -Rp ${phonepath}${line} /tmp/nand 1>/dev/null
                fi
            done
            nand=$( ls /tmp/nand/)
            pwd=$(pwd)
            #tar.gz the files 
            for line in $nand
            do
                cd /tmp/nand/    
                tar czfv ${backup}${line}.tar.gz ${line} 1>/dev/null
                rm -r ${phonepath}${line}
            done
            #house cleaning, remove temp files and other shit.
            cd $pwd
            unset {phone,backup,pwd,uid,nand,grep_check,grep_lock,grep_phone}
            rm /tmp/backup.lock
            rm /tmp/backup.list
            rm -r /tmp/nand
            exit 20
        fi
else
    echo "::::::::::PHONE NOT FOUND!:::::::::::::"
    exit 0
fi
I'm sure there are a lot of better ways to do this, but I don't know them. :vroam:


more words
Well, I tried this several times on my computer and with my phone, seems to work fine. It'll backup the nandroid folder to the backup folder (after tar.gz). It also removes all the contents of the nandroid folder, so you might want to copy one back over (after un tar.gz'ing it)....

Code:
tar zxvf 2012-04-30.tar.gz
but, if it sees that you already have a backup of a nandroid, it won't copy them so - that's nice.
:five:
 
:D Let me know how it goes. And if you just want to try it, you can skip the cron step and just do it via terminal .... ./backup.sh

If you have a lot of nandroids, it'll be slow.... (it copies the nandroid to /tmp, then from /tmp it goes to your backup drive and compresses it) ... but that's why I thought having it start by itself would be nice, it'll copy the nandroid over while you charge or something.

just some time trials - I copied two nandroids (total: 1.2 GB) to the tar.gz files (680MB) in about 4-7 minutes [I didn't see exactly when I started it]
 
  • Like
Reactions: ocnbrze
Upvote 0
one of the main reason i wrote mine in script was so that while it was doing the backups I could continue to do other things. Do note that even though you can run these as a normal user its best practice to run them as the root user. root gets preference. Besides it will prevent any errors on stuff you don't have permission to do. Once I have mine in the final stages I'll post it. Right now its in semifinal stages. I still am finding ways to clean it up and improve it.

Also Note Windows Users You can Use BATCH to accomplish alot of the same thing however it will be different than using Linux and the Bash system.
 
  • Like
Reactions: ocnbrze
Upvote 0
I don't know what any of this is.... BUT great job! :D


All kidding aside... this looks like it would be very useful to our linux users.
;)

Also, I'm thinking of adding on to it so it will check to see if that backup file already exists...

What I mean by that is say you have your Stock 2.2 backup (showing my age ;)), no point in backing that up every time, once it is in the backup dir, it should be ignored. So, I think that's my next step.
 
Upvote 0
;)

Also, I'm thinking of adding on to it so it will check to see if that backup file already exists...

What I mean by that is say you have your Stock 2.2 backup (showing my age ;)), no point in backing that up every time, once it is in the backup dir, it should be ignored. So, I think that's my next step.

Good idea thats why in mine I'm trying to remove them from the card after backing them up have run into a few snags with that it maybe because i'm not running it as root though i'm going to run it as root and see if that solves the issue. was also thinking of doing a check to make sure you were the root user to avoid issues with the script.
 
Upvote 0
Good idea thats why in mine I'm trying to remove them from the card after backing them up have run into a few snags with that it maybe because i'm not running it as root though i'm going to run it as root and see if that solves the issue. was also thinking of doing a check to make sure you were the root user to avoid issues with the script.

removing them isn't a bad idea, but I'd hate to remove *all* of them, then have to remove the sd card to get a nandroid back on there.... maybe something where it will remove all but one..?

checking for root would be pretty easy - something like this:
Code:
var=$(id -u)
if [[ $var -eq 0 ]]
then
echo 'root'
else
echo 'not root'
fi
Though, I ran mine from my user account if I recall correctly...
But, I wrote the majority of it in LM (as opposed to debian) and it gave an error with the df -k line - something about needing root permissions to analyze a part of a hidden file for root.... I ignored it. I think the script still ran without it.

--

Okay, I have it remove each file now, but it still leaves the nandroid folder empty (no backup) - I could get it to keep one (but that would need to be assigned by the script and checked in a loop...).

Also, I don't think I said this, but it *should* work on any phone that uses a similar nandroid structure. IE: /device/nandroid/#####/ABC-######/ It doesn't look at the name of the directories in bold.
 
Upvote 0
removing them isn't a bad idea, but I'd hate to remove *all* of them, then have to remove the sd card to get a nandroid back on there.... maybe something where it will remove all but one..?

checking for root would be pretty easy - something like this:
Code:
var=$(id -u)
if [[ $var -eq 0 ]]
then
echo 'root'
else
echo 'not root'
fi
Though, I ran mine from my user account if I recall correctly...
But, I wrote the majority of it in LM (as opposed to debian) and it gave an error with the df -k line - something about needing root permissions to analyze a part of a hidden file for root.... I ignored it. I think the script still ran without it.

--

Okay, I have it remove each file now, but it still leaves the nandroid folder empty (no backup) - I could get it to keep one (but that would need to be assigned by the script and checked in a loop...).

Also, I don't think I said this, but it *should* work on any phone that uses a similar nandroid structure. IE: /device/nandroid/#####/ABC-######/ It doesn't look at the name of the directories in bold.
Honestly I keep at most up to 3 Nandroids on the Phone and only up to about 6 on the computer after that they are really to old for me to restore. I have updates almost every day for this app or that app. by the end of the week I have updated about 15 to 20 apps. so really when I restore a nandroid its generally nothing real old and i only have a small amount of stuff to update again. for the most part I keep titanium backup fresh and keep a good copy of my favorite ROM on the card. This is as important as the Nandroid.

Yeah the root check isn't hard to do. Also if your using bash it shouldn't matter if your using LM, Debian, Ubuntu, or even Fedora its still going to be the same. they have the same bash rules.
 
Upvote 0
Yeah the root check isn't hard to do. Also if your using bash it shouldn't matter if your using LM, Debian, Ubuntu, or even Fedora its still going to be the same. they have the same bash rules.
yeah, they all look for /etc/profile or ~/.bash_profile or ~/.bashrc
but it was with how LM was handling df -k specifically.

df: `/root/.gvfs': Permission denied


but that doesn't actually seem to mess with the script at all. And I am working on getting it to check to see if the files are there in tar.gz format... I'm hoping to have some more time this weekend - but finals are coming up so I'll be a bit busy.

:D

--
(I think I might be getting there, wrote down what I'm thinking on paper - gotta get some sleep)
 
Upvote 0
yeah, they all look for /etc/profile or ~/.bash_profile or ~/.bashrc
but it was with how LM was handling df -k specifically.

df: `/root/.gvfs': Permission denied


but that doesn't actually seem to mess with the script at all. And I am working on getting it to check to see if the files are there in tar.gz format... I'm hoping to have some more time this weekend - but finals are coming up so I'll be a bit busy.

:D

--
(I think I might be getting there, wrote down what I'm thinking on paper - gotta get some sleep)

Finals First We have plenty of time to make this work correctly afterwords ok? Ok.

gvfs is a private file system for network file systems. basically it connects your home folder to the network "privately" I'm not exactly sure how it works but had someone much smarter than me explain it to me (Ok attempted to explain it to me) I got lost so basically told her to stop I didnt need an explaination that bad :D I wish I new where she was now though she was a wiz at all this stuff and the one responsible for getting me into Linux in the first place.

Ok for the rest of you

It shouldnt matter what version of linux your using but what is important that you are using BASH. This also can be done from a live environment however you will need to save it to an external hard drive or even to a thumb drive that has a larger capacity than your SDCARD. You can also expand it to go beyond the nandroid I'll be posting one here soon for you to manipulate for your use. Probably release it under GPL 3 but then again may just put it out there under an honor code we'll see.
 
Upvote 0
Okay, I think I have this working now. It still removes all the backups that you have, but eh...

As of now it'll check the content of the backup folder, and check that against the nandroid folder on your phone. It also will find that phone's serial number (according to the nandroid folder) so that's kinda cool.

If your nandroids match, it'll ignore them (and leave them intact - so that works for the stock backups after you already have it through this script). If they don't match, it'll copy and remove them.

here's the code
Code:
#!/bin/bash
#
# 9to5cynic
# the goal here is to backup the sd card (nandroids) when the usb is plugged in
# and only if the usb is plugged in.  
#
# exit codes: 0 -no usb/phone; 10 -lock file exists; 20 - worked;
# 
# obvious variables, backup directory and usb path ... with nandroid extension...
phone='/media/####-####'
backup='/home/9to5/nandroid/'
df -k | grep $phone &>/dev/null
grep_phone=$?

if [[ $grep_phone -eq 0 ]]
then
    # PHONE FOUND (lock file)
    ls /tmp | grep "backup.lock" 1>/dev/null
    grep_lock=$?
        if [[ $grep_lock -eq 0 ]]
        then 
            echo ":::::FILE LOCK FOUND:::::"
            exit 10
        else
            touch /tmp/backup.lock
            #before we backup, we've gotta find the nandroid subfolder (H...something)
            uid=$( ls ${phone}/nandroid/ ) 
            phonepath="${phone}/nandroid/${uid}"
            # make temp backup dir
            mkdir /tmp/nand
            # Okay, so here is where it'll read the backup folder and compare it the contents.
            ls ${backup} > /tmp/backup.list            
            content=$( ls $phonepath )
            for line in $content
            do
                grep ${line}.tar.gz /tmp/backup.list
                grep_check=$?
                if [[ $grep_check -eq 1 ]]
                then
                    cp -Rp ${phonepath}/${line} /tmp/nand 1>/dev/null
                fi
            done
            # Backup and TARGZ happen here. ;) 
            nand=$( ls /tmp/nand/)
            pwd=$(pwd)
            for line in $nand
            do
                cd /tmp/nand/    
                tar czfv ${backup}${line}.tar.gz ${line}
                rm -r ${phone}/nandroid/${uid}/${line}
            done
            cd $pwd
            # CLEAN UP
            #unset variables and remove shit
            unset {phone,backup,pwd,uid}
            rm /tmp/backup.lock
            rm /tmp/backup.list
            rm -r /tmp/nand
            exit 20
        fi    

else
    echo "::::::PHONE NOT FOUND::::::"
    exit 0
fi

!!!
Remember to change your variables as you see fit. I think I go into that pretty well in the first post.

I just was suggested to make it compatible with clockwork, so I'll be doing that next. Should be simple, but I just turned everything off. Tomorrow!
:five:
 
Upvote 0
Okay, I think I have this working now. It still removes all the backups that you have, but eh...

As of now it'll check the content of the backup folder, and check that against the nandroid folder on your phone. It also will find that phone's serial number (according to the nandroid folder) so that's kinda cool.

If your nandroids match, it'll ignore them (and leave them intact - so that works for the stock backups after you already have it through this script). If they don't match, it'll copy and remove them.

here's the code
Code:
#!/bin/bash
#
# 9to5cynic
# the goal here is to backup the sd card (nandroids) when the usb is plugged in
# and only if the usb is plugged in.  
#
# exit codes: 0 -no usb/phone; 10 -lock file exists; 20 - worked;
# 
# obvious variables, backup directory and usb path ... with nandroid extension...
phone='/media/####-####'
backup='/home/9to5/nandroid/'
df -k | grep $phone &>/dev/null
grep_phone=$?

if [[ $grep_phone -eq 0 ]]
then
    # PHONE FOUND (lock file)
    ls /tmp | grep "backup.lock" 1>/dev/null
    grep_lock=$?
        if [[ $grep_lock -eq 0 ]]
        then 
            echo ":::::FILE LOCK FOUND:::::"
            exit 10
        else
            touch /tmp/backup.lock
            #before we backup, we've gotta find the nandroid subfolder (H...something)
            uid=$( ls ${phone}/nandroid/ ) 
            phonepath="${phone}/nandroid/${uid}"
            # make temp backup dir
            mkdir /tmp/nand
            # Okay, so here is where it'll read the backup folder and compare it the contents.
            ls ${backup} > /tmp/backup.list            
            content=$( ls $phonepath )
            for line in $content
            do
                grep ${line}.tar.gz /tmp/backup.list
                grep_check=$?
                if [[ $grep_check -eq 1 ]]
                then
                    cp -Rp ${phonepath}/${line} /tmp/nand 1>/dev/null
                fi
            done
            # Backup and TARGZ happen here. ;) 
            nand=$( ls /tmp/nand/)
            pwd=$(pwd)
            for line in $nand
            do
                cd /tmp/nand/    
                tar czfv ${backup}${line}.tar.gz ${line}
                rm -r ${phone}/nandroid/${uid}/${line}
            done
            cd $pwd
            # CLEAN UP
            #unset variables and remove shit
            unset {phone,backup,pwd,uid}
            rm /tmp/backup.lock
            rm /tmp/backup.list
            rm -r /tmp/nand
            exit 20
        fi    

else
    echo "::::::PHONE NOT FOUND::::::"
    exit 0
fi

!!!
Remember to change your variables as you see fit. I think I go into that pretty well in the first post.

I just was suggested to make it compatible with clockwork, so I'll be doing that next. Should be simple, but I just turned everything off. Tomorrow!
:five:

Looks good :) I'm still tweaking the one I am working on along with a ton of other things I'm doing.
 
Upvote 0

BEST TECH IN 2023

We've been tracking upcoming products and ranking the best tech since 2007. Thanks for trusting our opinion: we get rewarded through affiliate links that earn us a commission and we invite you to learn more about us.

Smartphones