~kim0/serverguide/serverguide-review-ch19

« back to all changes in this revision

Viewing changes to build/serverguide/C/backups-shellscripts-rotation.html

  • Committer: Matthew East
  • Date: 2011-05-03 07:11:18 UTC
  • Revision ID: mdke@ubuntu.com-20110503071118-081aatibsr9k2yqy
Add files from ubuntu-docs natty branch, trim to use only those necessary for serverguide

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
 
2
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 
3
<html xmlns="http://www.w3.org/1999/xhtml">
 
4
  <head xmlns="http://www.w3.org/1999/xhtml">
 
5
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
 
6
    <title xmlns="">Archive Rotation</title>
 
7
    <link rel="stylesheet" href="../../libs/ubuntu-book.css" type="text/css" />
 
8
    <link rel="home" href="index.html" title="Ubuntu Server Guide" />
 
9
    <link rel="up" href="backups.html" title="Chapter 18. Backups" />
 
10
    <link rel="prev" href="backup-shellscripts.html" title="Shell Scripts" />
 
11
    <link rel="next" href="bacula.html" title="Bacula" />
 
12
    <link rel="copyright" href="legal.html" title="Credits and License" />
 
13
  </head>
 
14
  <body>
 
15
    <div id="round">
 
16
      <img id="topcap" alt="" src="https://help.ubuntu.com/htdocs/ubuntunew/img/cap-top.png" />
 
17
      <div id="layout" class="container clear-block">
 
18
        <script xmlns="" src="https://ssl.google-analytics.com/urchin.js" type="text/javascript"></script>
 
19
        <script xmlns="" type="text/javascript">
 
20
_uacct = "UA-1018242-8";
 
21
urchinTracker();
 
22
</script>
 
23
        <div id="header">
 
24
          <div id="logo-floater">
 
25
            <h1>
 
26
              <a href="https://help.ubuntu.com" title="Ubuntu Documentation">
 
27
                <img alt="Ubuntu" id="logo" src="https://help.ubuntu.com/htdocs/ubuntunew/img/logo.png" />
 
28
              </a>
 
29
            </h1>
 
30
          </div>
 
31
          <noscript>
 
32
            <form action="http://www.google.com/cse" id="cse-search-box">
 
33
              <div>
 
34
                <input type="hidden" name="cx" value="003883529982892832976:e2vwumte3fq" />
 
35
                <input type="hidden" name="ie" value="UTF-8" />
 
36
                <input type="text" name="q" size="27" />
 
37
                <input type="submit" name="sa" value="Search" />
 
38
              </div>
 
39
            </form>
 
40
          </noscript>
 
41
          <script>
 
42
 document.write('<form action="https://help.ubuntu.com/search.html" id="cse-search-box">');
 
43
 document.write('  <div>');
 
44
 document.write('    <input type="hidden" name="cof" value="FORID:9" />');
 
45
 document.write('    <input type="hidden" name="cx" value="003883529982892832976:e2vwumte3fq" />');
 
46
 document.write('    <input type="hidden" name="ie" value="UTF-8" />');
 
47
 document.write('    <input type="text" name="q" size="27" />');
 
48
 document.write('    <input type="submit" name="sa" value="Search" />');
 
49
 document.write('  </div>');
 
50
 document.write('</form>');
 
51
</script>
 
52
          <div id="sitename">
 
53
            <a href="https://help.ubuntu.com/">
 
54
              <img alt="Official Documentation" src="https://help.ubuntu.com/htdocs/ubuntunew/img/help-about.png" />
 
55
              <span>Official Documentation</span>
 
56
            </a>
 
57
          </div>
 
58
        </div>
 
59
        <div id="page">
 
60
          <div id="content">
 
61
            <div class="breadcrumbs"><a href="https://help.ubuntu.com/">Ubuntu Documentation</a> &gt; <a href="https://help.ubuntu.com/11.04">Ubuntu 11.04</a> &gt; <span class="breadcrumb-link"><a href="index.html">Ubuntu Server Guide</a></span> &gt; <span class="breadcrumb-link"><a href="backups.html">Backups</a></span> &gt; <span class="breadcrumb-node">Archive Rotation</span></div>
 
62
            <div xmlns="http://www.w3.org/1999/xhtml" class="sect1" title="Archive Rotation">
 
63
              <div class="titlepage">
 
64
                <div>
 
65
                  <div>
 
66
                    <h2 class="title" style="clear: both"><a id="backups-shellscripts-rotation"></a>Archive Rotation</h2>
 
67
                  </div>
 
68
                </div>
 
69
              </div>
 
70
              <p>
 
71
    The shell script in section <a class="xref" href="backup-shellscripts.html" title="Shell Scripts">the section called “Shell Scripts”</a> only allows for seven different archives.  For
 
72
    a server whose data doesn't change often this may be enough.  If the server has a large amount of data a more 
 
73
    robust rotation scheme should be used.
 
74
    </p>
 
75
              <div class="sect2" title="Rotating NFS Archives">
 
76
                <div class="titlepage">
 
77
                  <div>
 
78
                    <div>
 
