2 * Amanda, The Advanced Maryland Automatic Network Disk Archiver
3 * Copyright (c) 1991-1998 University of Maryland at College Park
6 * Permission to use, copy, modify, distribute, and sell this software and its
7 * documentation for any purpose is hereby granted without fee, provided that
8 * the above copyright notice appear in all copies and that both that
9 * copyright notice and this permission notice appear in supporting
10 * documentation, and that the name of U.M. not be used in advertising or
11 * publicity pertaining to distribution of the software without specific,
12 * written prior permission. U.M. makes no representations about the
13 * suitability of this software for any purpose. It is provided "as is"
14 * without express or implied warranty.
16 * U.M. DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL U.M.
18 * BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
19 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
20 * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
21 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
23 * Authors: the Amanda Development Team. Its members are listed in a
24 * file named AUTHORS, in the root directory of this distribution.
27 * $Id: amtrmlog.c,v 1.17 2006/07/25 18:27:57 martinea Exp $
29 * trims number of index files to only those still in system. Well
30 * actually, it keeps a few extra, plus goes back to the last level 0
36 #ifdef HAVE_NETINET_IN_SYSTM_H
37 #include <netinet/in_systm.h>
45 int amtrmidx_debug = 0;
47 int main(int argc, char **argv);
55 int no_keep; /* files per system to keep */
56 char **output_find_log;
62 char *oldfile = NULL, *newfile = NULL;
63 time_t today, date_keep;
72 int new_argc, my_argc;
73 char **new_argv, **my_argv;
78 set_pname("amtrmlog");
80 /* Don't die when child closes pipe */
81 signal(SIGPIPE, SIG_IGN);
83 parse_conf(argc, argv, &new_argc, &new_argv);
87 if (my_argc > 1 && strcmp(my_argv[1], "-t") == 0) {
94 fprintf(stderr, "Usage: %s [-t] <config> [-o configoption]*\n", my_argv[0]);
98 dbopen(DBG_SUBDIR_SERVER);
99 dbprintf(("%s: version %s\n", my_argv[0], version()));
101 config_name = my_argv[1];
103 config_dir = vstralloc(CONFIG_DIR, "/", config_name, "/", NULL);
104 conffile = stralloc2(config_dir, CONFFILE_NAME);
105 if (read_conffile(conffile)) {
106 error("errors processing amanda config file \"%s\"", conffile);
111 dbrename(config_name, DBG_SUBDIR_SERVER);
113 report_bad_conf_arg();
115 conf_diskfile = getconf_str(CNF_DISKFILE);
116 if (*conf_diskfile == '/') {
117 conf_diskfile = stralloc(conf_diskfile);
119 conf_diskfile = stralloc2(config_dir, conf_diskfile);
121 if (read_diskfile(conf_diskfile, &diskl) < 0) {
122 error("could not load disklist \"%s\"", conf_diskfile);
125 amfree(conf_diskfile);
127 conf_tapelist = getconf_str(CNF_TAPELIST);
128 if (*conf_tapelist == '/') {
129 conf_tapelist = stralloc(conf_tapelist);
131 conf_tapelist = stralloc2(config_dir, conf_tapelist);
133 if (read_tapelist(conf_tapelist)) {
134 error("could not load tapelist \"%s\"", conf_tapelist);
137 amfree(conf_tapelist);
139 today = time((time_t *)NULL);
140 dumpcycle = getconf_int(CNF_DUMPCYCLE);
143 date_keep = today - (dumpcycle * 86400);
145 output_find_log = find_log();
147 /* determine how many log to keep */
148 no_keep = getconf_int(CNF_TAPECYCLE) * 2;
149 dbprintf(("Keeping %d log file%s\n", no_keep, (no_keep == 1) ? "" : "s"));
151 conf_logdir = getconf_str(CNF_LOGDIR);
152 if (*conf_logdir == '/') {
153 conf_logdir = stralloc(conf_logdir);
155 conf_logdir = stralloc2(config_dir, conf_logdir);
157 olddir = vstralloc(conf_logdir, "/oldlog", NULL);
158 if (mkpdir(olddir, 02700, (uid_t)-1, (gid_t)-1) != 0) {
159 error("could not create parents of %s: %s", olddir, strerror(errno));
162 if (mkdir(olddir, 02700) != 0 && errno != EEXIST) {
163 error("could not create %s: %s", olddir, strerror(errno));
167 if (stat(olddir,&stat_old) == -1) {
168 error("can't stat oldlog directory \"%s\": %s", olddir, strerror(errno));
172 if (!S_ISDIR(stat_old.st_mode)) {
173 error("Oldlog directory \"%s\" is not a directory", olddir);
177 if ((dir = opendir(conf_logdir)) == NULL) {
178 error("could not open log directory \"%s\": %s", conf_logdir,strerror(errno));
181 while ((adir=readdir(dir)) != NULL) {
182 if(strncmp(adir->d_name,"log.",4)==0) {
184 for (name=output_find_log;*name !=NULL; name++) {
185 if((strlen(adir->d_name) >= 13 &&
186 strlen(*name) >= 13 &&
187 adir->d_name[12] == '.' && (*name)[12] == '.' &&
188 strncmp(adir->d_name,*name,12)==0) ||
189 strncmp(adir->d_name,*name,18)==0) {
194 logname=newvstralloc(logname,
195 conf_logdir, "/" ,adir->d_name, NULL);
196 if(stat(logname,&stat_log)==0) {
197 if((time_t)stat_log.st_mtime > date_keep) {
202 oldfile = newvstralloc(oldfile,
203 conf_logdir, "/", adir->d_name, NULL);
204 newfile = newvstralloc(newfile,
205 olddir, "/", adir->d_name, NULL);
206 if (rename(oldfile,newfile) != 0) {
207 error("could not rename \"%s\" to \"%s\": %s",
208 oldfile, newfile, strerror(errno));
215 for (name = output_find_log; *name != NULL; name++) {
218 amfree(output_find_log);
226 free_disklist(&diskl);
227 free_new_argv(new_argc, new_argv);
228 free_server_config();