public void TestBackupCallbacks()
        {
            testName = "TestBackupCallbacks";
            SetUpTest(true);
            string dbFileName = testName + ".db";

            // Open an environment.
            DatabaseEnvironmentConfig envConfig =
                new DatabaseEnvironmentConfig();
            envConfig.AutoCommit = true;
            envConfig.Create = true;
            envConfig.UseMPool = true;
            envConfig.UseLogging = true;
            envConfig.UseTxns = true;
            DatabaseEnvironment env = DatabaseEnvironment.Open(
                testHome, envConfig);

            // Open a databases.
            BTreeDatabaseConfig dbConfig =
                new BTreeDatabaseConfig();
            dbConfig.Creation = CreatePolicy.IF_NEEDED;
            dbConfig.Env = env;
            BTreeDatabase db = BTreeDatabase.Open(
                dbFileName, dbConfig);

            /*
             * Populate the database but keep it small, we're going to do a
             * byte-by-byte comparison at the end.
             */
            byte[] byteArr = new byte[1024];
            for (int i = 0; i < 10; i++)
                db.Put(new DatabaseEntry(
                    BitConverter.GetBytes(i)),
                    new DatabaseEntry(byteArr));

            db.Close();

            /* Set up the target directory for the backup. */
            string target = testFixtureHome + "/" + testName + "_backup";
            Configuration.ClearDir(target);

            BackupWriter backup = new BackupWriter();
            env.BackupHandler = backup;

            BackupOptions opt = new BackupOptions();
            opt.Creation = CreatePolicy.ALWAYS;
            env.Backup(target, opt);
            Assert.Greater(Directory.GetFiles(target).Length, 0);

            /* Check that our callbacks wrote the file correctly. */
            FileStream orig = new FileStream(Path.Combine(testHome, dbFileName), FileMode.Open, FileAccess.Read);
            FileStream bak = new FileStream(Path.Combine(target, dbFileName), FileMode.Open, FileAccess.Read);
            int orig_byte, bak_byte;
            do {
                orig_byte = orig.ReadByte();
                bak_byte = bak.ReadByte();
                Assert.AreEqual(orig_byte, bak_byte);
            } while (orig_byte != -1 && bak_byte != -1);
            orig.Close();
            bak.Close();
            Directory.Delete(target, true);
            env.Close();
        }
Ejemplo n.º 2
0
 /// <summary>
 /// Perform a hot back up of the open environment.
 /// <para>
 /// All files used by the environment are backed up, so long as the 
 /// normal rules for file placement are followed. For information on how
 /// files are normally placed relative to the environment directory, see
 /// the "Berkeley DB File Naming" section in the Berkeley DB Reference 
 /// Guide.
 /// </para>
 /// <para>
 /// By default, data directories and the log directory specified 
 /// relative to the home directory are recreated relative to the 
 /// target directory. If absolute path names are used, then use the 
 /// <see cref="BackupOptions.SingleDir"/> method.
 /// </para>
 /// <para>
 /// This method provides the same functionality as the db_hotbackup
 /// utility.  However, this method does not perform the housekeeping
 /// actions performed by that utility. In particular, you may want to
 /// run a checkpoint before calling this method. To run a checkpoint, 
 /// use the <see cref="DatabaseEnvironment.Checkpoint"/> method. For 
 /// more information on checkpoints, see the "Checkpoint" section in the
 /// Berkeley DB Reference Guide.
 /// </para>
 /// <para>
 /// To back up a single database file within the environment, use the
 /// <see cref="DatabaseEnvironment.BackupDatabase"/> method.
 /// </para>
 /// <para>
 /// In addition to the configuration options available using the 
 /// <see cref="BackupOptions"/> class, additional tuning modifications 
 /// can be made using the <see cref="DatabaseEnvironment.BackupReadCount"/>,
 /// <see cref="DatabaseEnvironment.BackupReadSleepDuration"/>,
 /// <see cref="DatabaseEnvironment.BackupBufferSize"/>, and
 /// <see cref="DatabaseEnvironment.BackupWriteDirect"/> properties. 
 /// Alternatively, you can write your own custom hot back up facility 
 /// using the <see cref="IBackup"/> interface.
 /// </para>
 /// </summary>
 /// <param name="target">Identifies the directory in which the back up 
 /// is placed. Any subdirectories required to contain the back up
 /// must be placed relative to this directory. If an 
 /// <see cref="IBackup"/> is configured for the environment, then the
 /// value specified to this parameter is passed on to the 
 /// <see cref="IBackup.Open"/> method.  If this parameter is null, then
 /// the target must be specified to the <see cref="IBackup.Open"/>
 /// method.
 /// <para>
 /// This directory, and any required subdirectories, are created for
 /// you if you specify <see cref="CreatePolicy.IF_NEEDED"/> or 
 /// <see cref="CreatePolicy.ALWAYS"/> for the 
 /// <see cref="BackupOptions.Creation"/> property.
 /// </para>
 /// </param>
 /// <param name="opt">The <see cref="BackupOptions"/> instance used to
 /// configure the hot back up.</param>
 public void Backup(string target, BackupOptions opt)
 {
     dbenv.backup(target, opt.flags);
 }
        public void TestBackup()
        {
            testName = "TestBackup";
            SetUpTest(true);
            string dbFileName = testName + ".db";

            // Open an environment.
            DatabaseEnvironmentConfig envConfig =
                new DatabaseEnvironmentConfig();
            envConfig.AutoCommit = true;
            envConfig.Create = true;
            envConfig.UseMPool = true;
            envConfig.UseLogging = true;
            envConfig.UseTxns = true;
            DatabaseEnvironment env = DatabaseEnvironment.Open(
                testHome, envConfig);

            // Open a databases.
            BTreeDatabaseConfig dbConfig =
                new BTreeDatabaseConfig();
            dbConfig.Creation = CreatePolicy.IF_NEEDED;
            dbConfig.Env = env;
            BTreeDatabase db = BTreeDatabase.Open(
                dbFileName, dbConfig);

            /*
             * Put 1000 records into the database to generate
             * more than one log file.
             */
            byte[] byteArr = new byte[1024];
            for (int i = 0; i < 1000; i++)
                db.Put(new DatabaseEntry(
                    BitConverter.GetBytes(i)),
                    new DatabaseEntry(byteArr));

            db.Close();

            /* Set up the target directory for the backup. */
            string target = testFixtureHome + "/" + testName + "_backup";
            Configuration.ClearDir(target);

            BackupOptions opt = new BackupOptions();
            opt.Creation = CreatePolicy.ALWAYS;
            env.Backup(target, opt);
            Assert.Greater(Directory.GetFiles(target).Length, 0);
            Directory.Delete(target, true);
            env.Close();
        }