LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-02-2014, 12:17 PM   #1
diw10
LQ Newbie
 
Registered: Feb 2014
Posts: 13

Rep: Reputation: Disabled
backup script


I need a Bash script that
-automatically identifies local and all remote pc connected in a network and mount remote files on to the local host,
-then perform backup of all local and remote file in the local host using tar command.

Last edited by diw10; 03-02-2014 at 12:23 PM.
 
Old 03-03-2014, 05:43 AM   #2
TenTenths
Senior Member
 
Registered: Aug 2011
Location: Dublin
Distribution: Centos 5 / 6 / 7
Posts: 3,475

Rep: Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553
So what have you written so far?
 
Old 03-03-2014, 07:26 AM   #3
diw10
LQ Newbie
 
Registered: Feb 2014
Posts: 13

Original Poster
Rep: Reputation: Disabled
sorry my mistake....
I believe this is proper....
-automatically connect to computers in a network and identify files in remote computers
-then mount the files or directories on to the local host where the script is runned
-then backup files on to local host one by one using tar command (to archive).

Last edited by diw10; 03-03-2014 at 07:34 AM.
 
Old 03-03-2014, 07:28 AM   #4
TenTenths
Senior Member
 
Registered: Aug 2011
Location: Dublin
Distribution: Centos 5 / 6 / 7
Posts: 3,475

Rep: Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553
I fully understand your question, so what have you done or looked at so far? Or are you expecting us to do all the work for you?
 
Old 03-03-2014, 07:49 AM   #5
diw10
LQ Newbie
 
Registered: Feb 2014
Posts: 13

Original Poster
Rep: Reputation: Disabled
I have written a script that takes path name from the user and then perform the backup....this is a manual work and is difficult to write path name for each remote system in a network., so i request a script that automatically identify remote systems and mount files in the local system to perform backup....
 
Old 03-03-2014, 08:15 AM   #6
szboardstretcher
Senior Member
 
Registered: Aug 2006
Location: Detroit, MI
Distribution: GNU/Linux systemd
Posts: 4,278

Rep: Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694
Are these remote linux machines?

Or are you talking about windows shares?

Do you already have access to all of them?

Have you tested a scenario where the local machine just connects to ONE of those, and successfully backed it up?
 
Old 03-03-2014, 08:42 AM   #7
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
i like to use sshfs to connect to my xbmc share since i find it easier that nfs.

some people like rsync but i think good old cp command will be enough for your needs.


are you sure you want a backup of all local and remote files (starting from /) ? if that is the case then you probably would rather image with dd instead of archive with tar ?

Last edited by schneidz; 03-03-2014 at 08:45 AM.
 
Old 03-03-2014, 11:23 AM   #8
diw10
LQ Newbie
 
Registered: Feb 2014
Posts: 13

Original Poster
Rep: Reputation: Disabled
Ya all are remote linux machines....and i have tried with one and successfully backed it up.

I need a script that connects to remote system with out user giving any system IP addrs or path name.
Help me out.
 
Old 03-03-2014, 11:29 AM   #9
szboardstretcher
Senior Member
 
Registered: Aug 2006
Location: Detroit, MI
Distribution: GNU/Linux systemd
Posts: 4,278

Rep: Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694
If you want to check for sshd running on machines in the 192.168.1.0/24 subnet, and connect to any found and run 'ls' then you can do this:

Code:
for ip in 192.168.1.{1..254}; do
if nc -zv $ip 22 2>&1 | grep succeeded; then 
ssh $ip "ls /root/"
fi
done
This is assuming that you have SSH all set up on both ends.
 
2 members found this post helpful.
Old 03-03-2014, 12:02 PM   #10
diw10
LQ Newbie
 
Registered: Feb 2014
Posts: 13

Original Poster
Rep: Reputation: Disabled
ok....i will try this code
thank you
 
Old 03-03-2014, 12:05 PM   #11
diw10
LQ Newbie
 
Registered: Feb 2014
Posts: 13

Original Poster
Rep: Reputation: Disabled
Using this code can i mount the files in different IPs on to the local host...?
 
Old 03-03-2014, 12:11 PM   #12
szboardstretcher
Senior Member
 
Registered: Aug 2006
Location: Detroit, MI
Distribution: GNU/Linux systemd
Posts: 4,278

Rep: Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694
You said that you had completed your process X, whatever it is, once on a host successfully.

Here is the script with comments:

Code:
# this line means that the script will attempt to run on every ip between 192.168.1.1 and 192.168.1.254
for ip in 192.168.1.{1..254}; do

# run this test on every ip listed above. netcat tries to connect to port 22, which is ssh
if nc -zv $ip 22 2>&1 | grep succeeded; then 

# if netcat is able to connect to port 22, ssh, then run this bit
# which goes to each host listed above and runs 'ls'
ssh $ip "ls /root/"

