Nandroid Backup Script


Last Updated: 2012-04-30 20:34:35
  1. 9to5cynic

    9to5cynic Well-Known Member

    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 (Text):
    1.  
    2. crontab -e
    3.  
    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 (Text):
    1.  
    2. * * * * * /home/YOURNAME/nandroid.sh
    3.  


    Nandroid Backup Script:
    Code (Text):
    1.  
    2. #!/bin/bash
    3. #############
    4. #9to5cynic
    5. #provided as is and all that shit.
    6. #####################################################
    7. # Nandroid Backup Script
    8. # This will copy, tar.gz and remove nandroids from your phone's SD card
    9. # and place them in your backup directory.
    10. ######################################################
    11. #Okay, to set this up, just change your phone variable to the what your device is
    12. # mine was something like /media/A001-3214 or something similar.  
    13. # backup is just your backup directory.
    14. ######################################################
    15. phone='/media/1gb'
    16. backup='/home/kyle/nandroid/'
    17. ######################################################
    18. df -k | grep $phone &>/dev/null
    19. grep_phone=$?
    20.  
    21. if [[ $grep_phone -eq 0  ]]
    22. then
    23.     # phone lock file, see if this is running alread.
    24.     ls /tmp | grep "backup.lock" 1>/dev/null
    25.     grep_lock=$?
    26.         if [[ $grep_lock -eq 0 ]]
    27.         then
    28.             echo ":::: FILE LOCK FOUND ::::"
    29.             exit 10
    30.         else
    31.             touch /tmp/backup.lock
    32.             # looking for amon-ra style recoveries - /sdcard/nandroid/SERIAL-NO/
    33.             ls $phone | grep 'nandroid' 1>/dev/null
    34.             type=$?
    35.             if [[ $type -eq 0 ]]
    36.             then
    37.                 uid=$( ls ${phone}/nandroid/ )
    38.                 phonepath="${phone}/nandroid/${uid}"
    39.                 unset type
    40.             fi
    41.             # looking for clockworkmod recoveries - /sdcard/clockworkmod/backup/
    42.             ls $phone | grep "clockwork" 1>/dev/null
    43.             type=$?
    44.             if [[ $type -eq 0 ]]
    45.             then
    46.                 phonepath="${phone}/clockworkmod/backup/"
    47.                 unset type
    48.             fi
    49.             mkdir /tmp/nand
    50.             #checks the contents of the backup folder, so it doesn't copy already backed up files
    51.             ls ${backup} > /tmp/backup.list
    52.             # copy files from sd card to temp folder
    53.             content=$( ls $phonepath )
    54.             for line in $content
    55.             do
    56.                 grep ${line}.tar.gz /tmp/backup.list 1>/dev/null
    57.                 grep_check=$?
    58.                 if [[ $grep_check -eq 1 ]]
    59.                 then
    60.                     cp -Rp ${phonepath}${line} /tmp/nand 1>/dev/null
    61.                 fi
    62.             done
    63.             nand=$( ls /tmp/nand/)
    64.             pwd=$(pwd)
    65.             #tar.gz the files
    66.             for line in $nand
    67.             do
    68.                 cd /tmp/nand/    
    69.                 tar czfv ${backup}${line}.tar.gz ${line} 1>/dev/null
    70.                 rm -r ${phonepath}${line}
    71.             done
    72.             #house cleaning, remove temp files and other shit.
    73.             cd $pwd
    74.             unset {phone,backup,pwd,uid,nand,grep_check,grep_lock,grep_phone}
    75.             rm /tmp/backup.lock
    76.             rm /tmp/backup.list
    77.             rm -r /tmp/nand
    78.             exit 20
    79.         fi
    80. else
    81.     echo "::::::::::PHONE NOT FOUND!:::::::::::::"
    82.     exit 0
    83. fi
    84.  
    85.  
    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 (Text):
    1.  
    2. tar zxvf 2012-04-30.tar.gz
    3.  
    4.  
    but, if it sees that you already have a backup of a nandroid, it won't copy them so - that's nice.
    :five:

    Advertisement
    Granite1, argedion, karandpr and 2 others like this.
  2. ocnbrze

    ocnbrze DON'T PANIC!!!!!!!!! Moderator

    ohhh nice!!!!
    gonna have to play with this this weekend.
    9to5cynic likes this.
  3. 9to5cynic

    9to5cynic Well-Known Member

    :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]
    ocnbrze likes this.
  4. argedion

    argedion The TechnoFrog VIP Member

    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.
    ocnbrze likes this.
  5. argedion

    argedion The TechnoFrog VIP Member

    Not sure about most of the xplorers today but I know Nautilus has gone pretty much graphical so you don't get the classic text address bar that you used to. With Nautilus all you have to do is press ctrl + L and it will show up for the session others may offer similar shortcuts such as this.
    ocnbrze likes this.
  6. Rxpert83

    Rxpert83 Dr. Feelgood Moderator

    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.
    9to5cynic likes this.
  7. 9to5cynic

    9to5cynic Well-Known Member

    ;)

    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.
    Rxpert83 and argedion like this.
  8. argedion

    argedion The TechnoFrog VIP Member

    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.
  9. 9to5cynic

    9to5cynic Well-Known Member

    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 (Text):
    1. var=$(id -u)
    2. if [[ $var -eq 0 ]]
    3. then
    4. echo 'root'
    5. else
    6. echo 'not root'
    7. 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.
  10. argedion

    argedion The TechnoFrog VIP Member

    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.
  11. 9to5cynic

    9to5cynic Well-Known Member

    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)
  12. argedion

    argedion The TechnoFrog VIP Member

    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.
  13. 9to5cynic

    9to5cynic Well-Known Member

    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 (Text):
    1.  
    2. #!/bin/bash
    3. #
    4. # 9to5cynic
    5. # the goal here is to backup the sd card (nandroids) when the usb is plugged in
    6. # and only if the usb is plugged in.  
    7. #
    8. # exit codes: 0 -no usb/phone; 10 -lock file exists; 20 - worked;
    9. #
    10. # obvious variables, backup directory and usb path ... with nandroid extension...
    11. phone='/media/####-####'
    12. backup='/home/9to5/nandroid/'
    13. df -k | grep $phone &>/dev/null
    14. grep_phone=$?
    15.  
    16. if [[ $grep_phone -eq 0 ]]
    17. then
    18.     # PHONE FOUND (lock file)
    19.     ls /tmp | grep "backup.lock" 1>/dev/null
    20.     grep_lock=$?
    21.         if [[ $grep_lock -eq 0 ]]
    22.         then
    23.             echo ":::::FILE LOCK FOUND:::::"
    24.             exit 10
    25.         else
    26.             touch /tmp/backup.lock
    27.             #before we backup, we've gotta find the nandroid subfolder (H...something)
    28.             uid=$( ls ${phone}/nandroid/ )
    29.             phonepath="${phone}/nandroid/${uid}"
    30.             # make temp backup dir
    31.             mkdir /tmp/nand
    32.             # Okay, so here is where it'll read the backup folder and compare it the contents.
    33.             ls ${backup} > /tmp/backup.list            
    34.             content=$( ls $phonepath )
    35.             for line in $content
    36.             do
    37.                 grep ${line}.tar.gz /tmp/backup.list
    38.                 grep_check=$?
    39.                 if [[ $grep_check -eq 1 ]]
    40.                 then
    41.                     cp -Rp ${phonepath}/${line} /tmp/nand 1>/dev/null
    42.                 fi
    43.             done
    44.             # Backup and TARGZ happen here. ;)
    45.             nand=$( ls /tmp/nand/)
    46.             pwd=$(pwd)
    47.             for line in $nand
    48.             do
    49.                 cd /tmp/nand/    
    50.                 tar czfv ${backup}${line}.tar.gz ${line}
    51.                 rm -r ${phone}/nandroid/${uid}/${line}
    52.             done
    53.             cd $pwd
    54.             # CLEAN UP
    55.             #unset variables and remove shit
    56.             unset {phone,backup,pwd,uid}
    57.             rm /tmp/backup.lock
    58.             rm /tmp/backup.list
    59.             rm -r /tmp/nand
    60.             exit 20
    61.         fi    
    62.  
    63. else
    64.     echo "::::::PHONE NOT FOUND::::::"
    65.     exit 0
    66. fi
    67.  

    !!!
    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:
  14. argedion

    argedion The TechnoFrog VIP Member

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

Share This Page