This tutorial shows you how you can schedule backups for Filestore instances using Cloud Scheduler and Cloud Run functions.

For information on creating backups using a managed service scheduler, see [Filestore Enhanced Backups](https://docs.cloud.google.com/filestore/docs/gcbdr-backups-overview).

> [!NOTE]
> Filestore Enhanced Backups feature is in the Restricted GA launch stage, which means it requires additional access. To request access, contact your Google Cloud account representative. For current backup and restore offering, see [Backups overview](https://docs.cloud.google.com/filestore/docs/backups.md.txt).

## Objectives

- Create a client service account for Cloud Scheduler that has the credentials needed to invoke a Cloud Run functions function.
- Create a client service account for Cloud Run functions that has the credentials to call the Filestore endpoint.
- Create a Cloud Run functions function that creates a backup of a Filestore instance.
- Create a Cloud Run functions function that deletes a backup of a Filestore instance.
- Create a Cloud Scheduler job that runs either function at regular intervals.

## Costs


In this document, you use the following billable components of Google Cloud:


- [Artifact Registry API](https://docs.cloud.google.com/artifact-registry/docs/overview.md.txt)
- [Cloud Build API](https://docs.cloud.google.com/build/docs/overview.md.txt)
- [Filestore API](https://docs.cloud.google.com/filestore/docs/backups.md.txt)
- [Cloud Functions API](https://docs.cloud.google.com/functions/docs/concepts/overview.md.txt)
- [Cloud Logging API](https://docs.cloud.google.com/logging/docs/overview.md.txt)
- [Pub/Sub API](https://docs.cloud.google.com/pubsub/docs/overview.md.txt)
- [Cloud Run Admin API](https://docs.cloud.google.com/run/docs/overview/what-is-cloud-run.md.txt)
- [Cloud Scheduler API](https://docs.cloud.google.com/scheduler/docs/overview.md.txt)


To generate a cost estimate based on your projected usage,
use the [pricing calculator](https://docs.cloud.google.com/products/calculator).
New Google Cloud users might be eligible for a [free trial](https://docs.cloud.google.com/free).

<br />

## Before you begin

1. If you don't have a Filestore instance in your project, you must first [create](https://docs.cloud.google.com/filestore/docs/creating-instances.md.txt) one.

<br />

## Create client service accounts for Cloud Scheduler and Cloud Run functions

1. If you haven't already done so, from the Google Cloud console, click
   **Activate Cloud Shell**.

2. Create a client service account that Cloud Scheduler runs as to
   invoke a Cloud Run functions function. For this example, use the
   [`iam service-accounts create`](https://docs.cloud.google.com/sdk/gcloud/reference/iam/service-accounts/create)
   command to name the account `schedulerunner` and set the display name to
   "Service Account for FS Backups-Scheduler":

   ```bash
   gcloud iam service-accounts create schedulerunner \
       --display-name="Service Account for FS Backups-Scheduler"
   ```
3. Create a client service account that Cloud Run functions runs as to call
   the Filestore endpoint. For this example, we name the account
   `backupagent` and set the display name to "Service Account for FS Backups-GCF":

   ```bash
   gcloud iam service-accounts create backupagent \
       --display-name="Service Account for FS Backups-GCF"
   ```

   You can check whether the service account is created by running the
   [`iam service-accounts list`](https://docs.cloud.google.com/sdk/gcloud/reference/iam/service-accounts/list)
   command:

   ```bash
   gcloud iam service-accounts list
   ```

   The command returns something like this:

   ```bash
   NAME                                         EMAIL                                                   DISABLED
   Service Account for FS Backups-GCF           backupagent@$PROJECT_ID.iam.gserviceaccount.com         False
   Service Account for FS Backups-Scheduler     schedulerunner@$PROJECT_ID.iam.gserviceaccount.com      False
   ```

## Set up environment variables

Set up the following environment variables in your local environment:

- Google Cloud project ID and project:

  ```bash
  export PROJECT_ID=`gcloud config get-value core/project`
  export PROJECT_NUMBER=`gcloud projects describe $PROJECT_ID --format="value(projectNumber)"`
  ```
- The Cloud Scheduler service agent and the client
  [service accounts](https://docs.cloud.google.com/iam/docs/service-accounts)
  for Cloud Scheduler and Cloud Run functions:

  ```bash
  export SCHEDULER_SA=service-$PROJECT_NUMBER@gcp-sa-cloudscheduler.iam.gserviceaccount.com
  export SCHEDULER_CLIENT_SA=schedulerunner@$PROJECT_ID.iam.gserviceaccount.com
  export GCF_CLIENT_SA=backupagent@$PROJECT_ID.iam.gserviceaccount.com
  ```

  > [!NOTE]
  > **Note:** In some cases, you may need to manually add the Cloud Scheduler Service Agent role to your Cloud Scheduler service account. This is necessary **only** if you enabled the Cloud Scheduler API prior to March 19, 2019, or, if you've removed the Cloud Scheduler Service Agent role from your service account. For more information, see [Manually add the Cloud Scheduler Service Agent role to your
  > Cloud Scheduler service account](https://docs.cloud.google.com/scheduler/docs/http-target-auth#add).

- Your Filestore instance:

  ```bash
  export SOURCE_INSTANCE_LOCATION=fs-location
  export SOURCE_INSTANCE_NAME=instance-id
  export SHARE_NAME=file-share-name
  ```

  Replace the following:
  - <var translate="no">fs-location</var> with the zone or region where the source Filestore instance resides.
  - <var translate="no">instance-id</var> with the instance ID of the source Filestore instance.
  - <var translate="no">file-share-name</var> with the name you specify for the NFS file share that is served from the instance.
- Set up environment variables for your Filestore backup:

  ```bash
  export BACKUP_REGION=backup-region
  ```

  Replace <var translate="no">backup-region</var> with the region where you want to store the backup.

## Create a function that creates a backup

1. In the Google Cloud console, go to the Cloud Run functions page.

   [Go to the Cloud Run functions page](https://console.cloud.google.com/run)
2. Click **Write a function** and configure the function as follows:

   - **Configure** :
     - **Service Name** : For this example, we name the function `fsbackup`.
     - **Region** : For this example, select `us-central1`.
     - **Runtime** : Select any supported Python 3 Runtime [fully supported by Cloud Run functions](https://docs.cloud.google.com/functions/docs/runtime-support.md.txt#python) from the menu.
   - **Trigger** :
     - Trigger doesn't need to be set for this example.
   - **Authentication** : Select `Require authentication`.
   - **Ingress** : Select `All`.
   - **Container(s), Volumes, Networking, Security**
     - Go to **Security** tab and select `Service Account for FS Backups-GCF` (`backupagent@$PROJECT_ID.iam.gserviceaccount.com`) from the menu.
3. Click **Create** and continue the configuration as follows:

   - **Function entry point** : Enter `create_backup`.
   - Add the following dependencies to your `requirements.txt` file:

     ```yaml
     functions-framework==3.*
     google-auth==2.29.0
     requests==2.31.0
     ```

     Depending on your use case, you may need to specify other dependencies
     along with their corresponding version numbers. For more information, see
     [Pre-installed packages](https://docs.cloud.google.com/functions/docs/writing/specifying-dependencies-python#pre-installed_packages).
   - Copy the following Python code sample into the `main.py` file using the
     inline editor:

     ### Create a backup

     This code sample creates a backup named `mybackup-` appended with
     the creation time.

     ```python
     PROJECT_ID = 'project-id'
     SOURCE_INSTANCE_LOCATION = 'fs-location'
     SOURCE_INSTANCE_NAME = 'instance-id'
     SOURCE_FILE_SHARE_NAME = 'file-share-name'
     BACKUP_REGION = 'backup-region'

     import functions_framework
     import google.auth
     import google.auth.transport.requests
     from google.auth.transport.requests import AuthorizedSession
     import time
     import requests
     import json

     credentials, project = google.auth.default()
     auth_request = google.auth.transport.requests.Request()
     credentials.refresh(auth_request)
     authed_session = AuthorizedSession(credentials)

     def get_backup_id():
         return "mybackup-" + time.strftime("%Y%m%d-%H%M%S")

     @functions_framework.http
     def create_backup(request):
         trigger_run_url = "https://file.googleapis.com/v1/projects/{}/locations/{}/backups?backupId={}".format(PROJECT_ID, BACKUP_REGION, get_backup_id())
         headers = {
           'Content-Type': 'application/json'
         }
         post_data = {
           "description": "my new backup",
           "source_instance": "projects/{}/locations/{}/instances/{}".format(PROJECT_ID, SOURCE_INSTANCE_LOCATION, SOURCE_INSTANCE_NAME),
           "source_file_share": "{}".format(SOURCE_FILE_SHARE_NAME)
         }
         print("Making a request to " + trigger_run_url)
         r = authed_session.post(url=trigger_run_url, headers=headers, data=json.dumps(post_data))
         data = r.json()
         print(data)
         if r.status_code == requests.codes.ok:
           print(str(r.status_code) + ": The backup is uploading in the background.")
         else:
           raise RuntimeError(data['error'])
         return "Backup creation has begun!"
     ```

     Replace the following:
     - <var translate="no">project-id</var> with the Google Cloud project ID of the source Filestore instance.
     - <var translate="no">fs-location</var> with the zone or region of the source Filestore instance.
     - <var translate="no">instance-id</var> with the name of the source Filestore instance.
     - <var translate="no">file-share-name</var> with the name of the file share.
     - <var translate="no">backup-region</var> with the region to store the backup.

     1. Click **Test**

        A new tab session opens in Cloud Shell. In it, the following
        message is returned if successful:

            Backup creation has begun!

     2. Click **Save and redeploy** and wait for the deployment to finish.

     3. Switch back to the previous Cloud Shell tab.

     ### Delete a backup

     This code sample deletes backups older than a predefined period.

     > [!CAUTION]
     > **Caution:** This code sample is for illustrative purposes only. It does not differentiate between automatically created backups and manually created backups and can delete backups that are not related to this tutorial. In your implementation, you should filter out manually created backups so that they don't get deleted. You might also want to restrict the script to delete backups of only specific Filestore instances.

     You can only delete one backup per source instance at a time. For more
     information, see [Backups](https://docs.cloud.google.com/filestore/docs/backups.md.txt#operations_concurrency).

     Configure this function in the same way as the function you used to
     create a backup, using the following modifications:
     - **Function name** : `deletefsbackup`.
     - **Entry point** : `delete_backup`.

     ```python
     PROJECT_ID = 'project-id'
     BACKUP_REGION = 'region'
     BACKUP_RETENTION_TIME_HRS = hours

     import functions_framework
     import google.auth
     import google.auth.transport.requests
     from google.auth.transport.requests import AuthorizedSession
     import time
     import requests
     import json

     credentials, project = google.auth.default()
     auth_request = google.auth.transport.requests.Request()
     credentials.refresh(auth_request)
     authed_session = AuthorizedSession(credentials)

     retention_seconds = BACKUP_RETENTION_TIME_HRS * 60 * 60

     @functions_framework.http
     def delete_backup(request):
         now = time.time()
         backup_list = []
         trigger_run_url = "https://file.googleapis.com/v1/projects/{}/locations/{}/backups".format(PROJECT_ID, BACKUP_REGION)
         r = authed_session.get(trigger_run_url)
         data = r.json()
         if not data:
             print("No backups to delete.")
             return "No backups to delete."
         else:
             backup_list.extend(data['backups'])
             while "nextPageToken" in data.keys():
                 nextPageToken = data['nextPageToken']
                 trigger_run_url_next = "https://file.googleapis.com/v1/projects/{}/locations/{}/backups?pageToken={}".format(PROJECT_ID, BACKUP_REGION, nextPageToken)
                 r = authed_session.get(trigger_run_url_next)
                 data = r.json()
                 backup_list.extend(data['backups'])
         for i in backup_list:
             backup_time = i['createTime']
             backup_time = backup_time[:-4]
             backup_time = float(time.mktime(time.strptime(backup_time, "%Y-%m-%dT%H:%M:%S.%f")))
             i['backup_timestamp'] = backup_time
         sorted_backup_list = sorted(backup_list, key=lambda d: d['backup_timestamp'])
         oldest_backup = sorted_backup_list[0]
         if now - oldest_backup['backup_timestamp'] > retention_seconds:
             print(oldest_backup['name'] + " is older than the indicated retention time.")
             r = authed_session.delete("https://file.googleapis.com/v1/{}".format(oldest_backup['name']))
             data = r.json()
             print(data)
             if r.status_code == requests.codes.ok:
                 print(str(r.status_code) + ": Deleting " + oldest_backup['name'] + " in the background.")
             else:
                 raise RuntimeError(data['error'])
             return "Backup deletion has begun!"
         return "All backups are within the indicated retention period."
     ```

     Replace the following:
     - <var translate="no">project-id</var> with the Google Cloud project ID of the backup.
     - <var translate="no">region</var> with the region where the backup resides. The backup, scheduler job, and function should all reside in the same location.
     - <var translate="no">hours</var> with the number of hours to retain backups. For example, if you want to retain backups for 10 days, input `240`.

## Assign IAM roles to the client service accounts

1. Add the Cloud Scheduler service agent to the IAM
   policy of the Cloud Scheduler client service account with the role
   of `roles/cloudscheduler.serviceAgent`. This allows the service agent to
   impersonate the client service account in order to invoke the function
   that creates a backup. Run the [`iam service-accounts add-iam-policy-binding`](https://docs.cloud.google.com/sdk/gcloud/reference/iam/service-accounts/add-iam-policy-binding)
   command:

   ```bash
   gcloud iam service-accounts add-iam-policy-binding $SCHEDULER_CLIENT_SA \
       --member=serviceAccount:$SCHEDULER_SA \
       --role=roles/cloudscheduler.serviceAgent
   ```
2. Give the client service account of Cloud Run functions the
   `roles/file.editor` role so that it can make calls to the
   Filestore endpoint. Run the [`projects add-iam-policy-binding`](https://docs.cloud.google.com/sdk/gcloud/reference/projects/add-iam-policy-binding) command:

   ```bash
   gcloud projects add-iam-policy-binding $PROJECT_ID \
       --member=serviceAccount:$GCF_CLIENT_SA \
       --role=roles/file.editor
   ```
3. Grant the client service account of Cloud Scheduler the role of
   `roles/run.invoker` for the function you want to use. Run the
   following [`run services add-iam-policy-binding`](https://docs.cloud.google.com/sdk/gcloud/reference/run/services/add-iam-policy-binding)
   command:

   ### Create a backup

   ```bash
   gcloud run services add-iam-policy-binding fsbackup \
       --member serviceAccount:$SCHEDULER_CLIENT_SA \
       --role roles/run.invoker \
       --region=us-central1
   ```

   Now, only the client service account of Cloud Scheduler can invoke
   `fsbackup`.

   ### Delete a backup

   ```bash
   gcloud run services add-iam-policy-binding deletefsbackup \
       --member serviceAccount:$SCHEDULER_CLIENT_SA \
       --role roles/run.invoker
   ```

   Now, only the client service account of Cloud Scheduler can invoke
   `deletefsbackup`.

## Create a Cloud Scheduler job that triggers the function on a specified schedule

### Create a backup

1. In our example for this tutorial, if you wanted to schedule a backup
   [every weekday at 10 pm](https://docs.cloud.google.com/scheduler/docs/configuring/cron-job-schedules.md.txt#sample_schedules),
   you would use the [`scheduler jobs create http`](https://docs.cloud.google.com/sdk/gcloud/reference/scheduler/jobs/create/http)
   command:

   ```bash
   gcloud scheduler jobs create http fsbackupschedule \
       --schedule "0 22 * * 1-5" \
       --http-method=GET \
       --uri=https://fsbackup-$PROJECT_NUMBER.us-central1.run.app \
       --oidc-service-account-email=$SCHEDULER_CLIENT_SA \
       --location=us-central1
   ```

   The `--schedule` flag is where you specify the frequency in which the job
   runs using [unix-cron](https://man7.org/linux/man-pages/man5/crontab.5.html)
   formatting. For details, see
   [Configuring cron job schedules](https://docs.cloud.google.com/scheduler/docs/configuring/cron-job-schedules.md.txt).

   You can create a maximum of six backups per instance per hour.
2. Start the Cloud Scheduler job created in the previous step. In
   our example, use the [`scheduler jobs runs`](https://docs.cloud.google.com/sdk/gcloud/reference/scheduler/jobs/run)
   command to run it immediately:

   ```bash
   gcloud scheduler jobs run fsbackupschedule
   ```

   The `fsbackupschedule` job invokes the `fsbackup` function immediately once
   you execute the command and then invokes it again every weekday at 10 pm
   until the job is paused.
3. Check the logs for the `fsbackup` function to see if it executes
   properly and returns a `status 200`.

   To view your logs in the Google Cloud console, use the Logs Explorer:
   1. In the Google Cloud console, go to the

      **Logs Explorer** page:

      [Go to **Logs Explorer**](https://console.cloud.google.com/logs/query)

      <br />

      If you use the search bar to find this page, then select the result whose subheading is
      **Logging**.

      Your most recent logs are displayed in the **Query results** pane.
4. Check the status of your existing backups using the [`backups list`](https://docs.cloud.google.com/sdk/gcloud/reference/filestore/backups/list)
   command:

   ```bash
   gcloud filestore backups list
   ```

   The command returns something similar to:

   ```bash
   NAME                      LOCATION     SRC_INSTANCE                        SRC_FILE_SHARE  STATE
   mybackup-20201123-184500  us-central1  us-central1-c/instances/nfs-server  vol1            READY
   ```

### Delete a backup

1. In our example for this tutorial, if you wanted to schedule an operation
   to delete a backup [every weekday at 10 pm](https://docs.cloud.google.com/scheduler/docs/configuring/cron-job-schedules.md.txt#sample_schedules),
   you would use the [`scheduler jobs create http`](https://docs.cloud.google.com/sdk/gcloud/reference/scheduler/jobs/create/http)
   command:

   ```bash
   gcloud scheduler jobs create http deletefsbackupschedule \
       --schedule "0 22 * * 1-5" \
       --http-method=GET \
       --uri=https://us-central1-$PROJECT_ID.cloudfunctions.net/deletefsbackup \
       --oidc-service-account-email=$SCHEDULER_CLIENT_SA    \
       --oidc-token-audience=https://us-central1-$PROJECT_ID.cloudfunctions.net/deletefsbackup
   ```

   The `--schedule` flag is where you specify the frequency in which the job
   runs using [unix-cron](https://man7.org/linux/man-pages/man5/crontab.5.html)
   formatting. For details, see
   [Configuring cron job schedules](https://docs.cloud.google.com/scheduler/docs/configuring/cron-job-schedules.md.txt).

   Backup `delete` operations associated with the same source instance
   must occur one at a time. For more information, see [Backups](https://docs.cloud.google.com/filestore/docs/backups.md.txt#operations_concurrency).
2. Start the Cloud Scheduler job created in the previous step. In our
   example, we use the [`scheduler jobs runs`](https://docs.cloud.google.com/sdk/gcloud/reference/scheduler/jobs/run)
   command to run it immediately:

   ```bash
   gcloud scheduler jobs run deletefsbackupschedule
   ```

   The `deletefsbackupschedule` job invokes the `deletefsbackup` function
   immediately once you execute the command and then invokes it again every
   weekday at 10 pm until the job is paused.
3. Check the logs for the `deletefsbackup` function to see if it executes
   properly and returns a `status 200`.

   To view your logs in the Google Cloud console, use the Logs Explorer:
   1. In the Google Cloud console, go to the

      **Logs Explorer** page:

      [Go to **Logs Explorer**](https://console.cloud.google.com/logs/query)

      <br />

      If you use the search bar to find this page, then select the result whose subheading is
      **Logging**.

      Your most recent logs are displayed in the **Query results** pane.
4. Check the status of your existing backups using the [`backups list`](https://docs.cloud.google.com/sdk/gcloud/reference/filestore/backups/list)
   command:

   ```bash
   gcloud filestore backups list
   ```

   The command returns something similar to:

   ```bash
   NAME                      LOCATION     SRC_INSTANCE                        SRC_FILE_SHARE  STATE
   mybackup-20201123-184500  us-central1  us-central1-c/instances/nfs-server  vol1            READY
   ```

## Low quota alerts for backups

If your implementation of scheduling backups puts you at risk of running out
of backups quota, we recommend that you set up
[low backups quota alerts](https://docs.cloud.google.com/filestore/docs/monitoring-instances.md.txt#low-backups-quota).
This way, you are notified when backups quota runs low.

## Clean up


After you finish the tutorial, you can clean up the resources that you created so that they
stop using quota and incurring charges. The following sections describe how to delete or turn off
these resources.

<br />

### Delete the project


The easiest way to eliminate billing is to delete the project that you
created for the tutorial.

To delete the project:

> [!CAUTION]
> **Caution** : Deleting a project has the following effects:
>
> - **Everything in the project is deleted.** If you used an existing project for the tasks in this document, when you delete it, you also delete any other work you've done in the project.
> - **Custom project IDs are lost.** When you created this project, you might have created a custom project ID that you want to use in the future. To preserve the URLs that use the project ID, such as an `appspot.com` URL, delete selected resources inside the project instead of deleting the whole project.
>
>
> If you plan to explore multiple architectures, tutorials, or quickstarts, reusing projects
> can help you avoid exceeding project quota limits.

1. In the Google Cloud console, go to the **Manage resources** page.

   [Go to Manage resources](https://console.cloud.google.com/iam-admin/projects)
2. In the project list, select the project that you want to delete, and then click **Delete**.
3. In the dialog, type the project ID, and then click **Shut down** to delete the project.

<br />

<br />

## What's next

- Learn about [Filestore snapshots](https://docs.cloud.google.com/filestore/docs/snapshots.md.txt).
- Learn more about [Filestore backups](https://docs.cloud.google.com/filestore/docs/backups.md.txt).
- Learn how to [Schedule Filestore Enterprise snapshots](https://docs.cloud.google.com/filestore/docs/schedule-filestore-enterprise-snapshots.md.txt).