fi
done
You can modify that to search for whatever port you would like to test, and also to run whatever you consider to be your 'backup commands,' whether that be mounting, copying or whatever. If you have successfully run a backup, as you said, then grabbing your history and using this bit of script as a wrapper shouldn't be difficult.

Last edited by szboardstretcher; 03-03-2014 at 12:12 PM.
 
1 members found this post helpful.
Old 03-03-2014, 12:22 PM   #13
diw10
LQ Newbie
 
Registered: Feb 2014
Posts: 13

Original Poster
Rep: Reputation: Disabled
Thank you....i will try it out
 
Old 03-03-2014, 12:33 PM   #14
diw10
LQ Newbie
 
Registered: Feb 2014
Posts: 13

Original Poster
Rep: Reputation: Disabled
I have used K shell for scripting....is the code K shell script....can i use this code in my already written ksh script
 
Old 03-04-2014, 07:59 AM   #15
diw10
LQ Newbie
 
Registered: Feb 2014
Posts: 13

Original Poster
Rep: Reputation: Disabled
Guys please help me out....i need to remove echo statements for taking input from user and replace it by code you guys gave and fix the destination path...

#!/bin/ksh
#
#SCRIPT: backupk script


#####################################
# PATH Declaration
MOUNT=/bin/mount
UMOUNT=/bin/umount
#####################################


#####################################
#Function Declaration

#Function for taking Local Backup
GetPathLocal ( ) { echo "Enter the Destination Directory[ ]:"
echo "No Default,Enter absolute path"
read DEST_PATH
CreateArch
}

#Function for taking Remote Backup
GetPathRemote ( ) { echo "\nEnter the Remote Machine IP Address[No Default]:\c "
read DEST_IP

echo "\nEnter the Remote Directory to be mounted[no default ]:\c"

read REMOTE_DIR

ISMOUNTED=`$MOUNT | grep "/mntback"`
if [ "$ISMOUNTED" = "" ]
then
if [ -d /mntback ]
then
echo `$MOUNT $DEST_IP:$REMOTE_DIR /mntback`
else
mkdir /mntback
echo `$MOUNT $DEST_IP:$REMOTE_DIR /mntback`
fi
fi

echo "\nEnter the Destination Directory Name[No default]:\c"
read DEST_NAME
DEST_PATH=/mntback/$DEST_NAME

CreateArch
$UMOUNT /mntback

}

#Function Performing Backup
CreateArch ( ) { if [ -d $SOUR_PATH ]
then
if [ ! -d $DEST_PATH ]
then
echo "Destination Directory does not exist.Creating..."
mkdir $DEST_PATH
fi
ls -1F $SOUR_PATH | grep / |awk -F/ '{print $1}' - > /tmp/user

cd $SOUR_PATH

for i in `cat /tmp/user`
do
#tar cvfp - $i | gzip > $DEST_PATH/$i.tar.gz
tar cvfpBz $DEST_PATH/$i.tar.gz $i # taking tar & zipping
done
GenerateReadme

else
echo "\nError: Source not a directory
EXITING BACKUP"
exit 1
fi



rm /tmp/user

}
#Function to Generate Readme file after backup is completed
GenerateReadme ( ) { README=$DEST_PATH/readme
echo "
***************************************************
BACKUP README
***************************************************

1. FileSystem Name(Source): $SOUR_PATH
2. Date of Creation: `date`
3. Machine used for backup: `hostname`
4. Destination(IP Address): $DEST_IP
5. Destination(Path): $REMOTE_DIR/$DEST_NAME " >$README

}

######################################
####### BEGINNING OF MAIN ###########
######################################

echo "\
**********************************************************
* *
* BACKUP UTILITY *
* *
**********************************************************
\n
Enter \"e\" to EXIT or \"c\" to CONTINUE [no default]: \c"
read ANSW

case $ANSW in
c|C)
echo "\nEnter the source Directory(Full Path)[ ]: \c"

read SOUR_PATH

echo "\nDestination Kind:\n
Local [1]
Remote [2]

Enter your choice [no default]: \c"

read DEST_KIND

case $DEST_KIND in
1)
GetPathLocal
;;
2)
GetPathRemote
;;
*)
echo "\n
No other option
***** BACKUP Exit *****"
exit 0
;;
esac
;;
e|E)
exit 0
;;
*)
echo "\n
No other option
***** BACKUP Exit *****"
exit 0
;
 
  


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
backup script help tsitras Linux - General 2 02-20-2013 09:38 AM
What happens with backup-manager.org (very useful backup script)? Murz Linux - Software 3 07-27-2010 06:46 AM
Newbie trying to write a simple backup script to backup a single folder Nd for school stryker759a Linux - Newbie 2 09-16-2009 08:52 AM
Need help with script to organise files into folders as part of DVD backup script jasybee2000 Linux - Newbie 5 06-15-2009 07:29 PM
how to create backup MYSQL Script to backup my database for every 1hour RMLinux Linux - Newbie 3 11-20-2008 10:13 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:31 PM.

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