diff options
author | green <green@FreeBSD.org> | 2004-04-13 02:58:06 +0000 |
---|---|---|
committer | green <green@FreeBSD.org> | 2004-04-13 02:58:06 +0000 |
commit | 581462d4656a457daa431e77d6ec725f56cba982 (patch) | |
tree | a1d674955b8c95633685662ea70b1c1e05573efd /sbin/restore | |
parent | a98a06d2276b300662ee818529a9a455454f506c (diff) | |
download | FreeBSD-src-581462d4656a457daa431e77d6ec725f56cba982.zip FreeBSD-src-581462d4656a457daa431e77d6ec725f56cba982.tar.gz |
Add -P arguments for dump(8) and restore(8) which allow the user to
use backup methods other than files and tapes. The -P argument is
a normal sh(1) pipeline with either $DUMP_VOLUME or $RESTORE_VOLUME
defined in the environment, respectively.
For example, I can back up my home to three DVD+R[W]s as so:
Filesystem 1K-blocks Used Avail Capacity Mounted on
/dev/ad0s2e 40028550 10093140 26733126 27% /home
green# dump -0 -L -C16 -B4589840 -P 'growisofs -Z /dev/cd0=/dev/fd/0' /home
Diffstat (limited to 'sbin/restore')
-rw-r--r-- | sbin/restore/extern.h | 2 | ||||
-rw-r--r-- | sbin/restore/main.c | 37 | ||||
-rw-r--r-- | sbin/restore/restore.8 | 33 | ||||
-rw-r--r-- | sbin/restore/tape.c | 39 |
4 files changed, 92 insertions, 19 deletions
diff --git a/sbin/restore/extern.h b/sbin/restore/extern.h index 2d1f464..9edf6f9 100644 --- a/sbin/restore/extern.h +++ b/sbin/restore/extern.h @@ -87,7 +87,7 @@ void rst_closedir(RST_DIR *dirp); void runcmdshell(void); char *savename(char *); void setdirmodes(int); -void setinput(char *); +void setinput(char *, int); void setup(void); void skipdirs(void); void skipfile(void); diff --git a/sbin/restore/main.c b/sbin/restore/main.c index 9006ea5..b2ace5d 100644 --- a/sbin/restore/main.c +++ b/sbin/restore/main.c @@ -62,6 +62,7 @@ static const char rcsid[] = int bflag = 0, cvtflag = 0, dflag = 0, vflag = 0, yflag = 0; int hflag = 1, mflag = 1, Nflag = 0; int uflag = 0; +int pipecmd = 0; char command = '\0'; long dumpnum = 1; long volno = 0; @@ -93,10 +94,9 @@ main(int argc, char *argv[]) (void)setlocale(LC_ALL, ""); - if ((inputdev = getenv("TAPE")) == NULL) - inputdev = _PATH_DEFTAPE; + inputdev = NULL; obsolete(&argc, &argv); - while ((ch = getopt(argc, argv, "b:df:himNRrs:tuvxy")) != -1) + while ((ch = getopt(argc, argv, "b:df:himNP:Rrs:tuvxy")) != -1) switch(ch) { case 'b': /* Change default tape blocksize. */ @@ -111,8 +111,18 @@ main(int argc, char *argv[]) dflag = 1; break; case 'f': + if (pipecmd) + errx(1, + "-P and -f options are mutually exclusive"); inputdev = optarg; break; + case 'P': + if (!pipecmd && inputdev) + errx(1, + "-P and -f options are mutually exclusive"); + inputdev = optarg; + pipecmd = 1; + break; case 'h': hflag = 0; break; @@ -165,7 +175,9 @@ main(int argc, char *argv[]) (void) signal(SIGTERM, SIG_IGN); setlinebuf(stderr); - setinput(inputdev); + if (inputdev == NULL && (inputdev = getenv("TAPE")) == NULL) + inputdev = _PATH_DEFTAPE; + setinput(inputdev, pipecmd); if (argc == 0) { argc = 1; @@ -277,12 +289,17 @@ main(int argc, char *argv[]) static void usage() { - (void)fprintf(stderr, "usage:\t%s\n\t%s\n\t%s\n\t%s\n\t%s\n", - "restore -i [-cdhmNuvy] [-b blocksize] [-f file] [-s fileno]", - "restore -r [-cdNuvy] [-b blocksize] [-f file] [-s fileno]", - "restore -R [-cdNuvy] [-b blocksize] [-f file] [-s fileno]", - "restore -x [-cdhmNuvy] [-b blocksize] [-f file] [-s fileno] [file ...]", - "restore -t [-cdhNuvy] [-b blocksize] [-f file] [-s fileno] [file ...]"); + const char *const common = + "[-b blocksize] [-P pipecmd | -f file] [-s fileno]"; + const char *const fileell = "[file ...]"; + + (void)fprintf(stderr, "usage:\t%s %s\n\t%s %s\n\t%s %s\n" + "\t%s %s %s\n\t%s %s %s\n", + "restore -i [-cdhmNuvy]", common, + "restore -r [-cdNuvy]", common, + "restore -R [-cdNuvy]", common, + "restore -x [-cdhmNuvy]", common, fileell, + "restore -t [-cdhNuvy]", common, fileell); done(1); } diff --git a/sbin/restore/restore.8 b/sbin/restore/restore.8 index 4d970a7..8b1514a 100644 --- a/sbin/restore/restore.8 +++ b/sbin/restore/restore.8 @@ -40,32 +40,32 @@ .Fl i .Op Fl cdhmNuvy .Op Fl b Ar blocksize -.Op Fl f Ar file +.Op Fl f Ar file | Fl P Ar pipecommand .Op Fl s Ar fileno .Nm .Fl R .Op Fl cdNuvy .Op Fl b Ar blocksize -.Op Fl f Ar file +.Op Fl f Ar file | Fl P Ar pipecommand .Op Fl s Ar fileno .Nm .Fl r .Op Fl cdNuvy .Op Fl b Ar blocksize -.Op Fl f Ar file +.Op Fl f Ar file | Fl P Ar pipecommand .Op Fl s Ar fileno .Nm .Fl t .Op Fl cdhNuvy .Op Fl b Ar blocksize -.Op Fl f Ar file +.Op Fl f Ar file | Fl P Ar pipecommand .Op Fl s Ar fileno .Op Ar .Nm .Fl x .Op Fl cdhmNuvy .Op Fl b Ar blocksize -.Op Fl f Ar file +.Op Fl f Ar file | Fl P Ar pipecommand .Op Fl s Ar fileno .Op Ar .Pp @@ -93,7 +93,9 @@ The utility works across a network; to do this see the .Fl f -flag described below. +and +.Fl P +flags described below. Other arguments to the command are file or directory names specifying the files that are to be restored. Unless the @@ -304,6 +306,25 @@ or .Nm reads from the named file on the remote host using .Xr rmt 8 . +.It Fl P Ar pipecommand +Use +.Xr popen 3 +to execute the +.Xr sh 1 +script string defined by +.Ar pipecommand +as the input for every volume in the backup. +This child pipeline's +.Dv stdout +.Pa ( /dev/fd/1 ) +is redirected to the +.Nm +input stream, and the environment variable +.Ev RESTORE_VOLUME +is set to the current volume number being read. +The +.Ar pipecommand +script is started each time a volume is loaded, as if it were a tape drive. .It Fl h Extract the actual directory, rather than the files that it references. diff --git a/sbin/restore/tape.c b/sbin/restore/tape.c index 0b59364..6a2163b 100644 --- a/sbin/restore/tape.c +++ b/sbin/restore/tape.c @@ -65,6 +65,8 @@ static const char rcsid[] = static long fssize = MAXBSIZE; static int mt = -1; static int pipein = 0; +static int pipecmdin = 0; +static FILE *popenfp = NULL; static char *magtape; static int blkcnt; static int numtrec; @@ -109,7 +111,7 @@ static void xtrskip(char *, long); * Set up an input source */ void -setinput(char *source) +setinput(char *source, int ispipecommand) { FLUSHTAPEBUF(); if (bflag) @@ -118,6 +120,9 @@ setinput(char *source) newtapebuf(NTREC > HIGHDENSITYTREC ? NTREC : HIGHDENSITYTREC); terminal = stdin; + if (ispipecommand) + pipecmdin++; + else #ifdef RRESTORE if (strchr(source, ':')) { host = source; @@ -182,6 +187,15 @@ setup(void) struct stat stbuf; vprintf(stdout, "Verify tape and initialize maps\n"); + if (pipecmdin) { + if (setenv("RESTORE_VOLUME", "1", 1) == -1) { + fprintf(stderr, "Cannot set $RESTORE_VOLUME: %s\n", + strerror(errno)); + done(1); + } + popenfp = popen(magtape, "r"); + mt = popenfp ? fileno(popenfp) : -1; + } else #ifdef RRESTORE if (host) mt = rmtopen(magtape, 0); @@ -304,6 +318,10 @@ getvol(long nextvol) } if (volno == 1) return; + if (pipecmdin) { + closemt(); + goto getpipecmdhdr; + } goto gethdr; } again: @@ -364,6 +382,19 @@ again: (void) strcpy(magtape, buf); magtape[strlen(magtape) - 1] = '\0'; } + if (pipecmdin) { + char volno[sizeof("2147483647")]; + +getpipecmdhdr: + (void)sprintf(volno, "%d", newvol); + if (setenv("RESTORE_VOLUME", volno, 1) == -1) { + fprintf(stderr, "Cannot set $RESTORE_VOLUME: %s\n", + strerror(errno)); + done(1); + } + popenfp = popen(magtape, "r"); + mt = popenfp ? fileno(popenfp) : -1; + } else #ifdef RRESTORE if (host) mt = rmtopen(magtape, 0); @@ -493,7 +524,7 @@ setdumpnum(void) rmtioctl(MTFSF, dumpnum - 1); else #endif - if (ioctl(mt, MTIOCTOP, (char *)&tcom) < 0) + if (!pipecmdin && ioctl(mt, MTIOCTOP, (char *)&tcom) < 0) fprintf(stderr, "ioctl MTFSF: %s\n", strerror(errno)); } @@ -982,6 +1013,10 @@ closemt(void) if (mt < 0) return; + if (pipecmdin) { + pclose(popenfp); + popenfp = NULL; + } else #ifdef RRESTORE if (host) rmtclose(); |