Wednesday, June 19, 2013

CRM 2011 Database Backup

How to easily automate CRM database backup?
You can do it with help of SQL command or own console app.
You’ll need to backup the following CRM databases:

  • MSCRM_CONFIG
  • *_MSCRM

SQL command to backup a database is:

backup database <database name> to disk ='C:\Temp\backups\sql\<database name_timestamp>.bak'
      with format, name ='Full backup of <database name>'

To backup the database using a console program - use the code below:

using (SqlConnection conn = new SqlConnection(this.ConnectionString))
{
    string path = Path.Combine("C:\Temp\backups\sql\", string.Format("{0}_{1:yyyyMMdd_HHmmss}.bak",this.DatabaseName, DateTime.Now));

    SqlCommand cmd = new SqlCommand(
        string.Format(@"backup database {0} to disk ='{1}' with format, name ='Full backup of {0}'",
            this.DatabaseName, path), conn);

    cmd.CommandTimeout = 120;
    conn.Open();
    cmd.ExecuteNonQuery();
    conn.Close();
}
Done ;)

No comments:

Post a Comment