Go Back   HowtoForge Forums | HowtoForge - Linux Howtos and Tutorials > Linux Forums > Programming/Scripts

Do you like HowtoForge? Please consider supporting us by becoming a subscriber.
Reply
 
Thread Tools Display Modes
  #1  
Old 2nd February 2008, 19:51
anything anything is offline
Junior Member
 
Join Date: Feb 2008
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default Shell Script backup a file HELP

could you please find a solution for this

script that backup a file. The file name to backup should be provided as input parameter, the backup file should have the same file name with the extension ".bak". If the user provides no input parameter, the script should display an error message. If there is an input file name, but it does not exist, the script should display an error message. If the input file exists, the script should create the backup file and overwrite any existing backup file with the same name.
Reply With Quote
Sponsored Links
  #2  
Old 3rd February 2008, 14:50
topdog topdog is offline
Senior Member
 
Join Date: Jan 2008
Location: South Africa
Posts: 1,352
Thanks: 0
Thanked 153 Times in 150 Posts
Default

Code:
#!/bin/bash
#
# Andrew <andrew@topdog.za.net>
# Quick dirty script to backup a file
# 03-02-2008
#

if [[ "$1" = "" || "$1" = "--help" ]]; then
        echo " Usage: $0 <file to backup>"
        echo ""
        exit 1
fi

if [ ! -r $1 ]; then
        echo "The file $1 does not exist"
        echo ""
        exit 1
fi
#work around for systems with cp -i alias
aliased=0
j=$(alias | grep cp &>/dev/null)
if [ "$?" = "0" ]; then
        i=$(alias cp | grep '\-i' &>/dev/null)
        if [ "$?" = "0" ]; then
                aliased=1
                unalias cp
        fi
fi
# actual copy
cp -a $1 $1.bak
if [ $aliased = "1" ]; then
        alias cp='cp -i'
fi
__________________
----
http://www.topdog.za.net - Got Linux problems ? - I can help.
http://www.baruwa.org - Try it.
Reply With Quote
  #3  
Old 4th February 2008, 16:04
anything anything is offline
Junior Member
 
Join Date: Feb 2008
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default

thank you, but can you add a massage inform that the backup is successfully created, when it do. and please could you give me a little explanation of it, I am really beginner on it. I need to understand the code.

thanks
Reply With Quote
  #4  
Old 4th February 2008, 16:48
topdog topdog is offline
Senior Member
 
Join Date: Jan 2008
Location: South Africa
Posts: 1,352
Thanks: 0
Thanked 153 Times in 150 Posts
Default

Quote:
Originally Posted by anything
thank you, but can you add a massage inform that the backup is successfully created, when it do. and please could you give me a little explanation of it, I am really beginner on it. I need to understand the code.

thanks
Comments are inline
Code:
#!/bin/bash
#
# Andrew <andrew@topdog.za.net>
# Quick dirty script to backup a file
# 03-02-2008
#

#check if any option was passed or --help
if [[ "$1" = "" || "$1" = "--help" ]]; then
        echo " Usage: $0 <file to backup>"
        echo ""
        exit 1
fi

#check if file exists
if [ ! -r $1 ]; then
        echo "The file $1 does not exist"
        echo ""
        exit 1
fi
#work around for systems with cp -i alias
aliased=0
j=$(alias | grep cp &>/dev/null)
if [ "$?" = "0" ]; then
        i=$(alias cp | grep '\-i' &>/dev/null)
        if [ "$?" = "0" ]; then
                aliased=1
                unalias cp
        fi
fi
# actual copy
cp -a $1 $1.bak
if [ "$?" = "0" ]; then
        echo "The file $1 has been backed up !"
fi

#set alias back
if [ $aliased = "1" ]; then
        alias cp='cp -i'
fi
__________________
----
http://www.topdog.za.net - Got Linux problems ? - I can help.
http://www.baruwa.org - Try it.
Reply With Quote
  #5  
Old 5th February 2008, 18:09
anything anything is offline
Junior Member
 
Join Date: Feb 2008
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default

thank you very mush, it was very helpful

can you see this topic, just this
http://www.howtoforge.com/forums/showthread.php?t=20115
Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

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 On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Postfix delivery problem erebus Installation/Configuration 8 29th July 2014 20:17
Chroot SSH + ISPConfig Norman Installation/Configuration 27 26th March 2007 03:40
Systemimager (rsync) doesn't copy all comedit HOWTO-Related Questions 11 19th January 2007 17:17
jamed up my table borders?? Boon-Dog-Danny Installation/Configuration 5 23rd September 2006 16:12
Howto suggestion suse PhP ver 4 + Ver 5 wwparrish Suggest HOWTO 11 7th August 2006 13:29


All times are GMT +2. The time now is 00:57.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2014, vBulletin Solutions, Inc.