Backing up Google Cloud Storage Bucket
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.
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);
I have also written a blog about how to set up the whole auto backup solution: https://medium.com/@anthonychiu/backup-cloud-storage-data-with-cloud-functions-77ee01f4ec02