Updated October 22, 2004
Created October 21, 2004
Autogenerated Site Map
Search this Site!:
1 #!/bin/bash
2
3 # Our configuration files are next to our script...
4 cd `dirname $0`
5
6 # Script options:
7
8 # Add any global flags such as "--exclude /data" and more into
9 # the file called rsyncflags (it is empty by default).
10
11 RSYNCFLAGS=`cat snapshot-rsyncflags | grep -v ^# | grep -v "^$"`
12
13 # Add your clients to a file called snapshot-clients.
14
15 # Add any excludes for any clients to clientname-exclude. Note
16 # that this script tries to grab the separate partitions and
17 # pull each one. Also the rsync command is configured to not
18 # cross mounted filesystems (i.e. proc, ...)
19
20 # To automate this, you should 1) lock down this computer
21 # (firewall for everything except _maybe_ ssh, 2) set good
22 # passwords, 3) limit how many accounts can get in, 4) finally
23 # add this accounts public key into authorized_keys of /root/.ssh
24 # of each computer you will be snapshotting in this manner. Be
25 # warned, once you set up those passwordless keys, you must
26 # protect this local account with the utmost security.
27
28 # Cycle through all the clients we wish to get a snapshot of
29 for CLIENT in `cat snapshot-clients | grep -v ^# | grep -v "^$"`; do
30 echo starting on client $CLIENT [`date`]
31 DATE=`date "+%Y%m%d%H%M%S"`
32 HOMEDIR="/snapshot/$CLIENT"
33 mkdir -p $HOMEDIR
34 DESTINATION="$HOMEDIR/$DATE"
35 LAST=`ls $HOMEDIR | tail -1`
36 mkdir -p $DESTINATION
37 if [ -n "$LAST" ]; then
38 # pre-populate $DESTINATION to not rely entirely on
39 # remote rsync version numbers. i.e. more reliable
40 # this way.
41 cp -al $HOMEDIR/$LAST/* $DESTINATION 2>/dev/null
42 fi
43
44 # guess local partitions to grab:
45 PARTITIONS=`ssh root@10.0.127.248 "cat /proc/partitions"`
46 MOUNTS=`ssh root@10.0.127.248 "mount"`
47 LOCALPARTITIONS=`echo "$PARTITIONS" | grep [0-9]$ \
48 | awk '{print $4}' \
49 | while read REPLY; do \
50 echo "$MOUNTS" | grep $REPLY; \
51 done | awk '{print $3}'`
52
53 if [ -z "$LOCALPARTITIONS" ]; then
54 # Oops. Machine down? No access? Who knows...
55 echo could not get partition listing for $CLIENT
56 # skip it and move to next client.
57 break
58 fi
59
60 # perform snapshot
61 for SOURCE in $LOCALPARTITIONS; do
62 echo Starting on partition [$SOURCE] [`date`]
63
64 if [ "$SOURCE" = "/" ]; then
65 THISDEST=${DESTINATION}/TheRootDir
66 else
67 THISDEST=${DESTINATION}/`echo ${SOURCE} \
68 | sed -e 's~/~_~g' -e 's/^_//'`
69 fi
70
71 mkdir -p $THISDEST
72
73 # Add in the client name/ip to the source item
74 SOURCE=`echo $SOURCE | sed -e "s/^/${CLIENT}:/"`
75
76 if [ -z "$SOURCE" ]; then continue; fi
77
78 # I'll leave this commented for now, it is untested.
79 #if [ -f "${CLIENT}-exclude" -a -n "`cat ${CLIENT}-exclude`" ]; then
80 # RSYNCFLAGS="$RSYNCFLAGS --exclude-from `cat ${CLIENT}-exclude`"
81 #fi
82
83 rsync --hard-links $RSYNCFLAGS --delete -e ssh -ax ${SOURCE}/ $THISDEST
84 echo Finished with partition [$SOURCE] [$THISDEST] [`date`]
85 done
86 echo Finished with Client $CLIENT [`date`]
87 done
If you want to remove the numbers from the left side, may I suggest the following sed command (works in vi too):
cat thisfile.txt | sed -e 's/^ *[0-9][0-9]*\t\t*//'
Oh, and you can get rid of the echo `date` entries as well, I just have them in there to give me an ide of how long it takes to do a run.
1 #!/bin/bash
2
3 # Our configuration files are next to our script...
4 cd `dirname $0`
5
6 # Script options:
7
8 # Add any global flags such as "--exclude /data" and more into
9 # the file called rsyncflags (it is empty by default).
10
11 RSYNCFLAGS=`cat snapshot-rsyncflags | grep -v ^# | grep -v "^$"`
12
13 # Add your clients to a file called snapshot-clients.
14
15 # Add any excludes for any clients to clientname-exclude. Note
16 # that this script tries to grab the separate partitions and
17 # pull each one. Also the rsync command is configured to not
18 # cross mounted filesystems (i.e. proc, ...)
19
20 # To automate this, you should 1) lock down this computer
21 # (firewall for everything except _maybe_ ssh, 2) set good
22 # passwords, 3) limit how many accounts can get in, 4) finally
23 # add this accounts public key into authorized_keys of /root/.ssh
24 # of each computer you will be snapshotting in this manner. Be
25 # warned, once you set up those passwordless keys, you must
26 # protect this local account with the utmost security.
27
28 # Cycle through all the clients we wish to get a snapshot of
29 for CLIENT in `cat snapshot-clients | grep -v ^# | grep -v "^$"`; do
30 echo starting on client $CLIENT [`date`]
31 DATE=`date "+%Y%m%d%H%M%S"`
32 HOMEDIR="/snapshot/$CLIENT"
33 mkdir -p $HOMEDIR
34 DESTINATION="$HOMEDIR/$DATE"
35 LAST=`ls $HOMEDIR | tail -1`
36 mkdir -p $DESTINATION
37 if [ -n "$LAST" ]; then
38 # pre-populate $DESTINATION to not rely entirely on
39 # remote rsync version numbers. i.e. more reliable
40 # this way.
41 cp -al $HOMEDIR/$LAST/* $DESTINATION 2>/dev/null
42 fi
43
44 # guess local partitions to grab:
45 PARTITIONS=`ssh root@10.0.127.248 "cat /proc/partitions"`
46 MOUNTS=`ssh root@10.0.127.248 "mount"`
47 LOCALPARTITIONS=`echo "$PARTITIONS" | grep [0-9]$ \
48 | awk '{print $4}' \
49 | while read REPLY; do \
50 echo "$MOUNTS" | grep $REPLY; \
51 done | awk '{print $3}'`
52
53 if [ -z "$LOCALPARTITIONS" ]; then
54 # Oops. Machine down? No access? Who knows...
55 echo could not get partition listing for $CLIENT
56 # skip it and move to next client.
57 break
58 fi
59
60 # perform snapshot
61 for SOURCE in $LOCALPARTITIONS; do
62 echo Starting on partition [$SOURCE] [`date`]
63
64 if [ "$SOURCE" = "/" ]; then
65 THISDEST=${DESTINATION}/TheRootDir
66 else
67 THISDEST=${DESTINATION}/`echo ${SOURCE} \
68 | sed -e 's~/~_~g' -e 's/^_//'`
69 fi
70
71 mkdir -p $THISDEST
72
73 # Add in the client name/ip to the source item
74 SOURCE=`echo $SOURCE | sed -e "s/^/${CLIENT}:/"`
75
76 if [ -z "$SOURCE" ]; then continue; fi
77
78 # I'll leave this commented for now, it is untested.
79 #if [ -f "${CLIENT}-exclude" -a -n "`cat ${CLIENT}-exclude`" ]; then
80 # RSYNCFLAGS="$RSYNCFLAGS --exclude-from `cat ${CLIENT}-exclude`"
81 #fi
82
83 # Throw on a trailing /, unless we are doing the root dir
84 SOURCE=`echo ${SOURCE}/ | sed -e 's,//$,/,'`
85
86 # One last check to make sure we don't do anything stupid
87 # This makes sure that source is remote and thisdest is local
88 echo "$SOURCE" | grep ':' >/dev/null \
89 && echo "$THISDEST" grep -v ':' >/dev/null \
90 || continue
91
92 rsync --hard-links $RSYNCFLAGS --delete -e ssh -ax $SOURCE $THISDEST
93 echo Finished with partition [$SOURCE] [$THISDEST] [`date`]
94 done
95 echo Finished with Client $CLIENT [`date`]
96 done
Search this Site!:
Homepage: http://www.cpqlinux.com
Site Map: http://www.cpqlinux.com/sitemap.html