The following bash script helps you to create automatic scheduled snapshot for all your google cloud instances as per your convenience.

Features of the following script includes:

  • Automatically runs as per your convenience.
  • Customization in days (daily, weekly, monthly), flexible time (you can choose time based on your server loading in your time zone).
  • Customize the rotation, i.e., how many days a snapshot should be kept and when it should be deleted.
  • Notification on successful running of script. Receive emails to your inbox after execution of code.

Requirements:

  • 1 or more Google Cloud VM Instances (up and running).
  • SSH Client like putty or direct SSH access in GCP Console.
  • admin or root access to GCP Instance.
  • Little Programming Knowledge in Linux and Python.

Steps and Instructions:

  1. Login to your GCP Console and connect to your instance using SSH (or) Connect to your instance using putty.
  2. navigate to root folder of the instance using the following code.
  3. cd ~
  4. Gain root access using the following code.
  5. sudo su
  6. Now find a suitable location in your instance to save the code. Preferably navigate to the following location.
  7. cd /srv/users/serverpilot/backup
  8. The above location is for example purpose. You can choose your own accessible folder.
  9. After navigating, create a new script file named snapshot.sh using the following code.
  10. nano snapshot.sh
  11. A blank file opens in your command window. Paste the following using right-click (for putty) or ctrl+v (for GCP SSH).
  12. #!/bin/bash
    
    # Author: hexacube India
    
    # Snapshot Creation Starts here.
    
    gcloud compute disks list --format='value(name,zone)'| while read DISK_NAME ZONE; do
    
     gcloud compute disks snapshot $DISK_NAME --snapshot-names autogcs-${DISK_NAME:0:31}-$(date "+%Y-%m-%d-%s") --zone $ZONE
    
    done
    
    # Snapshot Creation Ends here.
    
    #
    
    # Delete Snapshots aged more than 27 days.
    
    if [[ $(uname) == "Linux" ]]; then
    
     from_date=$(date -d "-27 days" "+%Y-%m-%d")
    
    else
    
     from_date=$(date -v -27d "+%Y-%m-%d")
    
    fi
    
    gcloud compute snapshots list --filter="creationTimestamp<$from_date" --regexp "(autogcs.*)" --uri | while read SNAPSHOT_URI; do
    
     gcloud compute snapshots delete $SNAPSHOT_URI
    
    done
    
    # Snapshot deletion ends here.
  13. Once pasted the above code, save them using shortcut ctrl+x
  14. That’s it. You have successfully created the script for snapshot process.
  15. Now schedule it run automatically using crontab.
  16. Type the following code to edit crontab
  17. crontab -e
  18. Goto bottom of the script and insert your cronjob as given below
  19. MAILTO=hexacubeindia@gmail.com
    
    30 22 * * 1 /srv/users/serverpilot/backup/snapshotbackup.sh
  20. In the above code, 30 indicates minutes, 22 indicates hours, first star indicates day of month, second star indicates month and 1 indicates day of week. So the above cronjob runs every monday 22:30 hours.
  21. You can generate your own cron using a simple tool that can be found in crontab guru here
  22. Once done. Everything is set.
  23. You can manually run the snapshot process using the following code in SSH
  24. bash /srv/users/serverpilot/backup/snapshotbackup.sh
  25. Incase of any difficulties, post it in the comments section below.
  26. Happy Hosting !
The following two tabs change content below.
Specific Thinking in Science & Technology

Latest posts by Think Specific (see all)


Think Specific

Specific Thinking in Science & Technology

12 Comments

jmitchell@arienne.com · February 28, 2018 at 8:26 pm

First of all, thanks for the work on this. I have it sort of half working. It creates all the snapshots, but when it gets to the deleting of the old ones it throws this error:

