Look for, backup and delete symlinks


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Look for, backup and delete symlinks
# 1  
Old 08-26-2012
Look for, backup and delete symlinks

Hi,

My first post here:
Was looking if someone can help enhancing my code.
I am limited to sh or ash shell (android / busybox)

I made a script to look for busybox symlinks, backup them and delete them

I have these questions about the below code:

- busybox tar do not has the options to append to an archive, any workaround for my script to avoid backing up separate files?

- I want to avoid grep to search for busybox string in the readlink output results. if [[ str1 == *busybox ]] option is not recognized in my case

- any other advises about that script are welcome

Thanks a lot

Code:
#!/system/xbin/busybox sh
# or android shell /bin/sh

bkdir="/sdcard/bb-uninstall" ; 
if [ -d "$bkdir" ] ; 
	then 
		busybox echo "Backup folder $bkdir already exists. Please remove it to proceed...">>$bkdir/__Error_Backup_Folder_Already_Exists__.txt ; 
		busybox echo "Backup folder \"$bkdir\" already exists. Please remove it to proceed..." ; 
		exit 1 ; 
	else 
		busybox mkdir $bkdir ; 
		if [ ! -d "$bkdir" ] ; 	 
			then 
				busybox echo "Backup folder \"$bkdir\" could not be created" ; 
				busybox echo "Check if sdcard is present, mounted, has free space or you have permissions!!" ; 
				exit 1 ; 
		fi ; 
fi ; 

bbdir="/system/xbin /system/bin" ; 
ndeletes=0 ; 
for bbpath in $bbdir ; 
	do 
		for f in $(busybox find $bbpath -type l) ; 
			do 
				gnr=$(busybox readlink $f) ; 
				if echo "$gnr" |busybox grep -q busybox ; 
					then 
						busybox echo -e "$f \t is linked to \t $gnr ---> backup up in sdcard then deleted">>$bkdir/bb-uninstall.log ; 
						bkfile=`busybox echo "$f" | busybox tr '/' '.'` ; 
						bkfile=bak$bkfile ; 
						busybox tar zcf $bkdir/$bkfile.tar.gz -C / ${f:1} ; 
						if [ ! -f "$bkdir/$bkfile.tar.gz" ] ; 
							then 
								busybox echo "Error while creating backup file \"$bkdir/$bkfile.tar.gz\"" ; 
								busybox echo "File \"$f\" was not deleted" ; 
								busybox echo "Check free space on \"$bkdir\" or if you have needed permissions" ; 
								busybox echo "Uninstall will stop. Launch it again after fixing write issue to \"$bkdir\"" ; 
								exit 1 ; 
						fi ; 
						busybox echo "$f linking to $gnr backed-up. Now deleting" ; 
						busybox rm $f ; 
						ndeletes=`busybox expr $ndeletes + 1` ; 
					else busybox echo "$f is not linked to busybox, but to $gnr ---> file not deleted">>$bkdir/bb-uninstall.log ; 
				fi ; 
		done 
done 


echo >>$bkdir/bb-uninstall.log ; 
echo "=========================================">>$bkdir/bb-uninstall.log ; 
echo "  Busybox Uninstaller v1.0 Completed !!  ">>$bkdir/bb-uninstall.log ; 
echo "=========================================">>$bkdir/bb-uninstall.log ; 
echo "               - Details -               ">>$bkdir/bb-uninstall.log ; 
echo "Number of deleted files: $ndeletes">>$bkdir/bb-uninstall.log ; 
echo "Busybox cleaned folders: \"$bbdir\"">>$bkdir/bb-uninstall.log ; 
echo "Backup folder: \"$bkdir\"">>$bkdir/bb-uninstall.log ; 
echo "Log file: \"$bkdir/bb-uninstall.log\"">>$bkdir/bb-uninstall.log ; 
echo >>$bkdir/bb-uninstall.log ; 

echo ; 
echo "=========================================" ; 
echo "  Busybox Uninstaller v1.0 Completed !!  " ; 
echo "=========================================" ; 
echo "               - Details -               " ; 
echo "Number of deleted files: $ndeletes" ; 
echo "Busybox cleaned folders: \"$bbdir\"" ; 
echo "Backup folder: \"$bkdir\"" ; 
echo "Log file: \"$bkdir/bb-uninstall.log\"" ; 
echo ;

# 2  
Old 08-26-2012
You could use a case statement to match the "busybox" string:

Code:
case "$gnr" in
    *busybox*)
                ....
    ;;
    *)
               ....
    ;;
esac

---------- Post updated at 09:48 AM ---------- Previous update was at 09:35 AM ----------

Quote:
Originally Posted by Phil3759
- busybox tar do not has the options to append to an archive, any workaround for my script to avoid backing up separate files?
Think you are going to have to resort to rebuilding the tar with your new/replaced files:
  • Extract existing tar archive into a temp area
  • copy additional/replacement files into same structure
  • retar the whole thing
  • delete the temp area.

---------- Post updated at 09:54 AM ---------- Previous update was at 09:48 AM ----------

