backups

17
BACKUPS Whenever a crash occurs we run a backup program. The backup device can be magnetic or a cartridge tape, a floppy diskette or even a disk file. Small systems especially workstations, may not have the tape facility, so the floppy drive will be used here to illustrate the features of both commands. UNIX offers two commands – cpio and tar. Both combine a group of files into a single file ( called an archive), with suitable headers preceding the contents of each file.

Upload: aliza

Post on 08-Jan-2016

23 views

Category:

Documents


0 download

DESCRIPTION

BACKUPS. Whenever a crash occurs we run a backup program. The backup device can be magnetic or a cartridge tape, a floppy diskette or even a disk file. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: BACKUPS

BACKUPS

• Whenever a crash occurs we run a backup program.

• The backup device can be magnetic or a cartridge tape, a floppy diskette or even a disk file.

• Small systems especially workstations, may not have the tape facility, so the floppy drive will be used here to illustrate the features of both commands.

• UNIX offers two commands – cpio and tar.

• Both combine a group of files into a single file ( called an archive), with suitable headers preceding the contents of each file.

• Each command has definite advantages over the other, and the formats they use to record data are not compatible with each other.

Page 2: BACKUPS

Copy input-output – the cpio CommandCopy input-output – the cpio Command

cpio (copy input output) copies files to and cpio (copy input output) copies files to and from a backup device. from a backup device.

It uses standard input to take the list of It uses standard input to take the list of filenames. filenames.

It then copies them with their contents and It then copies them with their contents and headers to a stream which can be headers to a stream which can be redirected to a file or device. This means redirected to a file or device. This means that cpio can be used with redirection and that cpio can be used with redirection and piping.piping.

cpio uses two key options, -o (output) and i cpio uses two key options, -o (output) and i (input) two commands should not be placed (input) two commands should not be placed at a time.at a time.

Page 3: BACKUPS

Packing up files(-o)Packing up files(-o)

Since cpio uses only standard input, you can use Since cpio uses only standard input, you can use ls to generate a list of filenames to serve as its ls to generate a list of filenames to serve as its input. The –o key option creates the archive on input. The –o key option creates the archive on the standard output, which you need to redirect the standard output, which you need to redirect to a device file.to a device file.

$ ls | cpio –ov > /dev/rdsk/foq18dt$ ls | cpio –ov > /dev/rdsk/foq18dt Array.plArray.pl CalendarCalendar Convert.shConvert.sh TheThe –v –v verbose option displays each file name on verbose option displays each file name on

the terminal when its being copied.the terminal when its being copied. cpio needs as input a list of files, and if this list is cpio needs as input a list of files, and if this list is

available in a file, redirection can be used too.available in a file, redirection can be used too. $cpio –o >/dev/rdsk/foq18dt < flist.$cpio –o >/dev/rdsk/foq18dt < flist.

Page 4: BACKUPS

Incremental Backups:Incremental Backups:

find can also produce a file list, so any files that satisfy its find can also produce a file list, so any files that satisfy its selection criteria can also be backed up.selection criteria can also be backed up.

You will frequently need to use find and cpio in You will frequently need to use find and cpio in combination to backup selected files.combination to backup selected files.

Find the files those that have been modified in the last two Find the files those that have been modified in the last two days.days.

Shell commandShell command find . –mtime -2 –print find . –mtime -2 –print | | Cpio –ovB > Cpio –ovB >

/dev/rdsk/foq18dt/dev/rdsk/foq18dt

Since the path list of find is dot, the files are backed up Since the path list of find is dot, the files are backed up with their relative path names.with their relative path names.

However if it is /, find will use absolute pathnames However if it is /, find will use absolute pathnames -B sets block size to 5120. for higher sizes –c option is -B sets block size to 5120. for higher sizes –c option is

used.used. [[Find . –mtime Find . –mtime -2 –print -2 –print | c| cpio –ovpio –ovc 51200 > c 51200 >

/dev/rdsk/foq18dt]/dev/rdsk/foq18dt]

Page 5: BACKUPS

Multivolume backupsMultivolume backups

When the created archive in the backup device is larger than When the created archive in the backup device is larger than the capacity of the device, cpio prompts for inserting a new the capacity of the device, cpio prompts for inserting a new diskette into the drive.diskette into the drive.

Find . –mtime -2 Find . –mtime -2 –print –print | c| cpio –ocB > pio –ocB > /dev/rdsk/foq18dt/dev/rdsk/foq18dt

Output:Output: Reached the end of the medium on output.Reached the end of the medium on output. If you want to go on, type device/filename If you want to go on, type device/filename

when ready when ready /dev/fd0/dev/fd0 3672 blocks3672 blocks Enter the device when cpio pauses to take input. In Enter the device when cpio pauses to take input. In

this way, an archive can be split into several this way, an archive can be split into several extents(volumes).extents(volumes).

Page 6: BACKUPS

Restoring FilesRestoring Files

A complete archive or selected files can be restored with the A complete archive or selected files can be restored with the –i key option–i key option

To restore files, use redirection to take input from the device.To restore files, use redirection to take input from the device. $cpio –iv $cpio –iv << /dev/rdsk/foq18dt /dev/rdsk/foq18dt Array.plArray.pl CalendarCalendar a.sqla.sql

When restoring subdirectories, cpio assumes that the When restoring subdirectories, cpio assumes that the subdirectory structures are also maintained on the hard disk; subdirectory structures are also maintained on the hard disk; it cant create them in case they are not.it cant create them in case they are not.

However the –d (directory) option overrides that.However the –d (directory) option overrides that.

cpio also accepts a quoted wild card pattern, so multiple files cpio also accepts a quoted wild card pattern, so multiple files fitting the pattern can be restored. Restoring only the shell fitting the pattern can be restored. Restoring only the shell scripts becomes quite easy.scripts becomes quite easy.

$Cpio –iv “*.sh” $Cpio –iv “*.sh” << /dev/rdsk/foq18dt /dev/rdsk/foq18dt

Page 7: BACKUPS

Handling modification timesHandling modification times

Use the –m option to tell cpio that the Use the –m option to tell cpio that the modification time has to be retained.modification time has to be retained.

cpio compares the modification time of a cpio compares the modification time of a file on the media with the one on disk (if file on the media with the one on disk (if any). If the disk file is newer than the copy, any). If the disk file is newer than the copy, or of the same age, then it wont be or of the same age, then it wont be restored.restored.

cpio then echoes the message.cpio then echoes the message. ““current <unit14> newer”current <unit14> newer” This is useful built in protection feature that This is useful built in protection feature that

safeguards the latest version of a file. safeguards the latest version of a file. However these can be overridden with the –However these can be overridden with the –u(unconditional) option .u(unconditional) option .

Page 8: BACKUPS

Displaying the archiveDisplaying the archive

The –t option displays the contents of the The –t option displays the contents of the device without restoring the files. This option device without restoring the files. This option must be combined with the –i key option.must be combined with the –i key option.

$cpio –itv “*.sh” $cpio –itv “*.sh” << /dev/rdsk/foq18dt /dev/rdsk/foq18dt Other optionsOther options -d -d creates directories as and when needed creates directories as and when needed -c-c writes header information in ASCII form for writes header information in ASCII form for

portabilityportability -r renames the file in interactive manner-r renames the file in interactive manner -f exp copies all files except those in expression-f exp copies all files except those in expression

Copy all files except those in expressionCopy all files except those in expression $Cpio –ivf “*.c” $Cpio –ivf “*.c” << /dev/rdsk/foq18dt /dev/rdsk/foq18dt

Page 9: BACKUPS

The “tape” archive Program – the tar The “tape” archive Program – the tar CommandCommand

tar is a versatile command with certain exclusive tar is a versatile command with certain exclusive features not found in cpio.features not found in cpio.

It doesn’t use standard input to obtain its file list. It doesn’t use standard input to obtain its file list. Tar accepts file and directory names as arguments.Tar accepts file and directory names as arguments.

It copies one or more entire directory trees, i.e. it It copies one or more entire directory trees, i.e. it operates recursively by default.operates recursively by default.

It can append to an archive without overwriting the It can append to an archive without overwriting the entire archive.entire archive.

The common key options are.The common key options are. -c -c ---------- copycopy -x-x ---------- extractextract -t-t ---------- table of contentstable of contents -f-f ---------- additionally used for specifying device additionally used for specifying device

namename

Page 10: BACKUPS

Backing up files (-c)Backing up files (-c)

tar accepts directory and file names directly on tar accepts directory and file names directly on command line. The –c option is used to copy files to command line. The –c option is used to copy files to the backup device. The –v verbose option shows the the backup device. The –v verbose option shows the progress of the backupprogress of the backup..

$tar –cvf /dev/rdsk/foq18dt$tar –cvf /dev/rdsk/foq18dt /home/sales/sql/*.sql/home/sales/sql/*.sql aa /home/sales/sql/a.sql /home/sales/sql/a.sql 1 tape block1 tape block aa /home/sales/sql/a.sql /home/sales/sql/a.sql 1 tape block1 tape block

The ‘a’ before each pathname indicates that the file The ‘a’ before each pathname indicates that the file is appended.is appended.

The command backs up all sql scripts with absolute The command backs up all sql scripts with absolute pathnames.pathnames.

They can only be restored in the same directory.They can only be restored in the same directory.

Page 11: BACKUPS

The advantage of tar lies in that it can copy an The advantage of tar lies in that it can copy an entire directory tree with all its subdirectories entire directory tree with all its subdirectories

18 tell to execute the program faster (i.e. 18*2*512 18 tell to execute the program faster (i.e. 18*2*512 bytes)bytes)

$tar –cvfb /dev/rdsk/foq18dt 18*$tar –cvfb /dev/rdsk/foq18dt 18*

The above command doesn’t backs up the hidden The above command doesn’t backs up the hidden files.files.

$tar –cvfb /dev/rdsk/foq18dt 18.$tar –cvfb /dev/rdsk/foq18dt 18.

The above command backs up the hidden files.The above command backs up the hidden files.

The files here are backed up with their relative The files here are backed up with their relative pathnames, assuming they all fit in one diskette.pathnames, assuming they all fit in one diskette.

If they don’t, tar in system V can accommodate If they don’t, tar in system V can accommodate them as much as much as possible and then quit the them as much as much as possible and then quit the program without warning.program without warning.

Page 12: BACKUPS

Multivolume BackupsMultivolume Backups

For multivolume backups tar in solaris uses –For multivolume backups tar in solaris uses –k k option followed by the volume size in kilobytes.option followed by the volume size in kilobytes.

$tar –$tar –cvfkbcvfkb /dev/rdsk/foq18dt 1440 18 index/dev/rdsk/foq18dt 1440 18 index

Output:Output: Volume ends at 1439, blocking factor=18Volume ends at 1439, blocking factor=18 Tar: large file index needs 2 extentsTar: large file index needs 2 extents Tar: Current device seek position =okTar: Current device seek position =ok +++ a index 1439 [extent #1 of ]+++ a index 1439 [extent #1 of ] Tar: please insert new volume, then press Tar: please insert new volume, then press

RETURNRETURNTar estimates that two 1440kb diskettes will be Tar estimates that two 1440kb diskettes will be

required.required.

Page 13: BACKUPS

Restoring FilesRestoring Files

Files are restored with the –x(extract) key option.Files are restored with the –x(extract) key option.

When no file or directory name is specified, it When no file or directory name is specified, it restores all files from the backup device.restores all files from the backup device.

The following command restores the files just The following command restores the files just blocked up.blocked up.

#tar –xvfb /dev/rdsk/foq18dt#tar –xvfb /dev/rdsk/foq18dt Output:Output: /home/sales/sql/a.sql /home/sales/sql/a.sql 169 bytes , 1 tape blocks169 bytes , 1 tape blocks /home/sales/sql/b.sql /home/sales/sql/b.sql 456 bytes , 1 tape blocks456 bytes , 1 tape blocks /home/sales/sql/c.sql /home/sales/sql/c.sql 4855 bytes , 1 tape blocks4855 bytes , 1 tape blocks

Page 14: BACKUPS

Selective ExtractionSelective Extraction It is possible by providing one or more directory or It is possible by providing one or more directory or

filenames.filenames.

#tar –#tar –xvF /dev/rdsk/foq18dt /home/sales/SQL/a.sqlxvF /dev/rdsk/foq18dt /home/sales/SQL/a.sql

Unlike cpio, when files are extracted, the modification time Unlike cpio, when files are extracted, the modification time of the files also remain unchanged. This can be overridden of the files also remain unchanged. This can be overridden by the –m option to reflect the system time at the time of by the –m option to reflect the system time at the time of extraction.extraction.

Key optionsKey options:: Option Option significancesignificance -c-c creates a new archive.creates a new archive. -x-x extracts files from archive.extracts files from archive. -t -t List contents of archive. List contents of archive. -r Appends file at the end of the archive.-r Appends file at the end of the archive. -u Like r, but only if files are newer than -u Like r, but only if files are newer than

archivearchive

Page 15: BACKUPS

Non key optionsNon key options -f device uses pathname device.-f device uses pathname device. -v verbose option lists files in long -v verbose option lists files in long

format.format. -w confirms from user about action -w confirms from user about action

to be taken.to be taken. -b n -b n uses blocking factor n, when n uses blocking factor n, when n

is restricted to 20is restricted to 20 -m-m changes modification time of file to time of changes modification time of file to time of

extraction.extraction. -I file_name-I file_name Takes file name from file(solaris only)Takes file name from file(solaris only) -T file_name-T file_name Takes file name from file(Linux only)Takes file name from file(Linux only) -X file_name-X file_name Excludes filenames in fileExcludes filenames in file -K num-K numMultivolume backup sets size of volume to num kilo Multivolume backup sets size of volume to num kilo

bytes.bytes. -M-M Multivolume backup Linux only.Multivolume backup Linux only. -Z-Z compress/uncompress with gzip(linux only)compress/uncompress with gzip(linux only)

Page 16: BACKUPS

Displaying the archiveDisplaying the archive

Like in cpio, the –t key displays the contents of the Like in cpio, the –t key displays the contents of the device in a long format similar to the listing.device in a long format similar to the listing.

#tar –tvf /dev/rdsk/foq18dt#tar –tvf /dev/rdsk/foq18dt Output:Output: rwxr_xrwx 203/50rwxr_xrwx 203/50 472472 jan 4 09:355 1991 ./dentry.shjan 4 09:355 1991 ./dentry.sh rwxr_xrwx 203/50rwxr_xrwx 203/50 472472 jan 4 09:355 1991 ./dentr.shjan 4 09:355 1991 ./dentr.sh

If you want to extract the file func.sh from the If you want to extract the file func.sh from the diskette.diskette.

#tar –tvf /dev/rdsk/foq18dt func.sh#tar –tvf /dev/rdsk/foq18dt func.sh Tar:func.sh: not found in archive Tar:func.sh: not found in archive Tar failed to find the file because it existed there Tar failed to find the file because it existed there

as ./func.sh and not func.sh put the ./ before the as ./func.sh and not func.sh put the ./ before the file name and get it.file name and get it.

#tar –tvf /dev/rdsk/foq18dt ./func.sh#tar –tvf /dev/rdsk/foq18dt ./func.sh

Page 17: BACKUPS

Other options:Other options: The –r option is used to append a file to an The –r option is used to append a file to an

archive. The unusual outcome of this is that an archive. The unusual outcome of this is that an archive can contain several versions of the same archive can contain several versions of the same file.file.

The –u option also adds a file to an archive but The –u option also adds a file to an archive but only if the file is not already there or is being only if the file is not already there or is being replaced with a newer version. You cant use the –replaced with a newer version. You cant use the –c option when using either –r or –u.c option when using either –r or –u.

The –w option permits interactive copying and The –w option permits interactive copying and restoration. It prints the name of the file and restoration. It prints the name of the file and prompts for the action to be taken(yes or no).prompts for the action to be taken(yes or no).