LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
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 07-27-2012, 06:46 AM   #1
kobus_etime_co_za
LQ Newbie
 
Registered: Jul 2012
Posts: 6

Rep: Reputation: 0
Need help with mounting devices as well as copying data to it


Hi all

I'm not really familiar with Linux syntax but can make out some of the stuff. I need help with a backup procedure that I want to get in place for a large company. Please advise if I'm using the correct method to backup data or not. If there is any other command line backup scripts that I can use, please let me know.

SCRIPT BELOW:
  1. I unmount the drives
  2. then format those devices
  3. then re-mount those drives
  4. and then copy data to the empty drives

Only one drive will be available at a time.

umount -l /dev/sdc1 /exusb
umount -l /dev/sdd1 /exusb

mkfs.ext3 /dev/sdc1
mkfs.ext3 /dev/sdd1

mount -t ext3 /dev/sdc1 /exusb
mount -t ext3 /dev/sdd1 /exusb

#check if the first drive is mounted or not
if grep -qs '/dev/sdc1' /exusb; then
cd /exusb
mkdir SGDatabase
time cp -dupvR /backup/SGDatabase /exusb/SGDatabase > SGDatabase_copy.txt
else
#check if the second drive is mounted or not
if grep -qs '/dev/sdd1' /exusb; then
cd /exusb
mkdir SGDatabase
time cp -dupvR /backup/SGDatabase /exusb/SGDatabase > SGDatabase_copy.txt
else
echo "Drives were not mounted"
fi
fi

When I run this script I get the following error, "syntax error near unexpected token `fi'." What could the possible reasons be for this?

When I add the following in the first line of the script file, "#!/bin/bash" I get a different error, "-bash: ./password.sh: /bin/bash^M: bad interpreter: No such file or directory." I'm not sure what this is. I have check the bin/bash directory and the file is there.

Any help will be much appreciated. Thank you

PS. I'm a Java and Php web application developer. This is my first shot at getting a backup solution for the client that runs CentOS with virtual environments and stuff like that.

Last edited by kobus_etime_co_za; 07-27-2012 at 06:53 AM.
 
Old 07-27-2012, 06:47 AM   #2
kobus_etime_co_za
LQ Newbie
 
Registered: Jul 2012
Posts: 6

Original Poster
Rep: Reputation: 0
Only one drive will be available at a time.
 
Old 07-27-2012, 06:56 AM   #3
eSelix
Senior Member
 
Registered: Oct 2009
Location: Wroclaw, Poland
Distribution: Arch, Kubuntu
Posts: 1,281

Rep: Reputation: 320Reputation: 320Reputation: 320Reputation: 320
Quote:
^M
You have bad "end of line" character. It should be \n code 0x0a in Linux, and you have \r\n code 0x0d,0x0a (it is from Windows). Change proper option in your text editor and apply new EOL character in your code. Then once again enter "#!/bin/bash" on the beggining. Also please use a CODE tag to enclose your code.

Last edited by eSelix; 07-27-2012 at 07:06 AM.
 
Old 07-27-2012, 07:15 AM   #4
schneidz
LQ Guru
 
Registered: May 2005
Location: boston, usa
Distribution: fedora-35
Posts: 5,313

Rep: Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918
are you sure you need 2 fi's at the end ?

also why are you mounting /dev/sdc and /dev/sdd to the same mount-point ?

also, this test just seems weird: if grep -qs '/dev/sdc1' /exusb
what condition are you trying to test ?

Last edited by schneidz; 07-27-2012 at 07:17 AM.
 
Old 07-27-2012, 07:33 AM   #5
kobus_etime_co_za
LQ Newbie
 
Registered: Jul 2012
Posts: 6

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by schneidz View Post
are you sure you need 2 fi's at the end ?

also why are you mounting /dev/sdc and /dev/sdd to the same mount-point ?

also, this test just seems weird: if grep -qs '/dev/sdc1' /exusb
what condition are you trying to test ?
The client have two hard drives that they want to use for their backups and those hard drives gets swapped daily. I have noticed that if I swap those drives that each use their own letters eg. sdc and sdd. It doesn't remain the same.

That if statement is something I've found to test whether a drive is mounted or not. I do not know the Linux syntax that well to know what to use for it, so Iv'e used the script Iv'e found.

Thank you for those quick responses

---------- Post added 07-27-12 at 07:34 AM ----------

Quote:
Originally Posted by eSelix View Post
You have bad "end of line" character. It should be \n code 0x0a in Linux, and you have \r\n code 0x0d,0x0a (it is from Windows). Change proper option in your text editor and apply new EOL character in your code. Then once again enter "#!/bin/bash" on the beggining. Also please use a CODE tag to enclose your code.
I have re-created that script file through the nano editor in Linux and it solved the syntax problem. Thank you very much.
 
