Advertisement
Guest User

Untitled

a guest
Aug 17th, 2017
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.96 KB | None | 0 0
  1. /*
  2. package.json
  3. {
  4. "name": "sample-cloud-storage",
  5. "version": "0.0.1",
  6. "dependencies": {
  7. "@google-cloud/storage": "1.2.1"
  8. }
  9. }
  10.  
  11.  
  12. /**
  13. * Triggered from a message on a Cloud Storage bucket.
  14. *
  15. * @param {!Object} event The Cloud Functions event.
  16. * @param {!Function} The callback function.
  17. */
  18. const Storage = require('@google-cloud/storage');
  19.  
  20. exports.backupTerraformState = function(event, callback) {
  21. const storage = Storage();
  22. const file = event.data;
  23. console.log(file.name);
  24.  
  25. var d = new Date();
  26. var patt = /^[a-zA-Z_-]*terraform\.tfstat$/;
  27.  
  28. if (file.name.str.match(patt)) {
  29. storage
  30. .bucket(file.bucket)
  31. .file(file.name)
  32. .copy(storage.bucket("my-terraform-bucket").file("backup/" + file.name+ "-" + Math.round(Date.now() / 1000) ))
  33. .then(() => {
  34. console.log("copied");
  35. })
  36. .catch((err) => {
  37. console.error('ERROR:', err);
  38. });
  39. } else {
  40. console.log("not match:[" + file.name + "]");
  41.  
  42. }
  43.  
  44. callback();
  45. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement