-
Notifications
You must be signed in to change notification settings - Fork 0
/
backup
1983 lines (1978 loc) · 52.6 KB
/
backup
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
Subject: v19i102: Save files that have changed in the last little while
Newsgroups: comp.sources.unix
Sender: sources
Approved: rsalz@uunet.UU.NET
Submitted-by: Rayan Zachariassen <rayan@ai.toronto.edu>
Posting-number: Volume 19, Issue 102
Archive-name: backup
The backup program is an online disk-to-disk incremental backup utility
that maintains a file queue in a seperate partition, copying files that
have been modified since its last run to the special backup filesystem.
As space is needed on the backup partition, just enough of the oldest files
are removed to make space for new file copies. In other words, a fixed
size (in disk blocks) queue of recently modified files.
Assumes SunOS filesystem interface (vnodes, etc.), would not be hard to
port to straight BSD filesystem.
#! /bin/sh
# This is a shell archive. Remove anything before this line, then unpack
# it by saving it into a file and typing "sh file". To overwrite existing
# If this archive is complete, you will see the following message at the end:
# "End of shell archive."
#
# Contents:
# README Makefile backup.8 backup.c backup.conf
# backup.filter getback.1 getback.c getback.sh ilw.c
PATH=/bin:/usr/bin:/usr/ucb ; export PATH
if test -f README -a "${1}" != "-c" ; then
echo shar: Will not over-write existing file \"README\"
else
echo shar: Extracting \"README\" \(1083 characters\)
sed "s/^X//" >README <<'END_OF_README'
XThe backup program is an online disk-to-disk incremental backup utility
Xthat maintains a file queue in a seperate partition, copying files that
Xhave been modified since its last run to the special backup filesystem.
XAs space is needed on the backup partition, just enough of the oldest files
Xare removed to make space for new file copies. In other words, a fixed
Xsize (in disk blocks) queue of recently modified files.
X
X
XDefaults, overridden by command line arguments:
X
X BACKUP /backup
X CONFIG /etc/backup.conf
X MAXSIZE 100k
X DELTA 24hr
X
XAssumes SunOS filesystem interface (vnodes, etc.), would not
Xbe hard to port to straight BSD filesystem.
X
XInstallation:
X
X- Edit Makefile.
X- Tweak default parameters in backup.c if you don't like the above values.
X- Create and install a configuration file, see the example in ./backup.conf.
X- make; make install
X- Allocate a /backup partition, create and mount an empty filesystem on it.
X- Add a crontab entry, e.g.:
X 37 4,12,20 * * * /local/etc/backup
X
XThanks to Ken Lalonde for code tweaks, getback.sh, and most of this README file
X
Xrayan
END_OF_README
if test 1083 -ne `wc -c <README`; then
echo shar: \"README\" unpacked with wrong size!
fi
# end of overwriting check
fi
if test -f Makefile -a "${1}" != "-c" ; then
echo shar: Will not over-write existing file \"Makefile\"
else
echo shar: Extracting \"Makefile\" \(549 characters\)
sed "s/^X//" >Makefile <<'END_OF_Makefile'
XDESTDIR=/local
XCOPTS = # -Bstatic
XCFLAGS = $(COPTS) -DCONFIG=\"$(DESTDIR)/etc/backup.conf\"
X
Xall: backup getback
X
Xbackup: backup.o ilw.o
X $(CC) $(CFLAGS) -o backup backup.o ilw.o
X
Xinstall: all
X install -m 710 backup $(DESTDIR)/etc/backup
X install -m 755 getback $(DESTDIR)/bin/getback
X install -c -m 644 backup.8 $(DESTDIR)/man/man8
X install -c -m 644 getback.1 $(DESTDIR)/man/man1
X
Xclean:
X rm -f *.o backup getback make.log
X
Xdist:
X shar README Makefile backup.8 backup.c backup.conf backup.filter getback.1 getback.c getback.sh ilw.c > backup.shar
END_OF_Makefile
if test 549 -ne `wc -c <Makefile`; then
echo shar: \"Makefile\" unpacked with wrong size!
fi
# end of overwriting check
fi
if test -f backup.8 -a "${1}" != "-c" ; then
echo shar: Will not over-write existing file \"backup.8\"
else
echo shar: Extracting \"backup.8\" \(4871 characters\)
sed "s/^X//" >backup.8 <<'END_OF_backup.8'
X.TH BACKUP 8 "UofToronto"
X.SH NAME
Xbackup \- save files that have changed in the last little while
X.SH SYNOPSIS
X.B backup
X[ -devz
X.RI -b #
X] [ -c
X.I backup.conf
X] [ -o
X.I /backup
X] [
X.I /path
X\&... ]
X.SH DESCRIPTION
X.I Backup
Xcopies to a special filesystem hierarchy all files which have been modified
Xsince the previous run of
X.I backup
Xon that subtree of the filesystem.
XThe subtree(s) may be specified explicitly on the command line.
XIf none are given, a configuration file (default is
X.BR /local/etc/backup.conf )
Xis read to determine which subtrees to scan for recently changed files.
XIf subtrees are explicitly listed, a run is forced. Otherwise,
X.I backup
Xwill only do its deed if the subtree is due for backing up as indicated
Xby the interval time, and the last backup scan time, in the configuration
Xfile. If the subtree is not found in the configuration file, all files
Xchanged during the last day will be selected.
X.PP
XThe format of the configuration file is:
X.IP -
Xlines starting with
X.B #
Xare ignored.
X.IP -
Xall other lines contain four space-separated fields:
X.IP
XField 1 is the path name of a subtree to back up.
X.IP
XField 2 is the desired interval between backups.
X.IP
XField 3 is the name of a filter file containing glob patterns,
Xone per line, describing names of files to
X.I ignore
Xwhen scanning the subtree.
X.IP
XField 4 is a timestamp in the exact syntax of
X.BR ctime (3).
X.PP
XThis is a sample configuration file:
X.ta 1.7i,2.3i,3.7i
X.nf
X
X# path intvl filter file last done
X/usr 8h /dev/null Sat Apr 30 00:33:03 1988
X/homes/neat/car 8h /etc/filter.u Sat Apr 30 00:33:03 1988
X/homes/neat/cdr 8h /etc/filter.u Sat Apr 30 00:33:03 1988
X/var 24h /dev/null Sat Apr 30 00:33:03 1988
X.fi
X.PP
XThe first three fields are maintained manually (and defaulted if
X.I backup
Xis given a subtree argument not appearing in the configuration file).
XThe last field is maintained by the program itself. After every successful
Xrun, the configuration file is written out containing the updated timestamp
Xinformation. The purpose of the configuration file is to allow all information
Xbe maintained in this form, and the
X.I backup
Xprogram started very frequently by
X.BR cron (8).
X.PP
XBacked up files are stored in a parallel directory hierarchy under the
Xroot of the backup hierarchy (by default
X.BR /backup ).
XEach backed up file has associated with it a directory whose relative
Xpathname under the root of the backup hierarchy is its full pathname.
XThis directory contains one or more backed up files in files named by
Xthe date of the backup. Thus, a user with a file
X.br
X.sp
X.I /homes/neat/car/user/src/stuff.c
X.br
X.sp
Xthat he has been working on and hence has been backed up a number
Xof times might find a set of files
X.br
X.sp
X.IR /backup/homes/neat/car/user/src/stuff.c/Apr30-06:05 ,
X.IR /backup/homes/neat/car/user/src/stuff.c/Apr30-09:05 ,
X.br
X.sp
Xand
X.br
X.sp
X.IR /backup/homes/neat/car/user/src/stuff.c/Apr30-12:05 .
X.br
X.sp
XPermissions and owner/group are copied with the files.
X.PP
XAs the backups are very expensive in terms of file space only
Xfiles of less than some size, currently 100 kilobytes, are saved.
XFiles can be deselected by creating a filter file, and adding to it
Xglob patterns that would match suspicious names. Typically,
Xfor example
X.IR core ,
X.IR a.out ,
Xemacs temporaries like
X.I #*
Xor
X.I *~
Xand many others, are not backed up.
X.PP
XWhen filespace usage on the backup root's file system passes a high water mark,
Xcurrently 95%,
X.I
Xbackup
Xdeletes files in order by modification date until it has enough space under
Xthe high water mark to add new backup files.
X.PP
X.I Backup
Xreads the entire i-list for the filesystem, saving information such as the
Xmodification time and type of the inode. Thereafter all the appropriate
Xfile names are identified by tree walks in the filesystem, and backed up.
XIf files must be deleted from the backup hierarchy,
Xthen it does the same for the backup subtree (only deleting instead of
Xbackup up!).
X.PP
XThe various options are:
X.IP -b
Xthe argument is the largest size in kilobytes of files to consider backing up.
X.IP -c
Xspecifies an alternate configuration file.
X.IP -d
Xturns a little bit of debugging output on.
X.IP -e
Xwill not ignore executable files when looking for files to back up.
X.IP -o
Xspecifies an alternate backup hierarchy to place backed up files.
X.IP -v
Xturns on verbosity, and can be doubled for more chatter.
X.IP -z
Xasks the program to go through the motions, but to not take any action
Xwhatsoever.
X.SH FILES
X.ta 2.5i
X/backup - mounted filesystem for backups
X.br
X/local/etc/backup.conf - backup configuration information
X.SH INFO
XOriginal idea by Ciaran O'Donnell of U of Waterloo, whose version was
Xwritten in 1975. This is an independent reimplementation native to the
XSunOS environment, written by Rayan Zachariassen at U of Toronto
X(bug fixes and improvements to <rayan@ai.toronto.edu>).
END_OF_backup.8
if test 4871 -ne `wc -c <backup.8`; then
echo shar: \"backup.8\" unpacked with wrong size!
fi
# end of overwriting check
fi
if test -f backup.c -a "${1}" != "-c" ; then
echo shar: Will not over-write existing file \"backup.c\"
else
echo shar: Extracting \"backup.c\" \(28044 characters\)
sed "s/^X//" >backup.c <<'END_OF_backup.c'
X/*
X * backup - do incremental disk-to-disk backups of individual files
X *
X * This code is a complete reimplementation (for the SunOS environment)
X * of an original idea by Ciaran O'Donnell. This implementation is
X * Copyright 1988 by Rayan Zachariassen, solely to prevent you selling
X * it or putting your name on it. Free (gratis) redistribution is
X * encouraged.
X */
X
X#ifndef lint
Xstatic char *RCSid = "$Header: /ai/car/src/etc/backup/RCS/backup.c,v 1.2 89/05/23 22:13:19 rayan Exp $";
X#endif lint
X
X#include <stdio.h>
X#include <ctype.h>
X#include <mntent.h>
X#include <values.h>
X#include <errno.h>
X#include <sys/param.h>
X#include <sys/types.h>
X#include <sys/file.h>
X#include <sys/stat.h>
X#include <sys/time.h>
X#include <sys/vnode.h>
X#include <sys/vfs.h>
X#include <ufs/inode.h>
X#include <ufs/fs.h>
X#include <sys/dir.h>
X
X#define HIWATER 95 /* % of backup filesystem used at high water */
X#define LOWATER 85 /* % of backup filesystem used at low water */
X
X#define BACKUP "/backup" /* backup filesystem mount point */
X#ifndef CONFIG
X#define CONFIG "/etc/backup.conf" /* configuration file */
X#endif /* !CONFIG */
X
X#define MAXSIZE 100000 /* default maximum size of files to back up */
X
X#define DELTA (24*60*60) /* default time since last backup */
X
Xint maxsize = MAXSIZE; /* maximum size in bytes of files to back up */
Xint doexecs = 0; /* should we back up executables? */
X
Xstruct a_path {
X char *path; /* path to tree hierarchy we want to back up */
X char *glob; /* pointer to the contents of the filter file */
X time_t newer; /* back up files newer than this time */
X struct config *config;/* back pointer to config structure */
X struct a_path *next;
X};
X
Xstruct a_dev {
X char *text; /* raw device name */
X struct a_path *paths; /* pointer to list of paths */
X struct a_dev *next; /* pointer to next device */
X};
X
Xstruct a_dev *devlist = NULL;
X
Xstruct config {
X char *path; /* top of hierarchy we want to back up */
X char *interval; /* how often to do incrementals */
X char *filter; /* name of file containing regexp's */
X char *line; /* the config file line excluding time field */
X time_t lasttime; /* last time incrementals were done here */
X struct config *next;
X};
X
Xchar *progname, *backup, *devbackup, *backupmnt;
Xint debug, verbose, dryrun;
Xtime_t now, lasttime;
X
Xextern int optind;
Xextern char *optarg;
Xextern char *emalloc();
X
X#define EMSG(x) (errno < sys_nerr ? sys_errlist[x] : \
X (sprintf(errmsgbuf, "unknown error %d", errno), errmsgbuf))
X
Xextern int errno, sys_nerr;
Xextern char *sys_errlist[];
Xchar errmsgbuf[30];
X
Xmain(argc, argv)
X int argc;
X char *argv[];
X{
X int c, errflag;
X char *cffile;
X struct a_dev *lp;
X struct a_path *pp;
X struct config *cf, *cfp;
X FILE *cf_fp;
X extern time_t time();
X extern char *getdevice(), *getpath(), *ctime();
X extern struct config *readconfig();
X extern void writeconfig();
X
X progname = argv[0];
X errflag = 0;
X dryrun = 0;
X debug = verbose = 0;
X backup = BACKUP;
X cffile = CONFIG;
X while ((c = getopt(argc, argv, "bc:devo:z")) != EOF) {
X switch (c) {
X case 'b': /* don't back up files larger than arg kbytes */
X if ((maxsize = atoi(optarg)) < 1) {
X fprintf(stderr,
X "%s: illegal argument to -b: %d\n",
X progname, optarg);
X ++errflag;
X } else
X maxsize <<= 10; /* multiple of 1k */
X break;
X case 'c': /* specify alternate configuration file */
X cffile = optarg;
X break;
X case 'd': /* debugging output */
X ++debug;
X break;
X case 'e': /* copy executables */
X ++doexecs;
X break;
X case 'v': /* be (more and more) verbose */
X ++verbose;
X break;
X case 'o': /* specify alternate backup location */
X backup = optarg;
X break;
X case 'z': /* fake it */
X ++dryrun;
X break;
X default:
X ++errflag;
X }
X }
X (void) time(&now);
X if ((cf_fp = fopen(cffile, "r+")) == NULL) {
X fprintf(stderr, "%s: open(%s): %s\n",
X progname, cffile, EMSG(errno) /* ... */);
X Usage();
X }
X if (verbose
X && flock(fileno(cf_fp), LOCK_EX|LOCK_NB) < 0
X && errno == EWOULDBLOCK)
X printf("waiting for exclusive lock on %s\n", cffile);
X if (flock(fileno(cf_fp), LOCK_EX) < 0) {
X fprintf(stderr, "%s: can't get lock on %s\n",
X progname, cffile);
X exit(1);
X } else if (verbose > 1) {
X setbuf(stdout, (char *)NULL);
X printf("Start at %slocked %s\n", ctime(&now), cffile);
X }
X cf = readconfig(cf_fp);
X if (optind == argc) {
X /* figure out what we should back up */
X for (cfp = cf; cfp != NULL; cfp = cfp->next) {
X if (cfp->lasttime == 0) {
X if (debug)
X printf("%s: lasttime is 0\n", cfp->path);
X continue;
X }
X /* give 5 minutes leeway */
X if (cfp->lasttime + interval(cfp->interval) < now+300) {
X /* add this path to the list */
X if (debug)
X printf("adding %s\n", cfp->path);
X addpath(cfp);
X } else if (debug)
X printf("ignoring %s: lasttime=%d interval=%s now=%d\n",
X cfp->path, cfp->lasttime, cfp->interval, now);
X }
X } else {
X for (; optind < argc; ++optind) {
X for (cfp = cf; cfp != NULL; cfp = cfp->next) {
X if (cfp->lasttime == 0)
X continue;
X if (strcmp(cfp->path, argv[optind]) == 0) {
X addpath(cfp);
X break;
X }
X }
X if (cfp == NULL) {
X /* append new entry to config file */
X for (cfp = cf; cfp != NULL && cfp->next != NULL;
X cfp = cfp->next)
X continue;
X if (cfp != NULL) {
X cfp->next = (struct config *)
X emalloc((u_int)sizeof (struct config));
X cfp = cfp->next;
X cfp->next = NULL;
X cfp->line = NULL;
X cfp->interval = "24h";
X cfp->path = argv[optind];
X cfp->filter = "/dev/null";
X }
X cfp->lasttime = now - DELTA;
X addpath(cfp);
X }
X }
X }
X
X if ((devbackup = getdevice(backup, MNTTAB)) == NULL &&
X (devbackup = getdevice(backup, MOUNTED)) == NULL) {
X fprintf(stderr, "%s: could not determine backup device\n",
X progname);
X exit(1);
X }
X if ((backupmnt = getpath(devbackup, MNTTAB)) == NULL &&
X (backupmnt = getpath(devbackup, MOUNTED)) == NULL) {
X fprintf(stderr, "%s: could not determine mount point for %s\n",
X progname, devbackup);
X ++errflag;
X }
X if (errflag)
X Usage();
X (void) umask(0);
X for (lp = devlist; lp != NULL; lp = lp->next)
X if (doit(lp->paths, lp->text))
X for (pp = lp->paths; pp != NULL; pp = pp->next)
X pp->config->lasttime = now;
X if (!dryrun)
X writeconfig(cf_fp, cf);
X (void) flock(fileno(cf_fp), LOCK_UN);
X if (verbose > 1) {
X time(&now);
X printf("unlocked %s\nEnd at %s", cffile, ctime(&now));
X }
X (void) fclose(cf_fp);
X#ifdef PROF
X /* drop profiling data in a known place */
X chdir("/tmp");
X#endif
X exit(0);
X}
X
XUsage()
X{
X fprintf(stderr,
X"Usage: %s [ -devz -b# ] [ -c backup.conf ] [ -o /backup ] [ /path ... ]\n",
X progname);
X exit(1);
X}
X
X/*
X * The filter files contain glob expressions, one per line. We need to
X * keep track of which filter files we've read in, since several paths
X * may share the same filters.
X */
X
Xstruct filter {
X char *name;
X char *contents;
X} filters[50];
X
Xint maxfilters = 0;
X
X/* return pointer to contents of a filter file stored away */
X
Xchar *
Xreadfilter(file)
X char *file;
X{
X int i, fd;
X struct stat stbuf;
X
X if (strcmp(file, "/dev/null") == 0)
X return NULL;
X for (i = 0; i < maxfilters
X && i < (sizeof filters/sizeof (struct filter)); ++i) {
X if (strcmp(file, filters[i].name) == 0)
X return filters[i].contents;
X }
X if (i == (sizeof filters/sizeof (struct filter))) {
X fprintf(stderr, "%s: ran out of filters\n", progname);
X exit(1);
X }
X if ((fd = open(file, O_RDONLY, 0)) < 0) {
X fprintf(stderr, "%s: can't open filter file %s\n",
X progname, file);
X exit(1);
X }
X if (fstat(fd, &stbuf) < 0) {
X fprintf(stderr, "%s: can't stat open filter file %s\n",
X progname, file);
X exit(1);
X }
X filters[i].contents = emalloc((u_int)stbuf.st_size+1);
X if (read(fd, filters[i].contents, stbuf.st_size) < stbuf.st_size) {
X fprintf(stderr, "%s: couldn't read %d bytes from %s\n",
X progname, stbuf.st_size, file);
X }
X *(filters[i].contents+stbuf.st_size) = '\0';
X filters[i].name = file;
X ++maxfilters;
X (void) close(fd);
X return filters[i].contents;
X}
X
X/*
X * We maintain a two-level linked list structure (i.e. list of lists),
X * associating paths with their raw device. When there are several paths
X * on the same device, we want to handle them simultaneously and only
X * do the ilist walking once per device. The root of this structure is
X * devlist.
X */
X
Xint
Xaddpath(cfp)
X struct config *cfp;
X{
X struct a_dev *lp;
X struct a_path *pp;
X char *rawdevice;
X
X if (cfp->path == NULL || *cfp->path != '/') {
X fprintf(stderr, "%s: illegal path: %s\n",
X progname,
X cfp->path == NULL ? "(null)" : cfp->path);
X } else if ((rawdevice = getdevice(cfp->path, MNTTAB)) == NULL) {
X fprintf(stderr, "%s: no device for %s\n",
X progname, cfp->path);
X } else if (*rawdevice != '/') {
X fprintf(stderr, "%s: bad device %s for %s\n",
X progname, rawdevice, cfp->path);
X } else {
X /* link the path, device pair into lists */
X for (lp = devlist; lp != NULL; lp = lp->next)
X if (strcmp(lp->text, rawdevice) == 0)
X break;
X pp = (struct a_path *)
X emalloc((u_int)sizeof (struct a_path));
X pp->path = cfp->path;
X pp->newer = cfp->lasttime;
X pp->glob = readfilter(cfp->filter);
X pp->config = cfp;
X if (lp != NULL)
X pp->next = lp->paths;
X else {
X lp = (struct a_dev *)
X emalloc((u_int)sizeof (struct a_dev));
X lp->next = devlist;
X devlist = lp;
X lp->text = emalloc((u_int)strlen(rawdevice)+1);
X (void) strcpy(lp->text, rawdevice);
X pp->next = NULL;
X }
X lp->paths = pp;
X }
X}
X
X/* return the name of the raw device corresponding to a particular file */
X
Xchar *
Xgetdevice(path, table)
X char *path;
X char *table;
X{
X char *cp, *s;
X struct mntent *me;
X FILE *fp;
X struct stat stpath, stdev;
X
X if (lstat(path, &stpath) < 0) {
X fprintf(stderr, "%s: lstat(%s): %s\n",
X progname, path, EMSG(errno));
X return NULL;
X }
X if (!((stpath.st_mode & S_IFMT) & (S_IFREG | S_IFDIR))) {
X fprintf(stderr, "%s: %s is a special file\n", progname, path);
X return NULL;
X }
X if ((fp = setmntent(table, "r")) == NULL) {
X fprintf(stderr, "%s: setmntent(%s): %s\n",
X progname, table, EMSG(errno));
X return NULL;
X }
X while ((me = getmntent(fp)) != NULL) {
X if (strcmp(me->mnt_type, MNTTYPE_42) == 0
X && strncmp(me->mnt_fsname, "/dev/", 5) == 0
X && lstat(me->mnt_fsname, &stdev) == 0
X && stdev.st_rdev == stpath.st_dev) {
X (void) endmntent(fp);
X cp = emalloc((u_int)strlen(me->mnt_fsname)+2);
X (void) strcpy(cp, me->mnt_fsname);
X s = cp + strlen(cp) + 1;
X while (s > cp && *(s - 1) != '/')
X *s = *(s-1), --s;
X if (s > cp)
X *s = 'r';
X return cp;
X }
X }
X (void) endmntent(fp);
X return NULL;
X}
X
X/* get the mount point of the filesystem on the raw device */
X
Xchar *
Xgetpath(device, table)
X char *device;
X char *table;
X{
X char *cp;
X struct mntent *me;
X FILE *fp;
X char devpath[MAXPATHLEN];
X struct stat stpath, stdev;
X extern char *rindex();
X
X (void) strcpy(devpath, device);
X if ((cp = rindex(devpath, '/')) != NULL) {
X ++cp;
X if (*cp == 'r')
X while ((*cp = *(cp+1)) != '\0')
X ++cp;
X }
X if ((fp = setmntent(table, "r")) == NULL) {
X fprintf(stderr, "%s: setmntent(%s): %s\n",
X progname, table, EMSG(errno));
X return NULL;
X }
X while ((me = getmntent(fp)) != NULL) {
X if (strcmp(me->mnt_type, MNTTYPE_42) == 0
X && strcmp(me->mnt_fsname, devpath) == 0
X && lstat(me->mnt_fsname, &stdev) == 0
X && lstat(me->mnt_dir, &stpath) == 0
X && stdev.st_rdev == stpath.st_dev) {
X (void) endmntent(fp);
X cp = emalloc((u_int)strlen(me->mnt_dir)+1);
X (void) strcpy(cp, me->mnt_dir);
X return cp;
X }
X }
X (void) endmntent(fp);
X return NULL;
X}
X
X#define sblock sb_un.u_sblock
X
Xstruct iinfo {
X int inum; /* must be int so can be -ve too */
X u_int blks;
X time_t mtime;
X};
X
Xint
Xcmpiinfo(iip1, iip2)
X register struct iinfo *iip1, *iip2;
X{
X return iip1->mtime - iip2->mtime;
X}
X
Xstruct iinfo *stack[2];
Xlong stacksize[2];
Xlong needspace[2];
Xint top = -1;
X
Xchar *dirmask;
Xint mustfree;
X
X#define SET(v,i) ((v)[(i)/BITSPERBYTE] |= (1<<((i)%BITSPERBYTE)))
X#define TST(v,i) ((v)[(i)/BITSPERBYTE] & (1<<((i)%BITSPERBYTE)))
X
Xint
Xdoit(path, dev)
X struct a_path *path;
X char *dev;
X{
X register struct iinfo *st;
X register int i, inum;
X int fd, hiwater, lowater;
X u_int nfiles;
X union { struct fs u_sblock; char dummy[SBSIZE]; } sb_un;
X char *bitvec, *dirvec, pathbuf[MAXPATHLEN];
X struct a_path *pp;
X struct statfs fsbuf;
X extern int itest(), mkbackup(), rmbackup();
X extern char *calloc();
X
X if (debug)
X printf("doing %s\n", dev);
X if ((fd = open(dev, O_RDONLY, 0)) < 0) {
X fprintf(stderr, "%s: open(%s): %s\n",
X progname, dev, EMSG(errno));
X return 0;
X }
X if (bread(fd, SBLOCK, (char *)&sblock, (long) SBSIZE) < 0) {
X fprintf(stderr, "%s: can't read superblock from %s: %s\n",
X progname, dev, EMSG(errno));
X return 0;
X }
X (void) close(fd);
X nfiles = sblock.fs_ipg * sblock.fs_ncg;
X stack[++top] = (struct iinfo *)calloc(nfiles, sizeof (struct iinfo));
X stacksize[top] = 0;
X needspace[top] = 0;
X dirvec = calloc((nfiles/BITSPERBYTE)+1, 1);
X dirmask = dirvec;
X if (top == 0) {
X /* figure out the oldest lasttime before i-list walk */
X lasttime = now;
X for (pp = path; pp != NULL; pp = pp->next)
X if (pp->newer < lasttime)
X lasttime = pp->newer;
X }
X if (debug)
X printf("%s: scan %d inodes\n", dev, nfiles);
X (void) ilw(dev, itest, 1);
X if (verbose)
X printf("%s: found %d candidate files, with %d blocks total\n",
X dev, stacksize[top], needspace[top]);
X if (stacksize[top] == 0) {
X (void) free(dirvec);
X (void) free((char *)stack[top--]);
X return 0;
X }
X bitvec = calloc((nfiles/BITSPERBYTE)+1, 1);
X if (top == 0) {
X /*
X * This is the filesystem we want to back up.
X * First make sure there is enough free space in the backup
X * filesystem (if not, call myself recursively), then run
X * a file tree walker to copy the indicated files to the
X * backup filesystem.
X */
X if (statfs(backup, &fsbuf) < 0) {
X fprintf(stderr, "%s: statfs(%s): %s\n",
X progname, backup, EMSG(errno));
X exit(1);
X }
X hiwater = (fsbuf.f_blocks-fsbuf.f_bfree
X +fsbuf.f_bavail)*HIWATER/100;
X if (fsbuf.f_blocks - fsbuf.f_bfree + needspace[top] > hiwater) {
X /* need to free some space */
X struct a_path backupdesc;
X /*
X * If you want to free so free space will be at
X * LOWATER after backup finishes, then enable the
X * next line and do s/hiwater/lowater/ in the
X * following line defining mustfree.
X */
X /* lowater = +(hiwater*LOWATER)/HIWATER; */
X mustfree = fsbuf.f_blocks - fsbuf.f_bfree
X + needspace[top] - hiwater;
X /* select all files */
X lasttime = 0;
X maxsize = sblock.fs_dsize * DEV_BSIZE;
X backupdesc.path = backup;
X backupdesc.next = NULL;
X backupdesc.glob = NULL;
X backupdesc.newer = lasttime;
X if (!doit(&backupdesc, devbackup)) {
X fprintf(stderr,
X "%s: Can't walk %s to free space\n",
X progname, backup);
X exit(1);
X }
X }
X for (i = 0, st = stack[top]; i < nfiles; ++i, ++st) {
X if (st->mtime > 0) {
X SET(bitvec, st->inum);
X }
X }
X for (; path != NULL; path = path->next) {
X if (chdir(path->path) < 0) {
X fprintf(stderr, "%s: chdir(%s): %s\n",
X progname, path->path, EMSG(errno));
X exit(1);
X }
X (void) sprintf(pathbuf, "%s/", path->path);
X lasttime = path->newer;
X if (verbose)
X printf("%s: select mtime within %d sec in %s\n",
X dev, now - lasttime, path->path);
X walk(pathbuf, pathbuf + strlen(pathbuf), path->glob,
X mkbackup, bitvec, dirvec);
X }
X } else {
X /*
X * This is the backup filesystem.
X * Sort the inodes selected into oldest-first, then run
X * a file tree walker to delete the files until we have
X * enough space *and* are under the low water mark.
X */
X
X /* assert strcmp(path->path, backup) == 0 */
X /* compress the inode array */
X st = stack[top];
X for (i = inum = 0; i < stacksize[top]; ++inum) {
X if ((st+inum)->mtime > 0) {
X (st+i)->inum = inum;
X (st+i)->blks = (st+inum)->blks;
X (st+i)->mtime = (st+inum)->mtime;
X ++i;
X }
X }
X if (chdir(path->path) < 0) {
X fprintf(stderr, "%s: chdir(%s): %s\n",
X progname, path->path, EMSG(errno));
X exit(1);
X }
X (void) sprintf(pathbuf, "%s/", path->path);
X if (strcmp(backup, backupmnt) != 0) {
X /* backup area is not an entire filesystem */
X walk(pathbuf, pathbuf + strlen(pathbuf), (char *)NULL,
X (int (*)())NULL, (char *)NULL, dirvec);
X /* now all possible inums are negative and rest +ve */
X st = stack[top];
X for (i = inum = 0; i < stacksize[top]; ++inum) {
X if ((st+inum)->inum < 0) {
X (st+i)->inum = inum;
X ++i;
X } else
X (st+i)->inum = 0;
X }
X /* now all possible inums are +ve and rest 0 */
X }
X /* sort it oldest first */
X qsort((char *)stack[top], stacksize[top],
X sizeof (struct iinfo), cmpiinfo);
X /* mustfree has been set in our parent doit() */
X /* go from oldest to newest, truncate after mustfree blocks */
X st = stack[top];
X for (i = 0; i < stacksize[top] && mustfree > 0; ++i, ++st) {
X if (st->inum > 2 && st->mtime > 0) {
X mustfree -= st->blks;
X SET(bitvec, st->inum);
X }
X }
X (void) sprintf(pathbuf, "%s/", path->path);
X walk(pathbuf, pathbuf + strlen(pathbuf), (char *)NULL,
X rmbackup, bitvec, dirvec);
X }
X (void) free(bitvec);
X (void) free(dirvec);
X (void) free((char *)stack[top--]);
X return 1;
X}
X
X/* This routine is used by the inode list walker) to test for relevant inodes */
X
Xitest(ip, inum)
X struct dinode *ip;
X int inum;
X{
X register struct iinfo *iip;
X
X if ((ip->di_mode & S_IFMT) == S_IFREG
X && ip->di_mtime > lasttime
X && ip->di_size < maxsize
X && (doexecs || (ip->di_mode & 07111) == 0)) {
X /* we have a candidate for backing up */
X iip = stack[top] + inum;
X iip->inum = inum;
X stacksize[top] += 1;
X needspace[top] += (iip->blks = ip->di_blocks);
X iip->mtime = ip->di_mtime;
X /*
Xprintf("%6d:\tmode=0%o uid=%d gid=%d size=%d nlink=%d, a=%D m=%D c=%D\n",
X inum, ip->di_mode, ip->di_uid, ip->di_gid, ip->di_size,
X ip->di_nlink, ip->di_atime, ip->di_mtime, ip->di_ctime);
X */
X } else if ((ip->di_mode & S_IFMT) == S_IFDIR)
X SET(dirmask, inum);
X return 0;
X}
X
X/*
X * Create all the directories down a particular backup path, copying stat
X * info from the original directories.
X */
X
Xcreatdirs(dirpath, origpath, stbufp)
X char *dirpath, *origpath;
X struct stat *stbufp;
X{
X char *cp;
X struct stat stbuf;
X extern char *rindex();
X
X if (mkdir(dirpath, stbufp->st_mode & 0777) < 0) {
X if (errno == ENOENT) {
X if ((cp = rindex(dirpath, '/')) > dirpath) {
X *cp = '\0';
X creatdirs(dirpath, origpath, stbufp);
X *cp = '/';
X }
X (void) mkdir(dirpath, (stbufp->st_mode & 0777)|0111);
X if (stat(origpath, &stbuf) == 0) {
X (void) chown(dirpath, stbuf.st_uid,
X stbuf.st_gid);
X if (stbuf.st_mode & 0400)
X stbuf.st_mode |= 0100;
X if (stbuf.st_mode & 040)
X stbuf.st_mode |= 010;
X if (stbuf.st_mode & 04)
X stbuf.st_mode |= 01;
X (void) chmod(dirpath, stbuf.st_mode & 0777);
X } else
X fprintf(stderr, "%s: stat(%s): %s\n",
X progname, origpath, EMSG(errno));
X }
X } else if (stat(origpath, &stbuf) == 0) {
X (void) chown(dirpath, stbuf.st_uid, stbuf.st_gid);
X if (stbuf.st_mode & 0400)
X stbuf.st_mode |= 0100;
X if (stbuf.st_mode & 040)
X stbuf.st_mode |= 010;
X if (stbuf.st_mode & 04)
X stbuf.st_mode |= 01;
X (void) chmod(dirpath, stbuf.st_mode & 0777);
X } else
X fprintf(stderr, "%s: stat(%s): %s\n",
X progname, origpath, EMSG(errno));
X}
X
X/* Create an actual backup file, return its file descriptor */
X
Xint
Xcreatbackup(path, stbufp, filename)