public int BackupItem(string tempFolder, XmlWriter writer, ServiceProviderItem item, ResourceGroupInfo group)
        {
            if (item is SqlDatabase)
            {
                // backup database
                DatabaseServer sql = GetDatabaseServer(item.ServiceId);

                string backupName       = String.Format("DatabaseBackup_{0}.zip", item.Id);
                string remoteBackupFile = sql.BackupDatabase(item.Name, backupName, true);

                // download remote backup
                string localBackupPath = Path.Combine(tempFolder, backupName);

                byte[]     buffer = null;
                FileStream stream = new FileStream(localBackupPath, FileMode.Create, FileAccess.Write);

                int  offset = 0;
                long length = 0;
                do
                {
                    // read remote content
                    buffer = sql.GetTempFileBinaryChunk(remoteBackupFile, offset, FILE_BUFFER_LENGTH);

                    // write remote content
                    stream.Write(buffer, 0, buffer.Length);

                    length += buffer.Length;
                    offset += FILE_BUFFER_LENGTH;
                }while (buffer.Length == FILE_BUFFER_LENGTH);
                stream.Close();

                // add file pointer

                BackupController.WriteFileElement(writer, "DatabaseBackup", backupName, length);

                // store meta item
                SqlDatabase   database   = sql.GetDatabase(item.Name);
                XmlSerializer serializer = new XmlSerializer(typeof(SqlDatabase));
                serializer.Serialize(writer, database);
            }
            else if (item is SqlUser)
            {
                // backup user
                DatabaseServer sql = GetDatabaseServer(item.ServiceId);

                SqlUser userItem = item as SqlUser;

                // store user info
                SqlUser user = sql.GetUser(item.Name, GetSqlDatabasesArray(item.PackageId, item.GroupName));
                user.Password = userItem.Password;

                XmlSerializer serializer = new XmlSerializer(typeof(SqlUser));
                serializer.Serialize(writer, user);
            }

            return(0);
        }
        public static string BackupSqlDatabase(int itemId, string backupName,
                                               bool zipBackup, bool download, string folderName)
        {
            // check account
            int accountCheck = SecurityContext.CheckAccount(DemandAccount.NotDemo);

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

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

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

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

            try
            {
                DatabaseServer sql      = GetDatabaseServer(item.ServiceId);
                string         backFile = sql.BackupDatabase(item.Name, backupName, zipBackup);

                if (!download)
                {
                    // copy backup files to space folder
                    string relFolderName = FilesController.CorrectRelativePath(folderName);
                    if (!relFolderName.EndsWith("\\"))
                    {
                        relFolderName = relFolderName + "\\";
                    }

                    // create backup folder if not exists
                    if (!FilesController.DirectoryExists(item.PackageId, relFolderName))
                    {
                        FilesController.CreateFolder(item.PackageId, relFolderName);
                    }

                    string packageFile = relFolderName + Path.GetFileName(backFile);

                    // delete destination file if exists
                    if (FilesController.FileExists(item.PackageId, packageFile))
                    {
                        FilesController.DeleteFiles(item.PackageId, new string[] { packageFile });
                    }

                    byte[] buffer = null;

                    int offset = 0;
                    do
                    {
                        // read remote content
                        buffer = sql.GetTempFileBinaryChunk(backFile, offset, FILE_BUFFER_LENGTH);

                        // write remote content
                        FilesController.AppendFileBinaryChunk(item.PackageId, packageFile, buffer);

                        offset += FILE_BUFFER_LENGTH;
                    }while (buffer.Length == FILE_BUFFER_LENGTH);
                }

                return(backFile);
            }
            catch (Exception ex)
            {
                throw TaskManager.WriteError(ex);
            }
            finally
            {
                TaskManager.CompleteTask();
            }
        }