LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 03-30-2012, 02:19 PM   #1
rajhans
LQ Newbie
 
Registered: Mar 2012
Location: India
Distribution: i'm a beginner
Posts: 10

Rep: Reputation: Disabled
Smile i want to write a shell script create backup of my system into a external hard disk.


create backup of the system in which for every extension there will be a folder.and each folder will have only one kind of extension files.

Last edited by rajhans; 03-30-2012 at 07:00 PM. Reason: last question was too long.
 
Old 03-30-2012, 02:32 PM   #2
fatmac
Senior Member
 
Registered: Sep 2011
Location: Upper Hale, Surrey/Hants Border, UK
Distribution: AntiX
Posts: 1,994

Rep: Reputation: Disabled
I won't write it for you; but perhaps something like :-

#!/bin/sh
### this script makes your BACKUP structure - run once only!
## code to check your disk is mounted, then :-
mkdir /dev/usbdisk/BACKUP
mkdir /dev/usbdisk/BACKUP/images
mkdir /dev/usbdisk/BACKUP/music
## continue as you wish
## save as create-backup.sh
## then test it out

#!/bin/sh
### this script copies to BACKUP
## code to check your disk is mounted
cp -a *.jpg /dev/usbdisk/BACKUP/images/
cp -a *.mp3 /dev/usbdisk/BACKUP/music/
## continue as you wish
## save as backup-files.sh
## then test it out


You need to :-
chmod +x backup-files.sh
chmod +x create-backup.sh

before they will run.

Last edited by fatmac; 03-30-2012 at 02:41 PM.
 
1 members found this post helpful.
Old 03-30-2012, 03:06 PM   #3
rajhans
LQ Newbie
 
Registered: Mar 2012
Location: India
Distribution: i'm a beginner
Posts: 10

Original Poster
Rep: Reputation: Disabled
do i have to make different directories manually by writing
mkdir /dev/usbdisk/BACKUP/images
cp -a *.jpg /dev/usbdisk/BACKUP/images/

as i have many extensions it will hard to write the code for every extension...
is there any code like
for i in .*
mkdir /dev/usbdisk/BACKUP/$i
cp -a *.jpg /dev/usbdisk/BACKUP/$i/

bt i m facing problem with the filename
so can u help

---------- Post added 03-31-12 at 12:37 AM ----------

do i have to make different directories manually by writing
mkdir /dev/usbdisk/BACKUP/images
cp -a *.jpg /dev/usbdisk/BACKUP/images/

as i have many extensions it will hard to write the code for every extension...
is there any code like
for i in .*
mkdir /dev/usbdisk/BACKUP/$i
cp -a *.jpg /dev/usbdisk/BACKUP/$i/

bt i m facing problem with the filename
so can u help
 
Old 03-30-2012, 03:24 PM   #4
fatmac
Senior Member
 
Registered: Sep 2011
Location: Upper Hale, Surrey/Hants Border, UK
Distribution: AntiX
Posts: 1,994

Rep: Reputation: Disabled
You seem to have some programming knowledge, I took you to be a complete newbie.
Yes there is a 'for x in mp3; do <code>; done'.
But if I remember right, you have to be in the right directory for it to work.
As I said, I won't write it for you; Linux is about learning.
What you need is a good bash scripting tutorial, there are plenty around.
Try Googling for some.
 
1 members found this post helpful.
Old 03-30-2012, 03:27 PM   #5
rajhans
LQ Newbie
 
Registered: Mar 2012
Location: India
Distribution: i'm a beginner
Posts: 10

Original Poster
Rep: Reputation: Disabled
thank you ......
i'll try to find it out...
 
Old 03-30-2012, 05:03 PM   #6
rajhans
LQ Newbie
 
Registered: Mar 2012
Location: India
Distribution: i'm a beginner
Posts: 10

Original Poster
Rep: Reputation: Disabled
Question

hey
can u help me wid this
when i m trying to visit every directory in the system
by this
for i in *
then i gets value of everything like linux.mp3 for a particular file
now i want to select only mp3 for this current value
how is this possible

help me with this
 
Old 03-30-2012, 05:18 PM   #7
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,561

Rep: Reputation: 2127Reputation: 2127Reputation: 2127Reputation: 2127Reputation: 2127Reputation: 2127Reputation: 2127Reputation: 2127Reputation: 2127Reputation: 2127Reputation: 2127
Why don't you just do something like this?

Code:
#!/bin/bash

extensions="mp3 jpg avi pdf txt doc mpeg"

for ext in $extensions; do
   dest=/dev/usbdisk/BACKUP/$ext/
   if [ ! -d $dest ]; do
      mkdir -p $dest
      stat=$?
      if [ $stat -ne 0 ]; do
         echo "Unable to create directory $dest"
         exit 1
      fi
   fi

   find /home/ -iname "*.$ext" -exec cp -f {} $dest \;
done
*Note that this is completely untested*

This will only backup certain file extensions. Is this what you want, or are you wanting to back up EVERYTHING, and separate it out by extension? I don't see the point in backing up everything without retaining the original directory structure, it'll just end up being a garbled mess, and what will you do with files that don't have an extension?

Last edited by suicidaleggroll; 03-30-2012 at 05:21 PM.
 
1 members found this post helpful.
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] how to take backup from hard disk to external hard disk? kri Linux - Newbie 4 09-28-2011 06:37 AM
HOW-TO allow normal users to write on NTFS external hard disk? xirtyllo Slackware 3 12-04-2010 11:46 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 11:39 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration