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