Old 07-27-2012, 07:47 AM   #6
kobus_etime_co_za
LQ Newbie
 
Registered: Jul 2012
Posts: 6

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by eSelix View Post
You have bad "end of line" character. It should be \n code 0x0a in Linux, and you have \r\n code 0x0d,0x0a (it is from Windows). Change proper option in your text editor and apply new EOL character in your code. Then once again enter "#!/bin/bash" on the beggining. Also please use a CODE tag to enclose your code.
Thank you for the heads-up on the CODE tags. Here is the script file that I want to run:

Code:
#!/bin/bash

umount -l /dev/sdc1 /exusb
umount -l /dev/sdd1 /exusb
sleep 10

mkfs.ext3 /dev/sdc1
mkfs.ext3 /dev/sdd1
sleep 10

mount -t ext3 /dev/sdc1 /exusb
mount -t ext3 /dev/sdd1 /exusb
sleep 10

function start_the_process(){
	startd=`date`
	echo "The copy of backup was started at, $startd" | mail -s "Copy Started" kobus@etime.co.za
	
	cd /exusb
	
	mkdir SGDatabase
	time cp -dupvR /backup/SGDatabase /exusb/SGDatabase > SGDatabase_copy.txt
	sleep 10
	
	mkdir 1
	cd 1
	
	mkdir exchange
	time cp -dupvR /backup/1/exchange /exusb/1/exchange > exchange_copy.txt
	sleep 10

	mkdir fileserver
	time cp -dupvR /backup/1/fileserver /exusb/1/fileserver > fileserver_copy.txt
	sleep 10

	mkdir sql
	time cp -dupvR /backup/1/sql /exusb/1/sql > sql_copy.txt
	sleep 10

	mkdir gateway
	time cp -dupvR /backup/1/gateway /exusb/1/gateway > gateway_copy.txt
	sleep 10

	rm -rf /backup/SGTrash/*

	endd=`date`
	echo "The copy  was Completed at, $endd" | mail -s "Copy Completed" kobus@etime.co.za
}

if grep -qs '/dev/sdc1' /exusb; then
	start_the_process
else
    if grep -qs '/dev/sdd1' /exusb; then
		start_the_process
	else
		failuredate=`date`
		echo "The drives was not found. Date: $failuredate" | mail -s "Drives not found" kobus@etime.co.za
	
		sleep 10
		
		exit 1
	fi
fi
 
Old 07-27-2012, 09:48 AM   #7
kobus_etime_co_za
LQ Newbie
 
Registered: Jul 2012
Posts: 6

Original Poster
Rep: Reputation: 0
Talking

Hi there,

Iv'e got a working solution for now. Thanks for the help thus far. If you do know of a better backup solution, please post it below.

The following script will run on a daily basis. At 4pm the hard drives will be swapped and at 5pm the cronjob will run.
Code:
#!/bin/bash

umount -l /dev/sdc1 /exusb
umount -l /dev/sdd1 /exusb
sleep 5

mkfs.ext3 /dev/sdc1
mkfs.ext3 /dev/sdd1
sleep 5

mount -t ext3 /dev/sdc1 /exusb
mount -t ext3 /dev/sdd1 /exusb
sleep 5

function start_copying() {
        start_date=`date`
        echo "Backup started at, $start_date" | mail -s "Backup Started" kobus@etime.co.za

        cd /exusb
        
	echo ""
	echo "Copying: SGDatabase"
        mkdir SGDatabase
        time cp -dupvR /backup/SGDatabase /exusb/SGDatabase > SGDatabase_copy.t$
        sleep 5

        mkdir 1
        cd 1

        echo ""
	echo "Copying: Exchange"
        mkdir exchange
        time cp -dupvR /backup/1/exchange /exusb/1/exchange > exchange_copy.txt
        sleep 5

        echo ""
	echo "Copying: File Server"
        mkdir fileserver
        time cp -dupvR /backup/1/fileserver /exusb/1/fileserver > fileserver_co$
        sleep 5

        echo ""
	echo "Copying: SQL"
        mkdir sql
        time cp -dupvR /backup/1/sql /exusb/1/sql > sql_copy.txt
        sleep 5

        echo ""
	echo "Copying: Gateway"
        mkdir gateway
        time cp -dupvR /backup/1/gateway /exusb/1/gateway > gateway_copy.txt
        sleep 5

	echo ""
        echo "Removing Trash"
        rm -rf /backup/SGTrash/*

        end_date=`date`
        echo "Backup finished at, $end_date" | mail -s "Backup Finished" kobus@etime.co.za
}

if [ $(mount | grep -c /exusb) == 1 ]
then
	start_copying
else
	echo "Drive was not mounted correctly!" | mail -s "Drive was not mounted correctly" kobus@etime.co.za
fi
 
Old 07-29-2012, 05:36 PM   #8
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,359

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
Code:
mount -t ext3 /dev/sdc1 /exusb
mount -t ext3 /dev/sdd1 /exusb
1. the sleep cmds are pointless

2. you are mounting 2 disks to that same mount pt (/exusb) at the same time. This makes no sense & in fact that last one will be the one used.
 
Old 07-30-2012, 02:16 AM   #9
kobus_etime_co_za
LQ Newbie
 
Registered: Jul 2012
Posts: 6

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by chrism01 View Post
Code:
mount -t ext3 /dev/sdc1 /exusb
mount -t ext3 /dev/sdd1 /exusb
1. the sleep cmds are pointless

2. you are mounting 2 disks to that same mount pt (/exusb) at the same time. This makes no sense & in fact that last one will be the one used.
Hi there,

I only use the sleep command to make sure that it waits for the previous commands to completely execute and clear.

We have 2 hard drives. When I plug in hard drive 1, it starts as /dev/sdc1. When I plug in hard drive 2, it starts as /dev/sdd1. I do not know Linux well enough to determine which is the latest drive 'name' that was plugged in. In this case I mount both of them. If one fails the other one will be used. That's why I mount them both to the same point. It might not be the correct way of doing things but it's working well for now.
 
Old 07-30-2012, 03:08 AM   #10
eSelix
Senior Member
 
Registered: Oct 2009
Location: Wroclaw, Poland
Distribution: Arch, Kubuntu
Posts: 1,281

Rep: Reputation: 320Reputation: 320Reputation: 320Reputation: 320
For mounting (especially when you are not sure which name disk will be assigned) better is using UUIDs, look at "blkid" command. You can also use udev to always create known to you symlink to this device. And you should check and react on error codes returned by mount.
 
Old 07-30-2012, 07:51 AM   #11
schneidz
LQ Guru
 
Registered: May 2005
Location: boston, usa
Distribution: fedora-35
Posts: 5,313

Rep: Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918
also this proves that your if conditions arent testing for anything:
Code:
[schneidz@hyper Downloads]$ df -h
Filesystem            Size  Used Avail Use% Mounted on
rootfs                 82G   72G  9.4G  89% /
udev                  427M     0  427M   0% /dev
tmpfs                 436M  304K  435M   1% /dev/shm
tmpfs                 436M  656K  435M   1% /run
/dev/mapper/vg_hyper-lv_root
                       82G   72G  9.4G  89% /
tmpfs                 436M     0  436M   0% /sys/fs/cgroup
tmpfs                 436M  656K  435M   1% /var/lock
tmpfs                 436M  656K  435M   1% /var/run
tmpfs                 436M     0  436M   0% /media
/dev/sda3             194M   84M  110M  44% /boot
/dev/sda2              47G   20G   27G  43% /media/win
/dev/sdb1             111G  103G  1.7G  99% /media/win/stuff/ide-34
[schneidz@hyper Downloads]$ grep -qs '/dev/sdb1' /media/win/stuff/ide-34
[schneidz@hyper Downloads]$ echo $?
1
[schneidz@hyper Downloads]$ grep -qs '/dev/sda2' /media/win/stuff/ide-34
[schneidz@hyper Downloads]$ echo $?
1
[schneidz@hyper Downloads]$ grep -qs '/dev/sda2' /exusb
[schneidz@hyper Downloads]$ echo $?
2
i think it would be better to check /etc/mtab or the output of the mount command for the existence of mounted partitions.


its dangerous not to precisely mount the partitions in an exact location especially since your reformatting/ backing up... your gonna' end up mounting or formatting the wrong drive (or worse, an internal drive) and your gonna' end up wiping over your data.

Last edited by schneidz; 07-30-2012 at 08:03 AM.
 
Old 07-30-2012, 11:06 PM   #12
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,359

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
Quote:
I only use the sleep command to make sure that it waits for the previous commands to completely execute and clear.
Still pointless; those cmds are run consecutively, not concurrently.

I agree with schneidz's comments in post#11 as well.

You may find these links handy:
http://rute.2038bug.com/index.html.gz
http://tldp.org/LDP/Bash-Beginners-G...tml/index.html
http://www.tldp.org/LDP/abs/html/
 
  


Reply



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
Data loss while copying . . . yashsaxena Linux - Newbie 1 01-27-2011 12:32 AM
[SOLVED] Data copying sharan.M Linux - Software 5 07-20-2010 02:36 PM
I/O error while copying some data on to external HDD gary4gar Linux - Hardware 2 10-31-2007 03:50 AM
copying grows data jonaskoelker Linux - Software 1 08-06-2006 05:01 PM
Issue copying data files from CD fryak Fedora 3 05-28-2004 11:49 AM

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

All times are GMT -5. The time now is 04:12 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
Open Source Consulting | Domain Registration