Example #1
0
        /// <summary>
        /// Gets the info.
        /// </summary>
        /// <param name="uidStream">The uid stream.</param>
        /// <returns></returns>
        public override BlobInfo GetInfo(Guid uidStream)
        {
            BlobInfo info;

            try
            {
                GetObjectRequest request = new GetObjectRequest(_Service, this.BucketName, uidStream.ToString(), true);
                using (GetObjectResponse response = request.GetResponse())
                {
                    info = this.CreateBlobInfo(response, uidStream);
                }
            }
            catch (S3Exception exception)
            {
                if (exception.ErrorCode == S3ErrorCode.NoSuchKey)
                {
                    return(null);
                }

                throw;
            }
            catch (Exception exception)
            {
                Trace.WriteLine(exception);
                throw;
            }
            return(info);
        }
Example #2
0
        /// <summary>
        /// Reads the stream.
        /// </summary>
        /// <param name="blobInfo">The BLOB info.</param>
        /// <returns></returns>
        public override System.IO.Stream ReadStream(BlobInfo blobInfo)
        {
            Stream stream = null;

            try
            {
                BlobStorage.RaiseReadingEvent(blobInfo);
                GetObjectRequest  request  = new GetObjectRequest(_Service, this.BucketName, blobInfo.Uid.ToString(), false);
                GetObjectResponse response = request.GetResponse();
                stream = response.GetResponseStream();
                BlobStorage.RaiseReadedEvent(blobInfo);
            }
            catch (Exception exception)
            {
                Trace.WriteLine(exception);
                throw;
            }
            return(stream);
        }
Example #3
0
        /// <summary>
        /// Returns true if the given object exists in the given bucket.
        /// </summary>
        public bool ObjectExists(string bucketName, string key)
        {
            var request = new GetObjectRequest(this, bucketName, key, true);

            // This is the recommended method from the S3 API docs.
            try
            {
                using (GetObjectResponse response = request.GetResponse())
                    return true;
            }
            catch (WebException exception)
            {
                var response = exception.Response as HttpWebResponse;
                if (response != null && response.StatusCode == HttpStatusCode.NotFound)
                    return false;
                else
                    throw;
            }
        }
Example #4
0
 /// <summary>
 /// Gets a data stream for an existing object in S3. It is your responsibility to close
 /// the Stream when you are finished.
 /// </summary>
 public Stream GetObjectStream(string bucketName, string key,
     out long contentLength, out string contentType)
 {
     var request = new GetObjectRequest(this, bucketName, key);
     GetObjectResponse response = request.GetResponse();
     contentLength = response.ContentLength;
     contentType = response.ContentType;
     return response.GetResponseStream();
 }
