3535
3636/* Global options */
3737char * basedir = NULL ;
38+ static char * xlog_dir = "" ;
3839char format = 'p' ;/* p(lain)/t(ar) */
3940char * label = "pg_basebackup base backup" ;
4041bool showprogress = false;
@@ -115,6 +116,7 @@ usage(void)
115116printf (_ (" -x, --xlog include required WAL files in backup (fetch mode)\n" ));
116117printf (_ (" -X, --xlog-method=fetch|stream\n"
117118" include required WAL files with specified method\n" ));
119+ printf (_ (" --xlogdir=XLOGDIR location for the transaction log directory\n" ));
118120printf (_ (" -z, --gzip compress tar output\n" ));
119121printf (_ (" -Z, --compress=0-9 compress tar output with given compression level\n" ));
120122printf (_ ("\nGeneral options:\n" ));
@@ -980,10 +982,14 @@ ReceiveAndUnpackTarFile(PGconn *conn, PGresult *res, int rownum)
980982{
981983/*
982984 * When streaming WAL, pg_xlog will have been created
983- * by the wal receiver process, so just ignore failure
984- * on that.
985+ * by the wal receiver process. Also, when transaction
986+ * log directory location was specified, pg_xlog has
987+ * already been created as a symbolic link before
988+ * starting the actual backup. So just ignore failure
989+ * on them.
985990 */
986- if (!streamwal || strcmp (filename + strlen (filename )- 8 ,"/pg_xlog" )!= 0 )
991+ if ((!streamwal && (strcmp (xlog_dir ,"" )== 0 ))
992+ || strcmp (filename + strlen (filename )- 8 ,"/pg_xlog" )!= 0 )
987993{
988994fprintf (stderr ,
989995_ ("%s: could not create directory \"%s\": %s\n" ),
@@ -1666,6 +1672,7 @@ main(int argc, char **argv)
16661672{"status-interval" ,required_argument ,NULL ,'s' },
16671673{"verbose" ,no_argument ,NULL ,'v' },
16681674{"progress" ,no_argument ,NULL ,'P' },
1675+ {"xlogdir" ,required_argument ,NULL ,1 },
16691676{NULL ,0 ,NULL ,0 }
16701677};
16711678int c ;
@@ -1750,6 +1757,9 @@ main(int argc, char **argv)
17501757exit (1 );
17511758}
17521759break ;
1760+ case 1 :
1761+ xlog_dir = pg_strdup (optarg );
1762+ break ;
17531763case 'l' :
17541764label = pg_strdup (optarg );
17551765break ;
@@ -1872,6 +1882,30 @@ main(int argc, char **argv)
18721882exit (1 );
18731883}
18741884
1885+ if (strcmp (xlog_dir ,"" )!= 0 )
1886+ {
1887+ if (format != 'p' )
1888+ {
1889+ fprintf (stderr ,
1890+ _ ("%s: transaction log directory location can only be specified in plain mode\n" ),
1891+ progname );
1892+ fprintf (stderr ,_ ("Try \"%s --help\" for more information.\n" ),
1893+ progname );
1894+ exit (1 );
1895+ }
1896+
1897+ /* clean up xlog directory name, check it's absolute */
1898+ canonicalize_path (xlog_dir );
1899+ if (!is_absolute_path (xlog_dir ))
1900+ {
1901+ fprintf (stderr ,_ ("%s: transaction log directory location must be "
1902+ "an absolute path\n" ),progname );
1903+ fprintf (stderr ,_ ("Try \"%s --help\" for more information.\n" ),
1904+ progname );
1905+ exit (1 );
1906+ }
1907+ }
1908+
18751909#ifndef HAVE_LIBZ
18761910if (compresslevel != 0 )
18771911{
@@ -1890,6 +1924,30 @@ main(int argc, char **argv)
18901924if (format == 'p' || strcmp (basedir ,"-" )!= 0 )
18911925verify_dir_is_empty_or_create (basedir );
18921926
1927+ /* Create transaction log symlink, if required */
1928+ if (strcmp (xlog_dir ,"" )!= 0 )
1929+ {
1930+ char * linkloc ;
1931+
1932+ verify_dir_is_empty_or_create (xlog_dir );
1933+
1934+ /* form name of the place where the symlink must go */
1935+ linkloc = psprintf ("%s/pg_xlog" ,basedir );
1936+
1937+ #ifdef HAVE_SYMLINK
1938+ if (symlink (xlog_dir ,linkloc )!= 0 )
1939+ {
1940+ fprintf (stderr ,_ ("%s: could not create symbolic link \"%s\": %s\n" ),
1941+ progname ,linkloc ,strerror (errno ));
1942+ exit (1 );
1943+ }
1944+ #else
1945+ fprintf (stderr ,_ ("%s: symlinks are not supported on this platform" ));
1946+ exit (1 );
1947+ #endif
1948+ free (linkloc );
1949+ }
1950+
18931951BaseBackup ();
18941952
18951953return 0 ;