LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 12-07-2013, 04:37 PM   #1
OldManRiver
LQ Newbie
 
Registered: Aug 2004
Posts: 26

Rep: Reputation: 5
Copy Utility Bash Script


All,

Was working on backup scripts and final piece needed to copy to network server with 1T HD for storage.

Anyway realized the files could be copied via:
  • CP,
  • FTP,
  • GVFS,
  • SCP,
  • SMB.
So attempting to write a generic BASH script that can copy files anywhere via any of these methods. Was looking at the following HOWTOs:

http://www.linuxquestions.org/questi...-shell-647253/

http://tamas.io/automatic-scp-using-expect-and-bash/

What I have for code is:
Code:
#! /bin/bash
# Command to run this is:
# bash /path_2_script/cp2svr_bup.sh

# Script to copy backup files to the server
clear;
#while getopts b:h:p:u: option
#	do
#		case "${option}"
#			in
#				b) BACKDIR=${OPTARG};;
#				m) MODE=$OPTARG;;
#				p) RMPASS=$OPTARG;;
#				r) RMHOST=${OPTARG};;
#				s) SRCPATH=${OPTARG};;
#				u) MUSER=$OPTARG;;
#		esac
#done
mode=$1;


# Move backups to 1T Drive on New_Off_DT box
path='/local_path';
rmpath='remote_path';
scppath='/script_path/';
ruser='remote_user';
rmpass='remote_pass';
rmip='remote_host_or_ip';
if [ ${mode} = "cp" ]; then	# Use CP (assumes local only)
	cp -r "${path}/" "${rmpath}/";

elif [ ${mode} = "ftp" ]; then	# Use FTP
	cd ${path};
	ftp -n -i ${rmip} <<EOF
	user ${ruser} ${rmpass}
	binary
	mput *.*
	quit
	EOF

elif [ ${mode} = "gvfs" ]; then	# Use GVFS
	gvfs-mount "smb://user:${ruser}:${rmpass}@${rmpath}/";
	gvfs-move "${path}*" "smb://${rmpath}/";

elif [ ${mode} = "scp" ]; then	# Use SCP
	sshpass -p ${ruser} scp -r "${path}/*" "${ruser}"@:${rmip}"${scppath}";

elif [ ${mode} = "smb" ]; then	# Use SMB/Samba
# Need to mount samba share
	cp -r "${path}/" "smb://${ruser}@${rmpath}/";
# then unmout

else
	echo "No Run-Type option selected! ";
fi
You can see I put in some comments for what I think is needed, but the problem with all the "remote" copies is how to handle the UID/PWD info needed for correct server login.

I think I have this right for the FTP, but still testing.

You can also see where I'm toying with "options" but not there yet.

All help appreciated!

Cheers!

OMR
 
Old 12-08-2013, 06:34 AM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Lets start with an important warning: Setting a password as an option to a script has security implications:
- The password will be visible in the process list while running the script,
- The password will be visible in the .bash_history file.

Unless this is an exercise or run exclusively on your local, private machines I would strongly advise against doing this.
Limit your backup modes to solutions that do not require the user to provide a password. Using cp and passwordless scp are 2 examples that are safe to use. Cifs and some ftp clients (ncftp comes to mind) can also be set up in a safe way.

That said: Have a look at the following, which concentrates on using getops and option/argument handling. It is obviously a rough unfinished example, but it will hopefully give you an idea on how to proceed with your script.
Code:
#!/bin/bash
# -------------------------------------------------------------------------- #
# getopts - example 
# -------------------------------------------------------------------------- #
# set defaults:
legalModes='(cp|ftp|gvfs|scp|smb)'
pwdModes='(ftp|gvfs|smb)'
backupMode=""
remoteHost=""
sourcePath=""
targetPath=""
localUser=$(id -un)
remoteUser=""

# -------------------------------------------------------- #
# functions
# show help
function showHelp() {
  echo "
usage: $0 options

Options:

  -m : mode              # legal modes: cp, ftp, gvfs, scp and smb
  -h : host
  -s : source directory
  -t : target directory
  -l : local user
  -u : remote user

  -H : show help.
"
  exit 0
}

# use cp
function useCp() {
  [[ ! -d "${targetPath}" ]] && { echo "Target dir doesn't exist" ; exit 1 ; }
  echo "do cp action here "
}

# use ftp
function useFtp() {
  [[ -z "${remoteUser}" ]] && { echo "No remote user provided" ; exit 1 ; }
  echo "do ftp action here"
}

# -------------------------------------------------------- #
# main

