3 May 2014

Backup using serialization before deploying packages

Well in previous post, we saw that we could generate the serialization prior installing any packages. Well the underlying goal was to actually backup the content... So let's extend a bit and see if we can do a bit more. What would be nice would be to delete the entire folder prior serialization and zipping the serialization folder once it is completed...

1- Get the serialization folder:
This is quite simple and you should be able to do it with the following code:
            string SerializationRootFolder = FileUtil.MapPath(SC.Configuration.Settings.SerializationFolder);


2- Clear the folder
So let's delete all files and subfolders... Obviously you will only be able to do that if you have the required file permission setup... Check your app pool if using network services... But for this exercies, let's say you have setup the permissions so you are allowed to delete files and folder...
        /// 
        /// Clear Serialisation folder
        /// 
        /// 
        private void ClearSerializationFolder(string SerializationRootFolder)
        {
            System.IO.DirectoryInfo downloadedMessageInfo = new DirectoryInfo(SerializationRootFolder);

            foreach (FileInfo file in downloadedMessageInfo.GetFiles())
            {
                file.Delete();
            }
            foreach (DirectoryInfo dir in downloadedMessageInfo.GetDirectories())
            {
                dir.Delete(true);
            }
        }

3- Start serializing the entire content tree

Well guess what, we do have this code from previous post
        private void BackupItemTree(string id)
        {
            Database db = SC.Configuration.Factory.GetDatabase("master");
            Item itm = db.GetItem(id);

            SC.Data.Serialization.Manager.DumpTree(itm);
        }

4- Zipping it
To zip the files, you can use different libraries available for .NET, but since Sitecore has a Zip library, why not using it...
        /// 
        /// Create the zip file of serialisation
        /// 
        /// 
        /// 
        private string CreateZipFile(string SerializationRootFolder)
        {
            string backupName = string.Format("backup_serialization_{0}.zip", DateTime.Now.ToString("yyyyMMddhhmmss"));

            var zipFile = Path.Combine(SerializationRootFolder, backupName);

            using (var fileWriter = new SC.Zip.ZipWriter(zipFile))
            {
                var files = GetAllFiles(SerializationRootFolder, SearchOption.AllDirectories);

                SC.Diagnostics.Log.Info(string.Format("Adding files ({0})", files.Count()), this);

                var length = SerializationRootFolder.Length;
                if (!SerializationRootFolder.EndsWith("\\"))
                {
                    length += 1;
                }

                foreach (var file in files)
                {
                    fileWriter.AddEntry(file.Remove(0, length), file);
                }
            }

            return zipFile;
        }

        /// 
        /// Get all files - you can include all subdirectories through search options
        /// 
        /// 
        /// 
        /// 
        protected string[] GetAllFiles(string path, SearchOption searchOption)
        {
            List files = new List();

            string searchPattern = "*.item";
            files.AddRange(Directory.GetFiles(path, searchPattern, searchOption));
            
            return files.ToArray();
        } 


OK, time to test that... SO let's install a package and watch the serialization folder:



No comments:

Post a Comment