usage: gcloud compute snapshots list [NAME …] [optional flags]
ERROR: (gcloud.compute.snapshots.list) unrecognized arguments: –filter=creationTimestamp<2018-02-01

    Think Specific · March 1, 2018 at 9:49 am

    GCP, recently deprecated the filter argument. Kindly use the following script (updated and tested).


    #!/bin/bash
    # Script Name: Do Snapshot
    # Script Description: Take live snapshots of all disks under a project. Gcloud init should be run for default account and project before execution.
    # Company: hexacube India
    # Website: www.hexacube.in
    # Email: info@hexacube.in
    # Last Updated: 2018-01-11
    #
    # Snapshot Creation Starts
    #
    starttime=$(date -d @$(( $(date +"%s") + 19800)) +"%Y-%m-%d-%H-%M")
    echo "Snapshot Creation Process started on $starttime"
    echo " "
    datetimestamp=$(date -d @$(( $(date +"%s") + 19800)) +"%Y-%m-%d---%H-%M-%S")
    gcloud compute disks list --format='value(name,zone)'| while read DISK_NAME ZONE; do
    gcloud compute disks snapshot $DISK_NAME --snapshot-names ${DISK_NAME:0:31}---$datetimestamp --zone $ZONE
    done
    #
    # Snapshot Creation Ends
    # Old Snapshot Deletion Starts
    #
    gcloud compute snapshots list --filter="creationTimestamp< $(date -d "-31 days" "+%Y-%m-%d")" --uri | while read -r SNAPSHOT_URI; do gcloud compute snapshots delete "$SNAPSHOT_URI" --quiet done echo " " endtime=$(date -d @$(( $(date +"%s") + 19800)) +"%Y-%m-%d-%H-%M") echo "Snapshot Creation Process completed on $endtime" echo " " echo "List of available snapshots" echo " " gcloud compute snapshots list echo " " # # Old Snapshot Deletion Ends #

Think Specific · March 1, 2018 at 9:55 am

Download Script (.sh) directly from the folowing URI:

https://dl.hexacube.in/resources/dosnapshot.sh

(Right-click and save-as to download script)

jmitchell@arienne.com · March 1, 2018 at 3:55 pm

Thank you so much for the quick response. A couple quick things. You said in your original comment that GCP recently deprecated the “filter” argument, but I see that same argument in the new script you did (under the “Old Snapshot Deletion Starts” line)… How will that work?

With your first script the snapshots would run, but I would get that error on the removal of the snapshots. On the new script, the snapshots aren’t even working. I get this error: “ERROR: (gcloud.compute.disks.snapshot) Some requests did not succeed:

– Invalid value for field ‘snapshot.name’: ‘SERVERNAME—‘. Must be a match of regex ‘(?:[a-z](?:[-a-z0-9]{0,61}[a-z0-9])?)'”

For now I have merged your old script with your new one. It is doing the backups now. We’ll see if it removes the snapshots like it should. Here is what I have:

# Snapshot Creation Starts here.
gcloud compute disks list –format=’value(name,zone)’| while read DISK_NAME ZONE; do
gcloud compute disks snapshot $DISK_NAME –snapshot-names autogcs-${DISK_NAME:0:31}-$(date “+%Y-%m-%d-%s”) –zone $ZONE
done
# Old Snapshot Deletion Starts
#
gcloud compute snapshots list –filter=”creationTimestamp<$(date -d "-31 days" "+%Y-%m-%d")" –uri | while read -r SNAPSHO
gcloud compute snapshots delete "$SNAPSHOT_URI" –quiet
done

jmitchell@arienne.com · March 1, 2018 at 4:21 pm

As I suspected, same error “ERROR: (gcloud.compute.snapshots.list) unrecognized arguments: –filter=creationTimestamp<"

    Think Specific · March 2, 2018 at 5:44 am

    There are differences between “-” and “–”. better u download the file directly given here. Also using notepad++ for editing. I see lot of mistakes in the script u have pasted in comments. Hope this solves the issue.

    Download here: https://dl.hexacube.in/resources/dosnapshot.sh

      lucas@inventoryclub.com · July 16, 2018 at 9:21 am

      Thanks this latest update worked for me.

Dharmendra Kumar · December 5, 2018 at 5:42 pm

hey, thank you. But could you please help to customize it. i do not want to create snapshot of all the disk in a project. i want to create snapshot of 10 disk out of 50.
This is script will create snapshot for all the disk in the project.
But i want define the disk name to create snapshot using this script.

    Think Specific · December 5, 2018 at 5:47 pm

    Glad to help. Could you be more specific about your need.

Mark John Myers · December 15, 2018 at 1:35 pm

how can you accomplish this on a windows vm

Leave your comments..