コード例 #1
0
        public static string AppendSqlBackupBinaryChunk(int itemId, string fileName, string path, byte[] chunk)
        {
            // load original meta item
            SqlDatabase item = (SqlDatabase)PackageController.GetPackageItem(itemId);

            if (item == null)
            {
                return(null);
            }

            DatabaseServer sql = GetDatabaseServer(item.ServiceId);

            return(sql.AppendTempFileBinaryChunk(fileName, path, chunk));
        }
コード例 #2
0
        public static int RestoreSqlDatabase(int itemId, string[] uploadedFiles, string[] packageFiles)
        {
            // check account
            int accountCheck = SecurityContext.CheckAccount(DemandAccount.NotDemo | DemandAccount.IsActive);

            if (accountCheck < 0)
            {
                return(accountCheck);
            }

            // load original meta item
            SqlDatabase item = (SqlDatabase)PackageController.GetPackageItem(itemId);

            if (item == null)
            {
                return(BusinessErrorCodes.ERROR_MSSQL_DATABASES_PACKAGE_ITEM_NOT_FOUND);
            }

            // check package
            int packageCheck = SecurityContext.CheckPackage(item.PackageId, DemandPackage.IsActive);

            if (packageCheck < 0)
            {
                return(packageCheck);
            }

            // place log record
            TaskManager.StartTask("SQL_DATABASE", "RESTORE", item.Name);
            TaskManager.ItemId = itemId;

            try
            {
                DatabaseServer sql = GetDatabaseServer(item.ServiceId);

                List <string> backupFiles = new List <string>();
                if (packageFiles != null && packageFiles.Length > 0)
                {
                    // copy package files to the remote SQL Server
                    foreach (string packageFile in packageFiles)
                    {
                        string path   = null;
                        byte[] buffer = null;

                        int offset = 0;
                        do
                        {
                            // read package file
                            buffer = FilesController.GetFileBinaryChunk(item.PackageId, packageFile, offset, FILE_BUFFER_LENGTH);

                            // write remote backup file
                            string tempPath = sql.AppendTempFileBinaryChunk(Path.GetFileName(packageFile), path, buffer);
                            if (path == null)
                            {
                                path = tempPath;
                                backupFiles.Add(path);
                            }

                            offset += FILE_BUFFER_LENGTH;
                        }while (buffer.Length == FILE_BUFFER_LENGTH);
                    }
                }
                else if (uploadedFiles != null && uploadedFiles.Length > 0)
                {
                    // upladed files
                    backupFiles.AddRange(uploadedFiles);
                }

                // restore
                if (backupFiles.Count > 0)
                {
                    sql.RestoreDatabase(item.Name, backupFiles.ToArray());
                }

                return(0);
            }
            catch (Exception ex)
            {
                throw TaskManager.WriteError(ex);
            }
            finally
            {
                TaskManager.CompleteTask();
            }
        }
コード例 #3
0
        public int RestoreItem(string tempFolder, XmlNode itemNode, int itemId, Type itemType, string itemName, int packageId, int serviceId, ResourceGroupInfo group)
        {
            if (itemType == typeof(SqlDatabase))
            {
                DatabaseServer sql = GetDatabaseServer(serviceId);

                // extract meta item
                XmlSerializer serializer = new XmlSerializer(typeof(SqlDatabase));
                SqlDatabase   db         = (SqlDatabase)serializer.Deserialize(
                    new XmlNodeReader(itemNode.SelectSingleNode("SqlDatabase")));

                // create database if required
                if (!sql.DatabaseExists(itemName))
                {
                    sql.CreateDatabase(db);
                }

                // copy database backup to remote server
                XmlNode fileNode            = itemNode.SelectSingleNode("File[@name='DatabaseBackup']");
                string  backupFileName      = fileNode.Attributes["path"].Value;
                long    backupFileLength    = Int64.Parse(fileNode.Attributes["size"].Value);
                string  localBackupFilePath = Path.Combine(tempFolder, backupFileName);

                if (new FileInfo(localBackupFilePath).Length != backupFileLength)
                {
                    return(-3);
                }

                FileStream stream = new FileStream(localBackupFilePath, FileMode.Open, FileAccess.Read);
                byte[]     buffer = new byte[FILE_BUFFER_LENGTH];

                int    readBytes        = 0;
                long   length           = 0;
                string remoteBackupPath = null;
                do
                {
                    // read package file
                    readBytes = stream.Read(buffer, 0, FILE_BUFFER_LENGTH);
                    length   += readBytes;

                    if (readBytes < FILE_BUFFER_LENGTH)
                    {
                        // resize buffer
                        Array.Resize <byte>(ref buffer, readBytes);
                    }

                    // write remote backup file
                    string tempPath = sql.AppendTempFileBinaryChunk(backupFileName, remoteBackupPath, buffer);
                    if (remoteBackupPath == null)
                    {
                        remoteBackupPath = tempPath;
                    }
                }while (readBytes == FILE_BUFFER_LENGTH);
                stream.Close();

                // restore database
                sql.RestoreDatabase(itemName, new string[] { remoteBackupPath });

                // add meta-item if required
                if (PackageController.GetPackageItemByName(packageId, group.GroupName,
                                                           itemName, typeof(SqlDatabase)) == null)
                {
                    db.PackageId = packageId;
                    db.ServiceId = serviceId;
                    db.GroupName = group.GroupName;
                    PackageController.AddPackageItem(db);
                }
            }
            else if (itemType == typeof(SqlUser))
            {
                DatabaseServer sql = GetDatabaseServer(serviceId);

                // extract meta item
                XmlSerializer serializer = new XmlSerializer(typeof(SqlUser));
                SqlUser       user       = (SqlUser)serializer.Deserialize(
                    new XmlNodeReader(itemNode.SelectSingleNode("SqlUser")));

                // create user if required
                if (!sql.UserExists(itemName))
                {
                    sql.CreateUser(user, CryptoUtils.Decrypt(user.Password));
                }

                // add meta-item if required
                if (PackageController.GetPackageItemByName(packageId, group.GroupName,
                                                           itemName, typeof(SqlUser)) == null)
                {
                    user.PackageId = packageId;
                    user.ServiceId = serviceId;
                    user.GroupName = group.GroupName;
                    PackageController.AddPackageItem(user);
                }
            }

            return(0);
        }