Example #5
0
        public Program()
        {
            var usingTrustedConnection = string.IsNullOrEmpty(Username) && string.IsNullOrEmpty(Password);

            var sourceConnection = usingTrustedConnection
                ? new ServerConnection(ServerName)
            {
                LoginSecure = true
            }
                : new ServerConnection(ServerName, Username, Password);

            var sqlServer = new Server(sourceConnection);

            if (sqlServer != null)
            {
                var backup = new Backup();
                var dbc    = sqlServer.Databases;

                if (dbc.Contains(DatabaseName))
                {
                    backup.Action = BackupActionType.Database;

                    backup.Database = DatabaseName;

                    var dateFilename   = DateTime.UtcNow.ToString("dd-MMM-yyyy");
                    var tempFilename   = String.Format("{0}-{1}.bak", DatabaseName, dateFilename);
                    var tempBackupPath = String.Format("{0}{1}", TempFilePath, tempFilename);

                    //remove old backups from this local temp location
                    foreach (var file in Directory.GetFiles(TempFilePath))
                    {
                        if (file != tempBackupPath)
                        {
                            Console.WriteLine("Removing previous temp backup " + file);
                            File.Delete(file);
                        }
                    }

                    try
                    {
                        var backupDevice = new BackupDeviceItem(tempBackupPath, DeviceType.File);
                        backup.Devices.Add(backupDevice);
                        backup.Checksum           = true;
                        backup.ContinueAfterError = false;
                        backup.LogTruncation      = BackupTruncateLogType.Truncate;

                        //if file exists then do an incremental, otherwise do a full
                        if (File.Exists(tempBackupPath))
                        {
                            backup.Incremental = true;
                        }
                        else
                        {
                            backup.Incremental = false;
                        }

                        // Perform backup.
                        backup.SqlBackup(sqlServer);

                        //now move the backup to S3 - overwriting anything that is there with the same name
                        var s3 = new S3Service
                        {
                            AccessKeyID     = AccessKeyID,
                            SecretAccessKey = SecretAccessKey
                        };

                        var bucket = Bucket;
                        s3.AddObject(tempBackupPath, bucket, tempFilename);

                        var metadataOnly = true;

                        foreach (var listEntry in s3.ListObjects(Bucket, ""))
                        {
                            var request = new GetObjectRequest(s3, Bucket, listEntry.Name, metadataOnly);

                            using (var response = request.GetResponse())
                            {
                                if (response.LastModified < DateTime.UtcNow.AddDays(DaysToKeepS3BackupFor * -1))
                                {
                                    Console.WriteLine("Going to delete old archive " + listEntry.Name);
                                    s3.DeleteObject(Bucket, listEntry.Name);
                                }
                            }
                        }
                        Console.Out.WriteLine("Backup to S3 is complete");
                        System.Threading.Thread.Sleep(10000);
                    }
                    catch (Exception ee)
                    {
                        Console.Out.WriteLine("Exception occurred - do not continue.  Wait until next run to try again " + ee.ToString());
                        System.Threading.Thread.Sleep(10000);
                    }
                }
            }
        }
        public Program()
        {
            var usingTrustedConnection = string.IsNullOrEmpty(Username) && string.IsNullOrEmpty(Password);

            var sourceConnection = usingTrustedConnection
                ? new ServerConnection(ServerName) { LoginSecure = true }
                : new ServerConnection(ServerName, Username, Password);

            var sqlServer = new Server(sourceConnection);

            if(sqlServer != null){

                var backup = new Backup();
                var dbc = sqlServer.Databases;

                if (dbc.Contains(DatabaseName))
                {
                    backup.Action = BackupActionType.Database;

                    backup.Database = DatabaseName;

                    var dateFilename = DateTime.UtcNow.ToString("dd-MMM-yyyy");
                    var tempFilename = String.Format("{0}-{1}.bak", DatabaseName, dateFilename);
                    var tempBackupPath = String.Format("{0}{1}", TempFilePath, tempFilename);

                    //remove old backups from this local temp location
                    foreach (var file in Directory.GetFiles(TempFilePath))
                    {
                        if (file != tempBackupPath)
                        {
                            Console.WriteLine("Removing previous temp backup " + file);
                            File.Delete(file);
                        }
                    }

                    try
                    {
                        var backupDevice = new BackupDeviceItem(tempBackupPath, DeviceType.File);
                        backup.Devices.Add(backupDevice);
                        backup.Checksum = true;
                        backup.ContinueAfterError = false;
                        backup.LogTruncation = BackupTruncateLogType.Truncate;

                        //if file exists then do an incremental, otherwise do a full
                        if (File.Exists(tempBackupPath))
                        {
                            backup.Incremental = true;
                        }
                        else
                        {
                            backup.Incremental = false;
                        }

                        // Perform backup.
                        backup.SqlBackup(sqlServer);

                        //now move the backup to S3 - overwriting anything that is there with the same name
                        var s3 = new S3Service
                                     {
                                         AccessKeyID = AccessKeyID,
                                         SecretAccessKey = SecretAccessKey
                                     };

                        var bucket = Bucket;
                        s3.AddObject(tempBackupPath, bucket, tempFilename);

                        var metadataOnly = true;

                        foreach(var listEntry in s3.ListObjects(Bucket,""))
                        {
                            var request = new GetObjectRequest(s3, Bucket, listEntry.Name, metadataOnly);

                            using (var response = request.GetResponse())
                            {
                                if (response.LastModified < DateTime.UtcNow.AddDays(DaysToKeepS3BackupFor * -1))
                                {
                                    Console.WriteLine("Going to delete old archive " + listEntry.Name);
                                    s3.DeleteObject(Bucket,listEntry.Name);
                                }
                            }
                        }
                        Console.Out.WriteLine("Backup to S3 is complete");
                        System.Threading.Thread.Sleep(10000);
                    }
                    catch(Exception ee)
                    {
                        Console.Out.WriteLine("Exception occurred - do not continue.  Wait until next run to try again "+ee.ToString());
                        System.Threading.Thread.Sleep(10000);
                    }
                }
            }
        }
        /// <summary>
        /// Given an S3 Url, this method will return the last modified date or the file, or
        /// null if it doesn't exist.
        /// </summary>
        /// <returns>The last modified date</returns>
        /// <param name="fullS3Url">Full s3 URL.</param>
        public DateTime? LastModified(string fullS3Url)
        {
            var s3Service = Utilities.GetS3Service ();
            var parts = S3Parts.FromUrl (fullS3Url);

            if (!Exists (fullS3Url)) {
                return null;
            }

            var req = new GetObjectRequest (s3Service, parts.Bucket, parts.File, true);
            try {
                using (var response = req.GetResponse ()) {
                    return response.LastModified;
                }
            } catch (Exception ex) {
                log.DebugFormat (
                    "LastModified - error when querying file {0}, err = {1}, " +
                    "returning null",
                    fullS3Url, ex.Message);

                return null;
            }
        }