Puppy Linux Discussion Forum Forum Index Puppy Linux Discussion Forum
Puppy HOME page : puppylinux.com
"THE" alternative forum : puppylinux.info
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

The time now is Tue 21 Oct 2014, 05:09
All times are UTC - 4
 Forum index » House Training » HOWTO ( Solutions )
Scripts to take backup of modified files only.
Moderators: Flash, Ian, JohnMurga
Post new topic   Reply to topic View previous topic :: View next topic
Page 1 of 1 [7 Posts]  
Author Message
snayak

Joined: 14 Sep 2011
Posts: 231

PostPosted: Sun 23 Dec 2012, 03:07    Post subject:  Scripts to take backup of modified files only.
Subject description: backup.sh restore.sh
 

Dear All,

After trying to take backup of modified files only(http://www.murga-linux.com/puppy/viewtopic.php?t=82975), I came up with two small scripts. Here I am sharing the scripts, please use it if you find it good. Please suggest improvements.

This script automatically remembers upto what date you have taken the backup.

Now it will start taking your backup since 01 Jan 2013. Then it remembers the date on which you have taken the backup. Then onwards simply run this script, it will take backup of files modified since last backup!


backup.sh
Quote:

#!/bin/sh

last_N_days=30
exception1="dont_backup.txt"

src_dir=""
dst_dir=""
backup_log=""

usage()
{
echo Usage: backup.sh src-dir dst-path
echo Example: backup.sh /mnt/sda5 /mnt/sdb1
echo Modify Pattern for \"files to be excluded from back-up\" in backup.sh file.
}

echo hello

if [ $# != 2 ]
then
usage
else

backup_log=`pwd`
backup_log=$backup_log/backup.log
echo > $backup_log
echo check $backup_log for files excluded from backup

this_script=`realpath "$0"`
src_dir=$1
dst_dir=$2

echo you have given $1 as source directory
echo you have given $2 as destination directory

if [ ! -d $src_dir ]
then
echo source directory $src_dir does not exist
exit
fi

if [ ! -d $dst_dir ]
then
echo destination directory $dst_dir does not exist
exit
fi

cd $src_dir
src_dir=`pwd`
cd - > /dev/null

cd $dst_dir
dst_dir=`pwd`
cd - > /dev/null

echo complete path for source directory is $src_dir
echo complete path for destination directory is $dst_dir

src_dir_to_copy=`echo $src_dir | awk -F/ '{print $NF}'`
echo backup will copy complete $src_dir_to_copy/ directory to $dst_dir

time_stamp=`cat $this_script | tail -1 | awk '{print $2}'`

if [ "$time_stamp" = "" ]
then
echo Run it now!
echo \#Upto `date +%Y%m%d%H%M` >> $this_script
exit
else
echo taking backup from $time_stamp
fi

touch -t $time_stamp $this_script

cd `dirname "$src_dir"`
#find $src_dir_to_copy -type f -ctime -$last_N_days | while read file_name;
find $src_dir_to_copy -type f -cnewer $this_script | while read file_name;
do
file_excluded=`echo $file_name | grep $exception1`
if [ $file_excluded ]
then
echo $file_name >> $backup_log
else
new_path_for_file=$dst_dir/$file_name
new_dir_for_file=`dirname "$new_path_for_file"`
echo directory $new_dir_for_file will be created if does not exist
mkdir -p "$new_dir_for_file"
echo $file_name file will be copied in to it
cp "$file_name" "$new_dir_for_file"
fi
done
cd - > /dev/null

echo ========Files excluded from backup========
cat ./backup.log
echo ========Files excluded from backup========

(echo "g/^#Upto 201/d"; echo 'wq') | ex -s $this_script
echo \#Upto `date +%Y%m%d%H%M` >> $this_script

fi

echo done


#=====keep_this_line=====
#Upto 201301010000


restore.sh
Quote:

#!/bin/sh

src_dir=""
dst_dir=""

usage()
{
echo Usage: restore.sh src-dir dst-path
echo Example: restore.sh /mnt/sdb1/sda5 /mnt/sda6
}

echo hello

if [ $# != 2 ]
then
usage
else

src_dir=$1
dst_dir=$2

echo you have given $1 as source directory
echo you have given $2 as destination directory

if [ ! -d $src_dir ]
then
echo source directory $src_dir does not exist
exit
fi

if [ ! -d $dst_dir ]
then
echo destination directory $dst_dir does not exist
exit
fi

cd $src_dir
src_dir=`pwd`
cd - > /dev/null

cd $dst_dir
dst_dir=`pwd`
cd - > /dev/null

echo complete path for source directory is $src_dir
echo complete path for destination directory is $dst_dir

src_dir_to_copy=`echo $src_dir | awk -F/ '{print $NF}'`
echo restore will copy complete $src_dir_to_copy/ directory to $dst_dir

echo directory "$dst_dir/$src_dir_to_copy" will be created if does not exist
mkdir -p "$dst_dir/$src_dir_to_copy"
cp -v -r $src_dir/* $dst_dir/$src_dir_to_copy

fi

echo done




Sincerely,
Srinivas Nayak

_________________
Machine: Wary 530, AMD Athlon 2000+, ASUS A7N266-VM board, 512MB DDR RAM, 40GB HDD, 52xCDRW, PS/2 Mouse/Keyboard, USB 2.0.
Home: http://www.mathmeth.com/sn/
Blog: http://srinivas-nayak.blogspot.com/

Last edited by snayak on Wed 06 Feb 2013, 04:21; edited 4 times in total
Back to top
View user's profile Send private message 
Bruce B


Joined: 18 May 2005
Posts: 11130
Location: The Peoples Republic of California

PostPosted: Sun 23 Dec 2012, 18:04    Post subject:  

Do you have all the necessary escape provisions? This is particularly important when distributing the scripts. We should presume the user doesn't understand the innards of the script.

src_dir=$1

Example: if $1 is innacurate or not entered what happens?

I do like this.

[ ! $1 ] && echo missing argument && exit
src_dir=$1
[ ! -d $src_dir ] && echo directory $src_dir doesn\'t exist && exit

_________________
New! Puppy Linux Links Page
Back to top
View user's profile Send private message 
snayak

Joined: 14 Sep 2011
Posts: 231

PostPosted: Sun 30 Dec 2012, 04:42    Post subject:  

Dear Bruce,

Many thanks for your suggestion. I got your point.
I thought,

cd $src_dir

shall fail in case src_dir is something wrong and the user shall be able to see the error message. I have everywhere provided echo to display whatever is going inside script.

Seeing your comments, to gain a bit more knowledge, I checked these two pages for understanding "[ expression ]".

1. http://en.wikibooks.org/wiki/Bash_Shell_Scripting#Combining_conditions

2. http://www.gnu.org/software/bash/manual/bashref.html#Bash-Conditional-Expressions

What is the difference between "[ expression ]" and "[[ expression ]]" ?

In link2, the first line reads "Conditional expressions are used by the [[ compound command and the test and [ builtin commands." What does it mean?

Sincerely,
Srinivas Nayak

_________________
Machine: Wary 530, AMD Athlon 2000+, ASUS A7N266-VM board, 512MB DDR RAM, 40GB HDD, 52xCDRW, PS/2 Mouse/Keyboard, USB 2.0.
Home: http://www.mathmeth.com/sn/
Blog: http://srinivas-nayak.blogspot.com/
Back to top
View user's profile Send private message 
snayak

Joined: 14 Sep 2011
Posts: 231

PostPosted: Thu 10 Jan 2013, 08:34    Post subject:  

Scripts updated to automatically remember the last backup date and time. So next time when you run this script, it will take backup of files modified since last backup!
_________________
Machine: Wary 530, AMD Athlon 2000+, ASUS A7N266-VM board, 512MB DDR RAM, 40GB HDD, 52xCDRW, PS/2 Mouse/Keyboard, USB 2.0.
Home: http://www.mathmeth.com/sn/
Blog: http://srinivas-nayak.blogspot.com/
Back to top
View user's profile Send private message 
snayak

Joined: 14 Sep 2011
Posts: 231

PostPosted: Sun 13 Jan 2013, 16:06    Post subject:  

Script updated to use absolute path of its own.

Learnings:
1. 'realpath' command shows the absolute path of a file.
2. When a script is in /bin, and the script is executed, $0 holds its full path, like '/bin/a.sh'

_________________
Machine: Wary 530, AMD Athlon 2000+, ASUS A7N266-VM board, 512MB DDR RAM, 40GB HDD, 52xCDRW, PS/2 Mouse/Keyboard, USB 2.0.
Home: http://www.mathmeth.com/sn/
Blog: http://srinivas-nayak.blogspot.com/
Back to top
View user's profile Send private message 
snayak

Joined: 14 Sep 2011
Posts: 231

PostPosted: Wed 06 Feb 2013, 04:25    Post subject:  

backup script modified to backup files downloaded since last backup.

Learning:

"find /mnt/sda5 -cnewer /tmp/b.txt" searches all files whose "inode information changed" since the "last modification time" of /tmp/b.txt.

_________________
Machine: Wary 530, AMD Athlon 2000+, ASUS A7N266-VM board, 512MB DDR RAM, 40GB HDD, 52xCDRW, PS/2 Mouse/Keyboard, USB 2.0.
Home: http://www.mathmeth.com/sn/
Blog: http://srinivas-nayak.blogspot.com/
Back to top
View user's profile Send private message 
heenry

Joined: 06 Mar 2013
Posts: 1

PostPosted: Wed 06 Mar 2013, 09:47    Post subject:  

I'm wondering about the escape provisions also as this is a fundamental part of scriipt distribution.
Back to top
View user's profile Send private message 
Display posts from previous:   Sort by:   
Page 1 of 1 [7 Posts]  
Post new topic   Reply to topic View previous topic :: View next topic
 Forum index » House Training » HOWTO ( Solutions )
Jump to:  

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 vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Powered by phpBB © 2001, 2005 phpBB Group
[ Time: 0.0698s ][ Queries: 12 (0.0101s) ][ GZIP on ]