LinuxQuestions.org
Review your favorite Linux distribution.
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 08-29-2012, 07:13 AM   #1
ali0123
LQ Newbie
 
Registered: Aug 2011
Posts: 4

Rep: Reputation: Disabled
Question backup Shell script help


Hi guys complete noob here I am trying to write my first shell script that backs up the content of a directory by creating a new directory with the Date and time as the directory name. the requirement is that the date and time must be entered by the user. I can do as much, but It also needs to check if the user input is valid or not as well as when copying stuff into the new directory it needs to check the files that have the creation date and time before the user input date so far I have been able to come up with this

#
#
#Back Up Script
#
#
# To create a direcotry with the name backup and currnet date and time
echo "Please Enter the Date and Time of the backup in DD_MM_YYYYformat"
read backupDate
if[$backupDate == date +%d_%m_%Y ]
mkdir $backupDate
else
echo "Please make sure the date format is corrct and include "_" while putting date and time"
fi

#To Copy all the files from the current directory to the newly created back directory
#with the curent time and date stamp
#cp * Backup`date +%d_%m_%Y_%T`
cp * $backupDate
#gives the details of all the coppied files
##echo -n "Backup copy(ies) made - file count = "
##ls Backup`date +%d_%m_%Y_%T` | wc -l | sed "s/[ \n]*//g"


lot of the code is commented out as things just were not working and i am trying to test them one by one.
any help would be hugely appreciated
 
Old 08-29-2012, 07:23 AM   #2
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
the find command has a ctime argument for creation time.
 
Old 08-29-2012, 07:25 AM   #3
ali0123
LQ Newbie
 
Registered: Aug 2011
Posts: 4

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by schneidz View Post
the find command has a ctime argument for creation time.
thanks for the reply could you please elaborate a bit further I mean how do i implement it here?
 
Old 08-29-2012, 07:42 AM   #4
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,415
Blog Entries: 55

Rep: Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600
...also it doesn't make sense to have a user enter the date as backup scripts tend to run as much unattended as possible (more than anything it's a discipline thing IMHO). And using DDMMYYYY looks nice but when you have to sort things "%Y%m%d_${HOSTNAME}" or "${HOSTNAME}_%Y%m%d_${LOGNAME}" or "%Y%m%d_${HOSTNAME}_${DIRECTORIES}" makes much more sense.

If you want to make it easier for a user you could use a simple menu like this:
Code:
select SELECTDATE in today custom ; do 
 case $SELECTDATE in
  today) USEDATE=$(/bin/date +'%Y%m%d');;
  *) echo -en "Set the date: "; read USEDATE;;
 esac; break; 
done; 
# Now "$USEDATE" is the string you can use to populate the tarball name with.
Such simple menus can also be used to query for use of other things like if the tarball name should contain the hostname, name of the current user, which directory or directories to back up, etc, etc.

Code:
function howto() { echo "Bash scripting guides:
http://www.tldp.org/HOWTO/Bash-Prog-Intro-HOWTO.html 
http://www.tldp.org/LDP/Bash-Beginners-Guide/html/index.html 
http://www.gnu.org/software/bash/manual/html_node/index.html
http://www.grymoire.com/Unix/Sh.html
http://www.tldp.org/LDP/abs/html/ 
http://mywiki.wooledge.org/BashFAQ
http://mywiki.wooledge.org/BashPitfalls"; }
 
1 members found this post helpful.
Old 08-29-2012, 07:51 AM   #5
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
heres how i would do part of it:
Code:
touch ali -d 2011-11-11
cp `find . -newer ali` /path/to/backup-dir

Last edited by schneidz; 08-29-2012 at 07:53 AM.
 
1 members found this post helpful.
Old 08-29-2012, 08:44 AM   #6
ali0123
LQ Newbie
 
Registered: Aug 2011
Posts: 4

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by unSpawn View Post
...also it doesn't make sense to have a user enter the date as backup scripts tend to run as much unattended as possible (more than anything it's a discipline thing IMHO). And using DDMMYYYY looks nice but when you have to sort things "%Y%m%d_${HOSTNAME}" or "${HOSTNAME}_%Y%m%d_${LOGNAME}" or "%Y%m%d_${HOSTNAME}_${DIRECTORIES}" makes much more sense.

If you want to make it easier for a user you could use a simple menu like this:
Code:
select SELECTDATE in today custom ; do 
 case $SELECTDATE in
  today) USEDATE=$(/bin/date +'%Y%m%d');;
  *) echo -en "Set the date: "; read USEDATE;;
 esac; break; 
done; 
# Now "$USEDATE" is the string you can use to populate the tarball name with.
Such simple menus can also be used to query for use of other things like if the tarball name should contain the hostname, name of the current user, which directory or directories to back up, etc, etc.

Code:
function howto() { echo "Bash scripting guides:
http://www.tldp.org/HOWTO/Bash-Prog-Intro-HOWTO.html 
http://www.tldp.org/LDP/Bash-Beginners-Guide/html/index.html 
http://www.gnu.org/software/bash/manual/html_node/index.html
http://www.grymoire.com/Unix/Sh.html
http://www.tldp.org/LDP/abs/html/ 
http://mywiki.wooledge.org/BashFAQ
http://mywiki.wooledge.org/BashPitfalls"; }

thanks for the response. getting the user input for the time and date is the Lecturer's requirement previously I had written it so that when backup script was called from the menu script it automatically created the folder with the current time and date copied every thing in and then just informed the number of copied files and listed them.
I am just trying to make the user input the date and time that can be matched with the current date and time?
 
Old 08-29-2012, 08:58 AM   #7
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,415
Blog Entries: 55

Rep: Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600
Date yes, time no unless you only use "%H%M".

Code:
# So you can see what it does:
set -vx
echo "Please Enter the Date and Time of the backup in YYYYMMDD format: "; read BACKUPDATE
if [ "${BACKUPDATE}" = "$(/bin/date +"%Y%m%d" ]; then
 # commence
else
 # Epic fail on the users part. Clean up process, chide user, return to "Start".
fi
set +vx
 
  


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
Shell Script to Backup DB prudens Programming 6 07-28-2009 11:41 PM
Backup Shell Script soad789 Programming 6 12-08-2007 06:35 AM
shell backup script xtremeclones Linux - Software 3 10-03-2006 02:13 PM
Shell Script for backup BBQ_Matt Linux - Software 7 06-30-2005 05:19 PM
writting a backup shell script yenonn Slackware 2 03-18-2004 07:49 PM

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

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