Don't think ${f:1} is supported under ash you man need to use ${f#?} instead.
# 3  
Old 08-27-2012
Quote:
Originally Posted by Chubler_XL
You could use a case statement to match the "busybox" string:

Code:
case "$gnr" in
    *busybox*)
                ....
    ;;
    *)
               ....
    ;;
esac

Thank you a lot for the support

Using case in: again, no advantage over grep, as both are external commands to the shell. Using Grep permits me to continue with the if/else structure
Any other way to replace this part of code:
Code:
if [[ $gnr == *busybox ]]

By the way, I tried also this one:
Code:
if [ $gnr = "$bbpath/busybox" ];

But it ends detecting all symlinks as related to busybox, even those that are not related to it. Any reason you see for that?

Should I try
Code:
if [[ $gnr = "$bbpath/busybox" ]];

?

Quote:
Think you are going to have to resort to rebuilding the tar with your new/replaced files:
  • Extract existing tar archive into a temp area
  • copy additional/replacement files into same structure
  • retar the whole thing
  • delete the temp area.

---------- Post updated at 09:54 AM ---------- Previous update was at 09:48 AM ----------

Don't think ${f:1} is supported under ash you man need to use ${f#?} instead.
Did not think at it that way, good idea :-)

Edit:
${f:1} worked fine under /bin/sh (android) and also with the busybox ash I use actually. But thank you for the other alternative

Last edited by Phil3759; 08-27-2012 at 06:59 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Red Hat

RHEL 7: Backup Space and Delete is not working in console

Hi All, During my virtual machine power on i have rc3.d script to accept user inputs like IP address. This script gets executed during first time boot up. It was working fine till my VM is using RHEL6.5. Now we migrated to RHEL 7 environment. While accepting the user inputs in console, I... (4 Replies)
Discussion started by: kalpeer
4 Replies

2. Shell Programming and Scripting

Find, backup and delete.

Is there any error while executing this script.. #!/bin/bash FINDPATH=/home/ftpcdr/cdr/192.168.3.91 BACKPATH=/home/ftpcdr/backup STATUS=$? cd $FINDPATH find -type d -mtime +30 -print > $BACKPATH/list.txt # FIND FILES THAT CREATED BEFORE 30 DAYS. FIRST=$(ls $BACKPATH/list.txt | grep... (5 Replies)
Discussion started by: leo_ultra_leo
5 Replies

3. Shell Programming and Scripting

Find + Symlinks = me confused

So i have read the man pages a few time. Searched google but I am not quite sure i understand all the lingo. What i want to do is list all files on / except i dont want any symlinks (because if I am searching / I will find the "true" file...correct?) So there is the -P, -H, and '-type l'... (2 Replies)
Discussion started by: nitrobass24
2 Replies

4. Shell Programming and Scripting

Script - Mysql backup and delete

Hello I have a production mysql server and archive server, unfortunitly its not possible to setup repliacation between the two, the reason is that the archive server is using some fancy storage engine which doesn't allow mysql replication. I'm trying to write a script using perl that does... (1 Reply)
Discussion started by: amlife
1 Replies

5. Shell Programming and Scripting

rsync backup mode(--backup) Are there any options to remove backup folders on successful deployment?

Hi Everyone, we are running rsync with --backup mode, Are there any rsync options to remove backup folders on successful deployment? Thanks in adv. (0 Replies)
Discussion started by: MVEERA
0 Replies

6. Shell Programming and Scripting

scripts to Initialise,backup,restore,delete and empty

you are to write scripts for a customer that would require a system enable the backing up of files and directories. The script should (these names should be used): 1. INITIALISE: Initialise the directory for the backup(called backup and should be in the home directory) and any other... (1 Reply)
Discussion started by: babby01
1 Replies

7. Slackware

Context dependent symlinks

Ive got multiple PCs, sharing an NFS mounted home dir. For certain apps I would like to keep the config files host specific. Easy solution is to create symlinks to local folders for configs. Ideally I would still want the .config files to reside in the user home folder. Is it possible to... (2 Replies)
Discussion started by: agentrnge
2 Replies

8. UNIX for Dummies Questions & Answers

Symlinks

Given a filename, is it possible to know haow many symbolic links are pointing to it? How can I tell? (1 Reply)
Discussion started by: ct1977
1 Replies

9. Shell Programming and Scripting

Nested Symlinks?

Please don't laugh or call me a fool... I'm trying to set up a script that will go through my Music File directory and generate a set of symbolic links in a directory called "What's New". Within that directory there will be a "30 Days", "3 Months", "6 Months" and "A Year" directories. Within... (0 Replies)
Discussion started by: deckard
0 Replies

10. UNIX for Advanced & Expert Users

search and replace symlinks

Hello All, Assuming i have a thousand symlinks under directory /mydir (and its sub-dir) such as: mysymlink1 -> ../../../myfoo/mysymlink1 mysymlink2 -> ../../../myfoo/mysymlink2 How can I search the string "myfoo" and replaced with "yourfoo" such that after the operation is complete the... (2 Replies)
Discussion started by: nixrock
2 Replies
Login or Register to Ask a Question

Featured Tech Videos