1 /*
2  * src/bin/pg_archivecleanup/pg_archivecleanup.c
3  *
4  * pg_archivecleanup.c
5  *
6  * Production-ready example of an archive_cleanup_command
7  * used to clean an archive when using standby_mode = on in 9.0
8  * or for standalone use for any version of PostgreSQL 8.0+.
9  *
10  * Original author:		Simon Riggs  simon@2ndquadrant.com
11  * Current maintainer:	Simon Riggs
12  */
13 #include "postgres_fe.h"
14 
15 #include <ctype.h>
16 #include <dirent.h>
17 #include <sys/stat.h>
18 #include <fcntl.h>
19 #include <signal.h>
20 #include <sys/time.h>
21 
22 #include "pg_getopt.h"
23 
24 #include "access/xlog_internal.h"
25 
26 const char *progname;
27 
28 /* Options and defaults */
29 bool		debug = false;		/* are we debugging? */
30 bool		dryrun = false;		/* are we performing a dry-run operation? */
31 char	   *additional_ext = NULL;	/* Extension to remove from filenames */
32 
33 char	   *archiveLocation;	/* where to find the archive? */
34 char	   *restartWALFileName; /* the file from which we can restart restore */
35 char		WALFilePath[MAXPGPATH * 2]; /* the file path including archive */
36 char		exclusiveCleanupFileName[MAXFNAMELEN];	/* the oldest file we want
37 													 * to remain in archive */
38 
39 
40 /* =====================================================================
41  *
42  *		  Customizable section
43  *
44  * =====================================================================
45  *
46  *	Currently, this section assumes that the Archive is a locally
47  *	accessible directory. If you want to make other assumptions,
48  *	such as using a vendor-specific archive and access API, these
49  *	routines are the ones you'll need to change. You're
50  *	encouraged to submit any changes to pgsql-hackers@postgresql.org
51  *	or personally to the current maintainer. Those changes may be
52  *	folded in to later versions of this program.
53  */
54 
55 /*
56  *	Initialize allows customized commands into the archive cleanup program.
57  *
58  *	You may wish to add code to check for tape libraries, etc..
59  */
60 static void
Initialize(void)61 Initialize(void)
62 {
63 	/*
64 	 * This code assumes that archiveLocation is a directory, so we use stat
65 	 * to test if it's accessible.
66 	 */
67 	struct stat stat_buf;
68 
69 	if (stat(archiveLocation, &stat_buf) != 0 ||
70 		!S_ISDIR(stat_buf.st_mode))
71 	{
72 		fprintf(stderr, _("%s: archive location \"%s\" does not exist\n"),
73 				progname, archiveLocation);
74 		exit(2);
75 	}
76 }
77 
78 static void
TrimExtension(char * filename,char * extension)79 TrimExtension(char *filename, char *extension)
80 {
81 	int			flen;
82 	int			elen;
83 
84 	if (extension == NULL)
85 		return;
86 
87 	elen = strlen(extension);
88 	flen = strlen(filename);
89 
90 	if (flen > elen && strcmp(filename + flen - elen, extension) == 0)
91 		filename[flen - elen] = '\0';
92 }
93 
94 static void
CleanupPriorWALFiles(void)95 CleanupPriorWALFiles(void)
96 {
97 	int			rc;
98 	DIR		   *xldir;
99 	struct dirent *xlde;
100 	char		walfile[MAXPGPATH];
101 
102 	if ((xldir = opendir(archiveLocation)) != NULL)
103 	{
104 		while (errno = 0, (xlde = readdir(xldir)) != NULL)
105 		{
106 			/*
107 			 * Truncation is essentially harmless, because we skip names of
108 			 * length other than XLOG_FNAME_LEN.  (In principle, one could use
109 			 * a 1000-character additional_ext and get trouble.)
110 			 */
111 			strlcpy(walfile, xlde->d_name, MAXPGPATH);
112 			TrimExtension(walfile, additional_ext);
113 
114 			/*
115 			 * We ignore the timeline part of the XLOG segment identifiers in
116 			 * deciding whether a segment is still needed.  This ensures that
117 			 * we won't prematurely remove a segment from a parent timeline.
118 			 * We could probably be a little more proactive about removing
119 			 * segments of non-parent timelines, but that would be a whole lot
120 			 * more complicated.
121 			 *
122 			 * We use the alphanumeric sorting property of the filenames to
123 			 * decide which ones are earlier than the exclusiveCleanupFileName
124 			 * file. Note that this means files are not removed in the order
125 			 * they were originally written, in case this worries you.
126 			 */
127 			if ((IsXLogFileName(walfile) || IsPartialXLogFileName(walfile)) &&
128 				strcmp(walfile + 8, exclusiveCleanupFileName + 8) < 0)
129 			{
130 				/*
131 				 * Use the original file name again now, including any
132 				 * extension that might have been chopped off before testing
133 				 * the sequence.
134 				 */
135 				snprintf(WALFilePath, sizeof(WALFilePath), "%s/%s",
136 						 archiveLocation, xlde->d_name);
137 
138 				if (dryrun)
139 				{
140 					/*
141 					 * Prints the name of the file to be removed and skips the
142 					 * actual removal.  The regular printout is so that the
143 					 * user can pipe the output into some other program.
144 					 */
145 					printf("%s\n", WALFilePath);
146 					if (debug)
147 						fprintf(stderr,
148 								_("%s: file \"%s\" would be removed\n"),
149 								progname, WALFilePath);
150 					continue;
151 				}
152 
153 				if (debug)
154 					fprintf(stderr, _("%s: removing file \"%s\"\n"),
155 							progname, WALFilePath);
156 
157 				rc = unlink(WALFilePath);
158 				if (rc != 0)
159 				{
160 					fprintf(stderr, _("%s: ERROR: could not remove file \"%s\": %s\n"),
161 							progname, WALFilePath, strerror(errno));
162 					break;
163 				}
164 			}
165 		}
166 
167 		if (errno)
168 			fprintf(stderr, _("%s: could not read archive location \"%s\": %s\n"),
169 					progname, archiveLocation, strerror(errno));
170 		if (closedir(xldir))
171 			fprintf(stderr, _("%s: could not close archive location \"%s\": %s\n"),
172 					progname, archiveLocation, strerror(errno));
173 	}
174 	else
175 		fprintf(stderr, _("%s: could not open archive location \"%s\": %s\n"),
176 				progname, archiveLocation, strerror(errno));
177 }
178 
179 /*
180  * SetWALFileNameForCleanup()
181  *
182  *	  Set the earliest WAL filename that we want to keep on the archive
183  *	  and decide whether we need cleanup
184  */
185 static void
SetWALFileNameForCleanup(void)186 SetWALFileNameForCleanup(void)
187 {
188 	bool		fnameOK = false;
189 
190 	TrimExtension(restartWALFileName, additional_ext);
191 
192 	/*
193 	 * If restartWALFileName is a WAL file name then just use it directly. If
194 	 * restartWALFileName is a .partial or .backup filename, make sure we use
195 	 * the prefix of the filename, otherwise we will remove wrong files since
196 	 * 000000010000000000000010.partial and
197 	 * 000000010000000000000010.00000020.backup are after
198 	 * 000000010000000000000010.
199 	 */
200 	if (IsXLogFileName(restartWALFileName))
201 	{
202 		strcpy(exclusiveCleanupFileName, restartWALFileName);
203 		fnameOK = true;
204 	}
205 	else if (IsPartialXLogFileName(restartWALFileName))
206 	{
207 		int			args;
208 		uint32		tli = 1,
209 					log = 0,
210 					seg = 0;
211 
212 		args = sscanf(restartWALFileName, "%08X%08X%08X.partial",
213 					  &tli, &log, &seg);
214 		if (args == 3)
215 		{
216 			fnameOK = true;
217 
218 			/*
219 			 * Use just the prefix of the filename, ignore everything after
220 			 * first period
221 			 */
222 			XLogFileNameById(exclusiveCleanupFileName, tli, log, seg);
223 		}
224 	}
225 	else if (IsBackupHistoryFileName(restartWALFileName))
226 	{
227 		int			args;
228 		uint32		tli = 1,
229 					log = 0,
230 					seg = 0,
231 					offset = 0;
232 
233 		args = sscanf(restartWALFileName, "%08X%08X%08X.%08X.backup", &tli, &log, &seg, &offset);
234 		if (args == 4)
235 		{
236 			fnameOK = true;
237 
238 			/*
239 			 * Use just the prefix of the filename, ignore everything after
240 			 * first period
241 			 */
242 			XLogFileNameById(exclusiveCleanupFileName, tli, log, seg);
243 		}
244 	}
245 
246 	if (!fnameOK)
247 	{
248 		fprintf(stderr, _("%s: invalid file name argument\n"), progname);
249 		fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname);
250 		exit(2);
251 	}
252 }
253 
254 /* =====================================================================
255  *		  End of Customizable section
256  * =====================================================================
257  */
258 
259 static void
usage(void)260 usage(void)
261 {
262 	printf(_("%s removes older WAL files from PostgreSQL archives.\n\n"), progname);
263 	printf(_("Usage:\n"));
264 	printf(_("  %s [OPTION]... ARCHIVELOCATION OLDESTKEPTWALFILE\n"), progname);
265 	printf(_("\nOptions:\n"));
266 	printf(_("  -d             generate debug output (verbose mode)\n"));
267 	printf(_("  -n             dry run, show the names of the files that would be removed\n"));
268 	printf(_("  -V, --version  output version information, then exit\n"));
269 	printf(_("  -x EXT         clean up files if they have this extension\n"));
270 	printf(_("  -?, --help     show this help, then exit\n"));
271 	printf(_("\n"
272 			 "For use as archive_cleanup_command in recovery.conf when standby_mode = on:\n"
273 			 "  archive_cleanup_command = 'pg_archivecleanup [OPTION]... ARCHIVELOCATION %%r'\n"
274 			 "e.g.\n"
275 			 "  archive_cleanup_command = 'pg_archivecleanup /mnt/server/archiverdir %%r'\n"));
276 	printf(_("\n"
277 			 "Or for use as a standalone archive cleaner:\n"
278 			 "e.g.\n"
279 			 "  pg_archivecleanup /mnt/server/archiverdir 000000010000000000000010.00000020.backup\n"));
280 	printf(_("\nReport bugs to <pgsql-bugs@postgresql.org>.\n"));
281 }
282 
283 /*------------ MAIN ----------------------------------------*/
284 int
main(int argc,char ** argv)285 main(int argc, char **argv)
286 {
287 	int			c;
288 
289 	set_pglocale_pgservice(argv[0], PG_TEXTDOMAIN("pg_archivecleanup"));
290 	progname = get_progname(argv[0]);
291 
292 	if (argc > 1)
293 	{
294 		if (strcmp(argv[1], "--help") == 0 || strcmp(argv[1], "-?") == 0)
295 		{
296 			usage();
297 			exit(0);
298 		}
299 		if (strcmp(argv[1], "--version") == 0 || strcmp(argv[1], "-V") == 0)
300 		{
301 			puts("pg_archivecleanup (PostgreSQL) " PG_VERSION);
302 			exit(0);
303 		}
304 	}
305 
306 	while ((c = getopt(argc, argv, "x:dn")) != -1)
307 	{
308 		switch (c)
309 		{
310 			case 'd':			/* Debug mode */
311 				debug = true;
312 				break;
313 			case 'n':			/* Dry-Run mode */
314 				dryrun = true;
315 				break;
316 			case 'x':
317 				additional_ext = pg_strdup(optarg); /* Extension to remove
318 													 * from xlogfile names */
319 				break;
320 			default:
321 				fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname);
322 				exit(2);
323 				break;
324 		}
325 	}
326 
327 	/*
328 	 * We will go to the archiveLocation to check restartWALFileName.
329 	 * restartWALFileName may not exist anymore, which would not be an error,
330 	 * so we separate the archiveLocation and restartWALFileName so we can
331 	 * check separately whether archiveLocation exists, if not that is an
332 	 * error
333 	 */
334 	if (optind < argc)
335 	{
336 		archiveLocation = argv[optind];
337 		optind++;
338 	}
339 	else
340 	{
341 		fprintf(stderr, _("%s: must specify archive location\n"), progname);
342 		fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname);
343 		exit(2);
344 	}
345 
346 	if (optind < argc)
347 	{
348 		restartWALFileName = argv[optind];
349 		optind++;
350 	}
351 	else
352 	{
353 		fprintf(stderr, _("%s: must specify oldest kept WAL file\n"), progname);
354 		fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname);
355 		exit(2);
356 	}
357 
358 	if (optind < argc)
359 	{
360 		fprintf(stderr, _("%s: too many command-line arguments\n"), progname);
361 		fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname);
362 		exit(2);
363 	}
364 
365 	/*
366 	 * Check archive exists and other initialization if required.
367 	 */
368 	Initialize();
369 
370 	/*
371 	 * Check filename is a valid name, then process to find cut-off
372 	 */
373 	SetWALFileNameForCleanup();
374 
375 	if (debug)
376 	{
377 		snprintf(WALFilePath, MAXPGPATH, "%s/%s",
378 				 archiveLocation, exclusiveCleanupFileName);
379 		fprintf(stderr, _("%s: keeping WAL file \"%s\" and later\n"),
380 				progname, WALFilePath);
381 	}
382 
383 	/*
384 	 * Remove WAL files older than cut-off
385 	 */
386 	CleanupPriorWALFiles();
387 
388 	exit(0);
389 }
390