79
                      <h3 class="title"><a id="backups-nfs-rotation"></a>Rotating NFS Archives</h3>
 
80
                    </div>
 
81
                  </div>
 
82
                </div>
 
83
                <p>
 
84
      In this section the shell script will be slightly modified to implement a grandfather-father-son rotation scheme 
 
85
      (monthly-weekly-daily): 
 
86
      </p>
 
87
                <div class="itemizedlist">
 
88
                  <ul class="itemizedlist" type="disc">
 
89
                    <li class="listitem">
 
90
                      <p>
 
91
          The rotation will do a <span class="emphasis"><em>daily</em></span> backup Sunday through Friday. 
 
92
          </p>
 
93
                    </li>
 
94
                    <li class="listitem">
 
95
                      <p>
 
96
          On Saturday a <span class="emphasis"><em>weekly</em></span> backup is done giving you four weekly backups a month.  
 
97
          </p>
 
98
                    </li>
 
99
                    <li class="listitem">
 
100
                      <p>
 
101
          The <span class="emphasis"><em>monthly</em></span> backup is done on the first of the month rotating two monthly backups based 
 
102
          on if the month is odd or even.  
 
103
          </p>
 
104
                    </li>
 
105
                  </ul>
 
106
                </div>
 
107
                <p>
 
108
      Here is the new script:
 
109
      </p>
 
110
                <pre class="programlisting">
 
111
#!/bin/bash
 
112
####################################
 
113
#
 
114
# Backup to NFS mount script with
 
115
# grandfather-father-son rotation.
 
116
#
 
117
####################################
 
118
 
 
119
# What to backup. 
 
120
backup_files="/home /var/spool/mail /etc /root /boot /opt"
 
121
 
 
122
# Where to backup to.
 
123
dest="/mnt/backup"
 
124
 
 
125
# Setup variables for the archive filename.
 
126
day=$(date +%A)
 
127
hostname=$(hostname -s)
 
128
 
 
129
# Find which week of the month 1-4 it is.
 
130
day_num=$(date +%d)
 
131
if (( $day_num &lt;= 7 )); then
 
132
        week_file="$hostname-week1.tgz"
 
133
elif (( $day_num &gt; 7 &amp;&amp; $day_num &lt;= 14 )); then
 
134
        week_file="$hostname-week2.tgz"
 
135
elif (( $day_num &gt; 14 &amp;&amp; $day_num &lt;= 21 )); then
 
136
        week_file="$hostname-week3.tgz"
 
137
elif (( $day_num &gt; 21 &amp;&amp; $day_num &lt; 32 )); then
 
138
        week_file="$hostname-week4.tgz"
 
139
fi
 
140
 
 
141
# Find if the Month is odd or even.
 
142
month_num=$(date +%m)
 
143
month=$(expr $month_num % 2)
 
144
if [ $month -eq 0 ]; then
 
145
        month_file="$hostname-month2.tgz"
 
146
else
 
147
        month_file="$hostname-month1.tgz"
 
148
fi
 
149
 
 
150
# Create archive filename.
 
151
if [ $day_num == 1 ]; then
 
152
        archive_file=$month_file
 
153
elif [ $day != "Saturday" ]; then
 
154
        archive_file="$hostname-$day.tgz"
 
155
else 
 
156
        archive_file=$week_file
 
157
fi
 
158
 
 
159
# Print start status message.
 
160
echo "Backing up $backup_files to $dest/$archive_file"
 
161
date
 
162
echo
 
163
 
 
164
# Backup the files using tar.
 
165
tar czf $dest/$archive_file $backup_files
 
166
 
 
167
# Print end status message.
 
168
echo
 
169
echo "Backup finished"
 
170
date
 
171
 
 
172
# Long listing of files in $dest to check file sizes.
 
173
ls -lh $dest/
 
174
</pre>
 
175
                <p>
 
176
      The script can be executed using the same methods as in <a class="xref" href="backup-shellscripts.html#backup-executing-shellscript" title="Executing the Script">the section called “Executing the Script”</a>.
 
177
      </p>
 
178
                <p> 
 
179
      It is good practice to take backup media off site in case of a disaster.  In the shell script example the backup 
 
180
      media is another server providing an NFS share.  In all likelihood taking the NFS server to another location would not
 
181
      be practical.  
 
182
      Depending upon connection speeds it may be an option to copy the archive file over a WAN link to a server in another location.
 
183
      </p>
 
184
                <p> 
 
185
      Another option is to copy the archive file to an external hard drive which can then be taken off site.  
 
186
      Since the price of external hard drives continue to decrease it may be cost-effective to use two drives for each archive level.  
 
187
      This would allow you to have one external drive attached to the backup server and one in another location.
 
188
      </p>
 
189
              </div>
 
190
              <div class="sect2" title="Tape Drives">
 
191
                <div class="titlepage">
 
192
                  <div>
 
193
                    <div>
 
194
                      <h3 class="title"><a id="backup-shellscript-tapedrive"></a>Tape Drives</h3>
 
195
                    </div>
 
196
                  </div>
 
197
                </div>
 
198
                <p> 
 
