ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
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).
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....
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 ?
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.
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...
#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
;
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.