Register
It is currently Sat Oct 25, 2014 4:00 pm

Bash backup script: adding time/date input


All times are UTC - 6 hours


Post new topic Reply to topic  [ 2 posts ] 
Author Message
 PostPosted: Wed Dec 12, 2007 10:29 pm   

Joined: Wed Dec 12, 2007 10:13 pm
Posts: 1
Location: Portland, OR
I need help coming up with something that will add a time/date flag so I can input an option for the following script to back up all files in a specified directory that have been modified after some inputted date or time, ie a command line something like this:

./backup.sh -i '/home/foo/* -o backup -d 3h

OR

./backup.sh -i '/home/foo/* backup -t 2007-12-12 18:00

which will backup any files in the home/foo/* directory that were modified in the past 3 hrs for the first permutation or backup anything modified after 6pm 12/12/07 for the second. I need to add these options to the script I have now but am not sure how to implement it. Help!

#!/bin/bash
#backup file script
unset myInput
unset myOutput
while getopts "i:o:" myOpt
do
if [ $myOpt == "i" ]
then
myInput="$myInput $OPTARG"
elif [ $myOpt == "o" ]
then
myOutput="$OPTARG"
fi
done

if [ -z "$myInput" ]
then
echo "Missing list of input files/dirs"
exit 1
fi

if [ -z "$myOutput" ]
then
echo "Missing archive name"
exit 1
fi

# calculate next archive file in sequence
num=1
archiveNum=`printf "%03d" $num`
archiveFile="$myOutput-$archiveNum.tar"
while [ -e "$archiveFile" ]
do
num=$(( $num + 1 ))
archiveNum=`printf "%03d" $num`
archiveFile="$myOutput-$archiveNum.tar"
done

echo "[Backing up $myInput to $archiveFile]"

for file in $myInput
do
if [ ! -e "$file" ]
then
echo "Input file/dir $file does not exist"
exit 1
fi
done

tar -jScvf $archiveFile $myInput


Top
 Profile WWW  
 PostPosted: Fri Feb 08, 2008 8:49 am   

Joined: Tue Feb 05, 2008 8:24 am
Posts: 14
eikonoklastic wrote:
I need … to back up all files in a specified directory that have been modified after some inputted date or time


Consider using find command.

Code:
find yourdirectory \( -type f -o -type d \) -mtime -4


will find in yourdirectory all files or directories (-type f -o -type d) last modified in less than 4*24 hours (-mtime -4).

You can backup them with cpio like this:
Code:
find yourdirectory \( -type f -o -type d \) -mtime -4 -print0 | cpio -0ov >yourbackup.last4days.cpio


When reading man find pay attention to the following options: -mtime, -mmin (the same as -mtime but time range in minutes), -daystart.

Date arithmetic remains up to you.

--
UseFreeTools — my blog about GNU/Linux and Free Software


Top
 Profile WWW  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 2 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: No registered users and 12 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
cron


BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group
© 2003 - 2011 USA LINUX USERS GROUP