# all options, except -H, require an argument
while getopts "m:h:s:t:l:u:p:H" OPTS
do
  case ${OPTS} in
    m) backupMode=${OPTARG}
       [[ "${backupMode}" =~ $legalModes ]] || \
         { echo "No legal mode selected" ; exit 1; }
       ;;
    h) remoteHost=${OPTARG} ;;
    s) sourcePath=${OPTARG} ;;
    t) targetPath=${OPTARG} ;;
    l) localUser=${OPTARG} ;;
    u) remoteUser=${OPTARG} ;;
    p) remotePwd=${OPTARG} ;;
    H) showHelp ;;
    \?) exit 1 ;;
  esac
done

# show options used
echo ""
echo "Selcted options are:"
echo "  (m) backupMode : ${backupMode}"
echo "  (h) remoteHost : ${remoteHost}"
echo "  (s) sourcePath : ${sourcePath}"
echo "  (t) targetPath : ${targetPath}"
echo "  (l) localUser  : ${localUser}"
echo "  (u) remoteUser : ${remoteUser}"
echo "  (p) remotePwd  : ${remotePwd}"
echo ""

# needed for all actions:
# - is a mode provided
[[ -z "${backupMode}" ]] && { echo "Mode not provided." ; exit 1; }
# - are source and target provided
[[ -z "${sourcePath}" ]] && { echo "Source dir not provided." ; exit 1 ; }
[[ -z "${targetPath}" ]] && { echo "Target dir not provided." ; exit 1 ; }
# - is source present
[[ ! -d "${sourcePath}" ]] && { echo "Source dir doesn't exist" ; exit 1 ; }

# needed for remote actions
[[ -z "${remoteHost}" && "${backupMode}" != "cp" ]] && \
  { echo "No host provided"  ; exit 1; }
# needed for remote actions (except for passwordless modes)
if [[ -n "${remoteUser}" && "${backupMode}" =~ $pwdModes ]]
then
  [[ -z "${remotePwd}" ]] && { echo "No password provided"  ; exit 1; }
fi

# do action for selected mode
case ${backupMode} in
  cp)   useCp ;;
  ftp)  useFtp ;;
  gvfs) echo "useGvfs" ;;
  scp)  echo "useScp" ;;
  smb)  echo "useSmb" ;;
esac

exit 0

# -------------------------------------------------------------------------- #
# End
The above script is safe to run, it doesn't perform any actual backup actions, it just echo's information.
 
1 members found this post helpful.
Old 12-08-2013, 06:52 AM   #3
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,415
Blog Entries: 55

Rep: Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600
Quote:
Originally Posted by druuna View Post
Using cp and passwordless scp are 2 examples that are safe to use.
...and rsync over SSH with pubkey auth.
 
Old 12-08-2013, 05:43 PM   #4
lleb
Senior Member
 
Registered: Dec 2005
Location: Florida
Distribution: CentOS/Fedora/Pop!_OS
Posts: 2,983

Rep: Reputation: 551Reputation: 551Reputation: 551Reputation: 551Reputation: 551Reputation: 551
Quote:
Originally Posted by unSpawn View Post
...and rsync over SSH with pubkey auth.
thats always my first choice.
 
Old 05-31-2014, 12:05 PM   #5
OldManRiver
LQ Newbie
 
Registered: Aug 2004
Posts: 26

Original Poster
Rep: Reputation: 5
RSync

Quote:
Originally Posted by unSpawn View Post
...and rsync over SSH with pubkey auth.
unSpawn,

Have never gotten rsync to ever work right. Wish I could!

Cheers!

OMR
 
Old 05-31-2014, 01:16 PM   #6
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,415
Blog Entries: 55

Rep: Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600
OK. Can you explain what you've tried?
 
Old 05-31-2014, 02:09 PM   #7
lleb
Senior Member
 
Registered: Dec 2005
Location: Florida
Distribution: CentOS/Fedora/Pop!_OS
Posts: 2,983

Rep: Reputation: 551Reputation: 551Reputation: 551Reputation: 551Reputation: 551Reputation: 551
Quote:
Originally Posted by OldManRiver View Post
unSpawn,

Have never gotten rsync to ever work right. Wish I could!

Cheers!

OMR
works like a dream for me. its my #1 choice.
 
  


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
Bash copy script by times driftux Programming 11 09-23-2012 01:36 AM
Bash script to copy without overwriting .. Help grandeinter Programming 5 10-22-2010 07:13 PM
bash script to copy files thtr2k Programming 1 02-08-2007 12:03 AM
script or utility to copy files and folders m2azer Linux - General 1 12-22-2005 10:09 AM
Running two copy's of the same bash script AndiOliver Programming 2 10-06-2004 03:46 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 05:57 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