Gentoo Archives: gentoo-user

From: Dale <rdalek1967@×××××.com>
To: gentoo-user@l.g.o
Subject: Re: [gentoo-user] Writing a bash script or thinking about it anyway.
Date: Sat, 26 Dec 2009 00:11:02
Message-Id: 4B3548FB.4070609@gmail.com
In Reply to: Re: [gentoo-user] Writing a bash script or thinking about it anyway. by Francisco Ares
1 Francisco Ares wrote:
2 > On Tue, Dec 22, 2009 at 3:09 AM, Dale <rdalek1967@×××××.com> wrote:
3 >
4 >> Neil Walker wrote:
5 >>
6 >>> Dale wrote:
7 >>>
8 >>>
9 >>>> Me again. I'm thinking about writing a bash script that backs up my
10 >>>> /home directory.
11 >>>>
12 >>>>
13 >>> I use a simple rsync cron job to backup entire servers every hour. Does
14 >>> the job for me. ;)
15 >>>
16 >>>
17 >>> Be lucky,
18 >>>
19 >>> Neil
20 >>> http://www.the-workathome.com
21 >>>
22 >>>
23 >>>
24 >> But I wouldn't learn how to write a script that way. I got to start
25 >> somewhere. This is a good place.
26 >>
27 >> Dale
28 >>
29 >> :-) :-)
30 >>
31 >>
32 >
33 > Now I got your point.
34 >
35 > I would think on something like this (untested):
36 >
37 >
38 > The command line would be
39 >
40 > BackupScriptName FromDirectory ToDirectory
41 >
42 >
43 > #! /bin/bash
44 > FROM=$1
45 > TO=$2
46 > cd $FROM
47 > for i in * .??*
48 > do
49 > if [[ -d "$i" ]] # is it a directory?
50 > then
51 > # yes, it is a directory
52 > if [[ -e "$TO/$i" ]] && [[ -d "$TO/$i" ]]
53 > then
54 >
55 > # on the TO side, the name exists
56 > # and it is a directory
57 > cd "$i"
58 >
59 > # just to show something
60 > pwd
61 >
62 > # calls recursively this same script
63 > $0 "$FROM/$i" "$TO/$i"
64 > cd ..
65 >
66 > else
67 > # didn't existed yet on the TO side,
68 > # so copy everything
69 > cp -a $FROM/$i $TO/$i
70 > fi
71 > else
72 > # it is a file, not a directory
73 > if [[ -e "$TO/$i" ]]
74 > then
75 > # the file already exists
76 >
77 > # do something to compare the files, like:
78 >
79 > # gets size from "ls" result
80 > SIZE1=`ls -l "$i" | cut -d" " -f5`
81 > SIZE2=`ls -l "TO/$i" | cut -d" " -f5
82 >
83 > if (( $SIZE1!=$SIZE2 ))
84 > then
85 >
86 > # size is different, so copy the file
87 > cp -a "$FROM/$i" "$TO/$i"
88 >
89 > else
90 > # more tests for differences other
91 > # than size, like date/time
92 > # or even MD5SUM
93 > fi
94 > else
95 >
96 > # file doesn't exist at the "TO" side
97 > cp -a "$FROM/$i" "$TO/$i"
98 >
99 > fi
100 >
101 > fi
102 >
103 > done
104 >
105 >
106 > Hope this helps
107 > Francisco
108 >
109
110 After learning a little about scripting I'll give this a once over and
111 see what I learn. Right now, it's like. . . huh. lol
112
113 Thanks.
114
115 Dale
116
117 :-) :-)