199
      A tape drive attached to the server can be used instead of a NFS share.  Using a tape drive simplifies archive rotation, and
 
200
      taking the media off site as well.  
 
201
      </p>
 
202
                <p> 
 
203
      When using a tape drive the filename portions of the script aren't needed because the date is sent directly to the tape device.
 
204
      Some commands to manipulate the tape are needed.  This is accomplished using <span class="application"><strong>mt</strong></span>, a magnetic tape
 
205
      control utility part of the <span class="application"><strong>cpio</strong></span> package.
 
206
      </p>
 
207
                <p> 
 
208
      Here is the shell script modified to use a tape drive:
 
209
      </p>
 
210
                <pre class="programlisting">
 
211
#!/bin/bash
 
212
####################################
 
213
#
 
214
# Backup to tape drive script.
 
215
#
 
216
####################################
 
217
 
 
218
# What to backup. 
 
219
backup_files="/home /var/spool/mail /etc /root /boot /opt"
 
220
 
 
221
# Where to backup to.
 
222
dest="/dev/st0"
 
223
 
 
224
# Print start status message.
 
225
echo "Backing up $backup_files to $dest"
 
226
date
 
227
echo
 
228
 
 
229
# Make sure the tape is rewound.
 
230
mt -f $dest rewind
 
231
 
 
232
# Backup the files using tar.
 
233
tar czf $dest $backup_files
 
234
 
 
235
# Rewind and eject the tape.
 
236
mt -f $dest rewoffl
 
237
 
 
238
# Print end status message.
 
239
echo
 
240
echo "Backup finished"
 
241
date
 
242
</pre>
 
243
                <div class="note" title="Note" style="margin-left: 0.5in; margin-right: 0.5in;">
 
244
                  <table border="0" summary="Note">
 
245
                    <tr>
 
246
                      <td rowspan="2" align="center" valign="top" width="25">
 
247
                        <img alt="[Note]" src="../../libs/admon/note.png" />
 
248
                      </td>
 
249
                      <th align="left"></th>
 
250
                    </tr>
 
251
                    <tr>
 
252
                      <td align="left" valign="top">
 
253
                        <p>
 
254
        The default device name for a SCSI tape drive is <code class="filename">/dev/st0</code>.  Use the appropriate device path for your 
 
255
        system.
 
256
        </p>
 
257
                      </td>
 
258
                    </tr>
 
259
                  </table>
 
260
                </div>
 
261
                <p>
 
262
      Restoring from a tape drive is basically the same as restoring from a file.  Simply rewind the tape and use the device path 
 
263
      instead of a file path.  For example to restore the <code class="filename">/etc/hosts</code> file to <code class="filename">/tmp/etc/hosts</code>:
 
264
      </p>
 
265
                <pre class="screen">
 
266
<span class="command"><strong>mt -f /dev/st0 rewind</strong></span>
 
267
<span class="command"><strong>tar -xzf /dev/st0 -C /tmp etc/hosts</strong></span>
 
268
</pre>
 
269
              </div>
 
270
            </div>
 
271
          </div>
 
272
          <div xmlns="http://www.w3.org/1999/xhtml" class="navfooter">
 
273
            <hr />
 
274
            <table width="100%" summary="Navigation footer">
 
275
              <tr>
 
276
                <td width="40%" align="left"><a accesskey="p" href="backup-shellscripts.html"><img src="../../libs/navig/prev.png" alt="Prev" /></a> </td>
 
277
                <td width="20%" align="center">
 
278
                  <a accesskey="u" href="backups.html">
 
279
                    <img src="../../libs/navig/up.png" alt="Up" />
 
280
                  </a>
 
281
                </td>
 
282
                <td width="40%" align="right"> <a accesskey="n" href="bacula.html"><img src="../../libs/navig/next.png" alt="Next" /></a></td>
 
283
              </tr>
 
284
              <tr>
 
285
                <td width="40%" align="left" valign="top">Shell Scripts </td>
 
286
                <td width="20%" align="center">
 
287
                  <a accesskey="h" href="index.html">
 
288
                    <img src="../../libs/navig/home.png" alt="Home" />
 
289
                  </a>
 
290
                </td>
 
291
                <td width="40%" align="right" valign="top"> Bacula</td>
 
292
              </tr>
 
293
            </table>
 
294
          </div>
 
295
          <hr />
 
296
          <div id="footer">
 
297
            <div id="ubuntulinks">
 
298
              <p>The material in this document is available under a free license, see <a href="/legal.html">Legal</a> for details<br />
 
299
        For information on contributing see the <a href="https://wiki.ubuntu.com/DocumentationTeam">Ubuntu Documentation Team wiki page</a>. To report a problem, visit the <a href="https://bugs.launchpad.net/ubuntu/+source/ubuntu-docs">bug page for Ubuntu Documentation</a></p>
 
300
            </div>
 
301
          </div>
 
302
          <div id="bottomcap">
 
303
            <img src="https://help.ubuntu.com/htdocs/ubuntunew/img/cap-bottom.png" alt="" />
 
304
          </div>
 
305
        </div>
 
306
      </div>
 
307
    </div>
 
308
  </body>
 
309
</html>