15

Is there a way I can regularly backup one bucket into another one so that in case files get deleted from one bucket, they can always be recovered from another?

2 Answers 2

21

There are several options you could consider for this.

Firstly, you can create a transfer job using the Storage Transfer Service ( navigate to 'Storage' > 'Transfer'). This can be configured to automatically backup data from one bucket to another (you also have the option to configure this to backup AWS buckets to Google Cloud Storage). Transfer is a fairly flexible tool and amongst other things, allows you to define files to transfer based on file prefix, modified times or target specific object URLs.

Another option would be to use the gsutil command to copy or sync files from one bucket to another. If you wanted to automate this process, you could add the command as a cronjob on an instance and run it at your chosen times/intervals.

For example, to copy everything in a source bucket to a destination bucket, you could use a command similar to this:

$ gsutil cp -r gs://SOURCE_BUCKET_NAME/* gs://DESTINATION_BUCKET_NAME/

Alternatively you could use gsutil rsync with the -r switch to synchronise the contents of a source bucket with a destination bucket. For example:

$ gsutil rsync -r gs://SOURCE_BUCKET_NAME gs://DESTINATION_BUCKET_NAME/

If you are concerned about deleting files, it's worth looking into Cloud Storage Object Versioning. If this functionality is enabled, if objects in the bucket are overwritten or deleted, an archived version of the original object is created, so that if required at a later date, the original object can be retrieved. This essentially protects an object from accidental deletion.

It's worth noting with Object Versioning that each archived object takes up as much space as the live object version, and you are charged the same amount for archived storage as for live storage. The archived objects can be managed (for example, automatically deleted when they reach a certain age) by utilising Object Lifecyle Management.

2

You can set up a cron job for auto backup with Cloud Scheduler and Cloud Functions.

With Node.js, you can get a list of file names like this code:

const [sourceFiles] = await sourceBucket.getFiles({
    prefix: 'data/'
});

And then copy the files to another bucket like this code:

let promises = [];
for (let fileName of sourceFileNames) {
  const copyFilePromise = sourceBucket.file(fileName).copy(destBucket.file(`${dateStr}/${fileName}`));
  promises.push(copyFilePromise);
}
await Promise.all(promises);
2
  • This does not copy the metadata associated with objects in the bucket I think.
    – HixField
    Feb 18, 2022 at 20:17
  • Using cloud functions as a backup tool does not work if you have gigabytes of data (and tens of thousands of files) to back up. The cloud function goes to timeout.
    – Timo77
    Oct 11, 2022 at 5:55

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .