{"id":2023,"date":"2021-12-28T10:31:10","date_gmt":"2021-12-28T08:31:10","guid":{"rendered":"https:\/\/upcloud.com\/global\/us\/resources\/tutorials\/simple-backups-terraform\/"},"modified":"2021-12-28T10:31:10","modified_gmt":"2021-12-28T08:31:10","slug":"simple-backups-terraform","status":"publish","type":"tutorial","link":"https:\/\/upcloud.com\/global\/resources\/tutorials\/simple-backups-terraform\/","title":{"rendered":"How to manage Simple Backups using Terraform"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\"><a href=\"https:\/\/upcloud.com\/global\/blog\/upcloud-verified-terraform-provider\/\" target=\"_blank\" rel=\"noopener\">Terraform<\/a> is a simple yet powerful open-source infrastructure management tool developed by HashiCorp. Terraform allows you to safely and predictably manage everything around your Cloud Servers, including Simple Backups,&nbsp;by codifying APIs into declarative configuration files.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><img decoding=\"async\" src=\"https:\/\/upcloud.com\/media\/terraform_logo.png\" alt=\"Terraform logo\" class=\"wp-image-6158\" \/><\/figure>\n<\/div>\n\n\n<p class=\"wp-block-paragraph\">Building your cloud setup around Terraform is a great way to configure something once and be able to deploy it again and again with no effort at all. In this guide, we will show how to manage Simple Backups using Terraform.<\/p>\n\n\n\n<div class=\"wp-block-buttons is-layout-flex wp-block-buttons-is-layout-flex\">\n<div class=\"wp-block-button\"><a class=\"wp-block-button__link wp-element-button\" href=\"https:\/\/signup.upcloud.com\">Try out Terraform on UpCloud<\/a><\/div>\n<\/div>\n\n\n\n<h2 class=\"wp-block-heading\">Getting started<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Managing your cloud resources on UpCloud using Terraform works via our <a href=\"https:\/\/upcloud.com\/global\/docs\/tooling\/terraform-with-upcloud\/\" target=\"_blank\" rel=\"noreferrer noopener\">verified provider module<\/a>. With the UpCloud Terraform provider, it is as simple as declaring the required providers in a configuration file and then giving Terraform something to do.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In practice, you need to allow Terraform to access your UpCloud account by setting an account name and password in your environmental variables. If you haven\u2019t done so already, check out our <a href=\"https:\/\/upcloud.com\/global\/docs\/guides\/get-started-terraform\/\" target=\"_blank\" rel=\"noreferrer noopener\">Terraform intro tutorial<\/a> to set your API credentials before continuing with the rest of this tutorial.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Creating a basic Cloud Server<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Managing cloud infrastructure using Terraform makes it simple to edit, review, and version, as well as easy to share amongst team members. This includes everything related to Cloud Servers and of course Simple Backups. However, before you can enable Simple Backups, you first need a Cloud Server.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">If you have already configured Cloud Servers with Terraform, feel free to skip to the next section.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">First, create a new Terraform configuration, for example, <tt>simple-backup-example.tf<\/tt> in your Terraform directory.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">touch s<tt>imple-backup-example<\/tt>.tf<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Open the file in your favourite text editor then include the resource section as described in the example configuration below.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Replace the <tt>ssh-rsa public key<\/tt>&nbsp;in the login segment with your public SSH key and path to your private SSH key in the connection settings.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">resource \"upcloud_server\" \"simple-backup-example\" {\n  # System hostname\n  hostname = \"s<tt>imple-backup-example<\/tt>.com\"\n\n  # Availability zone\n  zone = \"nl-ams1\"\n\n  # Number of CPUs and memory in GB\n  plan = \"1xCPU-1GB\"\n\n  template {\n    # System storage device size\n    size = 25\n\n    # Template UUID for Ubuntu 20.04\n    storage = \"01000000-0000-4000-8000-000030200200\"\n  }\n\n  # Network interfaces\n  network_interface {\n    type = \"public\"\n  }\n\n  network_interface {\n    type = \"utility\"\n  }\n\n  # Include at least one public SSH key\n  login {\n    user = \"root\"\n    keys = [\n      <span style=\"color: #ff0000;\">\"ssh-rsa public key\"<\/span>,\n    ]\n    create_password = true\n    password_delivery = \"email\"\n  }\n\n  # Configuring connection details\n  connection {\n    # The server public IP address\n    host        = self.network_interface[0].ip_address\n    type        = \"ssh\"\n    user        = \"root\"\n    private_key = file(<span style=\"color: #ff0000;\">\"~\/.ssh\/rsa_private_key\"<\/span>)\n  }\n\n  # Remotely executing a command on the server\n  provisioner \"remote-exec\" {\n    inline = [\n      \"echo 'Hello world!'\"\n    ]\n  }\n}\n<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Once done, just save the file and you are good to go.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">When you\u2019ve defined your Cloud Server configuration, next you need to deploy it. Terraform provides easy-to-use commands for safely and predictably deploying resources and applying changes.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">First, verify your build plan with the following command.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">terraform plan<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">This generates an execution plan that shows what actions will be taken when the plan is applied. It includes the server configuration, log-in details, storage settings, and the deployment zone.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Next, deploy the configuration by executing the plan with the command below.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">terraform apply<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Reply <tt>yes<\/tt> when asked to confirm the deployment.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Terraform performs syntax verifications again in deployment to spare you from configuration errors that could take precious time to roll back.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Separating plans and applying commands reduces mistakes and uncertainty at scale. Plans show operators what would happen when executing changes with the apply command.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Configuring Simple Backups<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Simple Backups are the easy way to back up your Cloud Servers. It saves a full snapshot of the state of your Cloud Server, backing up everything in one go without interruption or slowing down the running Cloud Server.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Configuring Simple Backups using Terraform is as simple as including the following instruction block in your Cloud Server configuration.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">resource \"upcloud_server\" \"simple-backup-example\" {\n  ...\n  template {\n    # System storage device size\n    size = 25\n\n    # Template UUID for Ubuntu 20.04\n    storage = \"01000000-0000-4000-8000-000030200200\"\n  }\n\n  # Simple Backups\n  <span style=\"color: #00ff00;\">simple_backup {\n    plan = \"dailies\"\n    time = \"2200\"\n  }<\/span>\n  ...\n}\n<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The Simple Backup configuration block includes two required parameters, plan and time:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Plan<\/strong> lets you choose how long your backups are kept. The options \u201cdailies\u201d, \u201cweeklies\u201d, and \u201cmonthlies\u201d retain backups for a week, month and year respectively.<\/li>\n\n\n\n<li><strong>Time<\/strong> sets the time of the day format the backup is taken within a 24-hour. You might want to choose a time during lower usage and stagger the backups if you have multiple servers.<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">Once you are all set, just save the file and apply the changes.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">terraform apply<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Again, reply <tt>yes<\/tt> when asked to confirm.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Terraform will then detect the requested changes and apply them in place without any additional steps or downtime.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Enable and forget, with Simple Backups configured, you can rest easy knowing your data is safe! You don\u2019t need to configure backup schedules or retention periods, just pick the backup duration you are likely to need and Simple Backups will the rest.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Furthermore, if you ever want to make changes to the backup plan or time, just update your configuration file and apply the changes using Terraform.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Disabling Simple Backups<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Enabling Simple Backups using Terraform is really easy and disabling it is just as simple. In practice, you just need to remove the simple_backup configuration block. This can be done by deleting the entry from your configuration file. Alternatively, like in the example below, you can comment out the block by adding <em>#<\/em>-charater to the start of each line.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">resource \"upcloud_server\" \"simple-backup-example\" {\n  ...\n  template {\n    # System storage device size\n    size = 25\n\n    # Template UUID for Ubuntu 20.04\n    storage = \"01000000-0000-4000-8000-000030200200\"\n  }\n\n  # Simple Backups\n  <span style=\"color: #00ff00;\">#simple_backup {\n  #  plan = \"dailies\"\n  #  time = \"2200\"\n  #}<\/span>\n  ...\n}\n<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">When ready, save the file and apply the changes.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">terraform apply<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Reply <tt>yes<\/tt> when asked to confirm.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Terraform will compare the configuration file to the existing infrastructure and make the required changes to achieve the configured state.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Note that <strong>disabling the Simple Backup plan does not delete existing backups<\/strong> but instead converts them to flexible backups. When you no longer need the backups, you will need to delete them manually via either your UpCloud Control Panel or the API.<\/p>\n","protected":false},"author":3,"featured_media":23192,"comment_status":"open","ping_status":"closed","template":"","community-category":[262,235],"class_list":["post-2023","tutorial","type-tutorial","status-publish","has-post-thumbnail","hentry"],"acf":[],"_links":{"self":[{"href":"https:\/\/upcloud.com\/global\/wp-json\/wp\/v2\/tutorial\/2023","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/upcloud.com\/global\/wp-json\/wp\/v2\/tutorial"}],"about":[{"href":"https:\/\/upcloud.com\/global\/wp-json\/wp\/v2\/types\/tutorial"}],"author":[{"embeddable":true,"href":"https:\/\/upcloud.com\/global\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/upcloud.com\/global\/wp-json\/wp\/v2\/comments?post=2023"}],"version-history":[{"count":0,"href":"https:\/\/upcloud.com\/global\/wp-json\/wp\/v2\/tutorial\/2023\/revisions"}],"wp:attachment":[{"href":"https:\/\/upcloud.com\/global\/wp-json\/wp\/v2\/media?parent=2023"}],"wp:term":[{"taxonomy":"community-category","embeddable":true,"href":"https:\/\/upcloud.com\/global\/wp-json\/wp\/v2\/community-category?post=2023"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}