{"id":569,"date":"2017-08-30T13:03:00","date_gmt":"2017-08-30T12:03:00","guid":{"rendered":"http:\/\/chewett.co.uk\/blog\/?p=569"},"modified":"2017-11-18T21:43:57","modified_gmt":"2017-11-18T21:43:57","slug":"creating-raspberry-pi-backup-script-scp-bash-windows","status":"publish","type":"post","link":"https:\/\/chewett.co.uk\/blog\/569\/creating-raspberry-pi-backup-script-scp-bash-windows\/","title":{"rendered":"Creating a Raspberry Pi backup script with scp and Bash on Windows"},"content":{"rendered":"<p>This post describes a simple way to keep backups of a Raspberry Pi or any other Linux computer.<\/p>\n<h2><!--more--><\/h2>\n<h2>Why do I need backups of my Raspberry Pi?<\/h2>\n<p>Some of the nodes <a href=\"\/blog\/367\/overview-raspberry-pi-cluster\/\">in my cluster<\/a> are going to be compute only, meaning that they will just run what is asked of them. Others will hold important information such as a database or files that the other nodes are processing. An important feature of having a cluster is ensuring that any information is always available.<\/p>\n<p>There are a couple ways of doing this but the most simple one is ensuring that your important data is always backed up. Then if something were to happen to an important node it can be replaced and the data can be restored.<\/p>\n<p>In the future I will look into more advanced ways of ensuring the data is spread across the cluster but for now a low tech solution (having a script to save important files) is simple and easy to use.<\/p>\n<h2>Testing what scp can do for us<\/h2>\n<p>To run and backup the script I am going to use the Linux tool <code>scp<\/code>\u00a0which stands for Secure Copy. Originally this would have been only accessible to those running Linux or some kind of emulation tools. However recently Windows released &#8220;Windows Subsystem for Linux&#8221; which allows you to run the core of Ubuntu on Windows.<\/p>\n<p>This guide assumes you have <a href=\"\/blog\/572\/installing-ubuntu-linux-tools-windows\/\">installed Windows Subsystem for Linux<\/a> or are running a Linux based OS.<\/p>\n<p>The program <code>scp<\/code>\u00a0can be used to transfer files to and from remote Linux operating systems over SSH.<\/p>\n<p>A basic command to copy a file from your raspberry pi (or other Linux systems) is:<\/p>\n<pre>scp bunker-master2:\/home\/pi\/file.txt file.txt<\/pre>\n<p>Here we are copying a file called <code>file.txt<\/code>\u00a0in the <code>\/home\/pi<\/code>\u00a0 directory from a computer called <code>bunker-master2<\/code>\u00a0and saving it as <code>file.txt<\/code>.<\/p>\n<p>This is helpful but only lets us copy a single file at a time. We can improve this command by replacing the second filename with a dot. This tells scp to put the file in the current directory<\/p>\n<pre>scp bunker-master2:\/home\/pi\/file.txt .<\/pre>\n<p>This means we dont need to specify the filename of the file we are copying. It will always use the name of the file as it exists on the remote host. In addition to using dot to mean the current directory, we can use the asterisk character <code>*<\/code>\u00a0to use as a wildcard. By changing the above command and replacing <code>file.txt<\/code> with an asterisk we can make it download the entire directory.<\/p>\n<pre>scp bunker-master2:\/home\/pi\/* .<\/pre>\n<h2>Creating the backup script<\/h2>\n<p>Now the improved <code>scp<\/code>\u00a0command copies nearly every file (note, it wont copy dot files) from the <code>pi<\/code>\u00a0home directory. Using this we can change what we copy to copy everything of importance to me off the Raspberry Pi. Below is now my backup file which I can use to backup my Pi.<\/p>\n<pre>rm -rf home var\r\nscp -r bunker-master2:\/home\/* home\/\r\nscp -r bunker-master2:\/var\/www\/* var\/www\/<\/pre>\n<p>The first command we run is to delete the old copies we downloaded from the Raspberry Pi. This means any files that were deleted on the Raspberry Pi will not be stored on our computer.<\/p>\n<p>Depending on what is important to you, you might want to consider backing up <code>\/etc<\/code> for your configuration files.<\/p>\n<p>I have added these commands to a file called <code>backup.sh<\/code>\u00a0and I can run the file by running <code>.\/backup.sh<\/code>\u00a0in the bash terminal.<\/p>\n<p>In the future, I am going to improve this backup method as there are a couple of downsides to using it. One of them is that even if a file hasn&#8217;t changed from your local archive, it will still copy it across. In addition, this won&#8217;t backup files such as a MySQL database which to properly backup require\u00a0a different type of backup.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This post describes a simple way to keep backups of a Raspberry Pi or any other Linux computer.<\/p>\n","protected":false},"author":1,"featured_media":612,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_exactmetrics_skip_tracking":false,"_exactmetrics_sitenote_active":false,"_exactmetrics_sitenote_note":"","_exactmetrics_sitenote_category":0,"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[5,122],"tags":[159,158,39,37,147,29,156],"class_list":["post-569","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-informational","category-raspberry-pi-cluster","tag-backup","tag-bash","tag-linux","tag-raspberry-pi","tag-ubuntu","tag-windows","tag-windows-subsystem-for-linux"],"wppr_data":{"cwp_meta_box_check":"No"},"jetpack_publicize_connections":[],"jetpack_featured_media_url":"https:\/\/i0.wp.com\/chewett.co.uk\/blog\/wp-content\/uploads\/2017\/08\/backing_up_a_rasperry_pi.jpg?fit=800%2C800&ssl=1","jetpack_shortlink":"https:\/\/wp.me\/p2toWX-9b","jetpack_sharing_enabled":true,"jetpack-related-posts":[{"id":613,"url":"https:\/\/chewett.co.uk\/blog\/613\/backup-raspberry-pi-rsync\/","url_meta":{"origin":569,"position":0},"title":"Backup a Raspberry Pi with Rsync","author":"Chewett","date":"December 20, 2017","format":false,"excerpt":"I am going to upgrade our previous Raspberry Pi backup script that previously used Secure Copy to one that uses rsync. The primary reason for this is that rsync will only copy new or changed files over. This post goes through the reasons why you might want to use rsync\u00a0instead\u2026","rel":"","context":"In &quot;Raspberry Pi Cluster&quot;","block_context":{"text":"Raspberry Pi Cluster","link":"https:\/\/chewett.co.uk\/blog\/category\/raspberry-pi-cluster\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/chewett.co.uk\/blog\/wp-content\/uploads\/2017\/12\/rsync_backup_raspberry_pi.jpg?fit=800%2C800&ssl=1&resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/chewett.co.uk\/blog\/wp-content\/uploads\/2017\/12\/rsync_backup_raspberry_pi.jpg?fit=800%2C800&ssl=1&resize=350%2C200 1x, https:\/\/i0.wp.com\/chewett.co.uk\/blog\/wp-content\/uploads\/2017\/12\/rsync_backup_raspberry_pi.jpg?fit=800%2C800&ssl=1&resize=525%2C300 1.5x, https:\/\/i0.wp.com\/chewett.co.uk\/blog\/wp-content\/uploads\/2017\/12\/rsync_backup_raspberry_pi.jpg?fit=800%2C800&ssl=1&resize=700%2C400 2x"},"classes":[]},{"id":615,"url":"https:\/\/chewett.co.uk\/blog\/615\/compressing-raspberry-pi-backups-automatically\/","url_meta":{"origin":569,"position":1},"title":"Compressing Raspberry Pi backups automatically","author":"Chewett","date":"November 22, 2017","format":false,"excerpt":"Today I will be improving the basic script used to backup my Raspberry Pi's so that it compresses the files after they have been copied. This will allow me to store several backups instead of having a single copy stored on my computer. This assumes you have followed the first\u2026","rel":"","context":"In &quot;Raspberry Pi Cluster&quot;","block_context":{"text":"Raspberry Pi Cluster","link":"https:\/\/chewett.co.uk\/blog\/category\/raspberry-pi-cluster\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/chewett.co.uk\/blog\/wp-content\/uploads\/2017\/11\/compressing_raspberry_pi.jpg?fit=800%2C800&ssl=1&resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/chewett.co.uk\/blog\/wp-content\/uploads\/2017\/11\/compressing_raspberry_pi.jpg?fit=800%2C800&ssl=1&resize=350%2C200 1x, https:\/\/i0.wp.com\/chewett.co.uk\/blog\/wp-content\/uploads\/2017\/11\/compressing_raspberry_pi.jpg?fit=800%2C800&ssl=1&resize=525%2C300 1.5x, https:\/\/i0.wp.com\/chewett.co.uk\/blog\/wp-content\/uploads\/2017\/11\/compressing_raspberry_pi.jpg?fit=800%2C800&ssl=1&resize=700%2C400 2x"},"classes":[]},{"id":367,"url":"https:\/\/chewett.co.uk\/blog\/367\/overview-raspberry-pi-cluster\/","url_meta":{"origin":569,"position":2},"title":"Overview of the Raspberry Pi Cluster","author":"Chewett","date":"June 17, 2017","format":"gallery","excerpt":"Now I have the power supply and built the stacking system\u00a0I can show off the clusters initial state. Every Raspberry Pi in\u00a0the cluster Here are the 7 initial Pi's that are to become part of the cluster. Each have been given a codename to designate\u00a0its position in the cluster.\u00a0The Raspberry\u2026","rel":"","context":"In &quot;Raspberry Pi Cluster&quot;","block_context":{"text":"Raspberry Pi Cluster","link":"https:\/\/chewett.co.uk\/blog\/category\/raspberry-pi-cluster\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/chewett.co.uk\/blog\/wp-content\/uploads\/2017\/05\/cluster_uncovered.jpg?resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/chewett.co.uk\/blog\/wp-content\/uploads\/2017\/05\/cluster_uncovered.jpg?resize=350%2C200 1x, https:\/\/i0.wp.com\/chewett.co.uk\/blog\/wp-content\/uploads\/2017\/05\/cluster_uncovered.jpg?resize=525%2C300 1.5x, https:\/\/i0.wp.com\/chewett.co.uk\/blog\/wp-content\/uploads\/2017\/05\/cluster_uncovered.jpg?resize=700%2C400 2x"},"classes":[]},{"id":567,"url":"https:\/\/chewett.co.uk\/blog\/567\/upgrading-raspbian-jessie-raspbian-stretch\/","url_meta":{"origin":569,"position":3},"title":"Upgrading Raspbian Jessie to Raspbian Stretch","author":"Chewett","date":"November 4, 2017","format":false,"excerpt":"This post describes how you can upgrade Raspbian Jessie, version 8, to Raspbian Stretch, version 9. Raspbian Stretch was recently released and while they recommend installing from fresh you are able to upgrade the versions. Here I follow the steps of upgrading the Raspberry Pi's in my cluster including some\u2026","rel":"","context":"In &quot;Raspberry Pi Cluster&quot;","block_context":{"text":"Raspberry Pi Cluster","link":"https:\/\/chewett.co.uk\/blog\/category\/raspberry-pi-cluster\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/chewett.co.uk\/blog\/wp-content\/uploads\/2017\/10\/upgrading_to_raspbian_stretch.jpg?fit=800%2C800&ssl=1&resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/chewett.co.uk\/blog\/wp-content\/uploads\/2017\/10\/upgrading_to_raspbian_stretch.jpg?fit=800%2C800&ssl=1&resize=350%2C200 1x, https:\/\/i0.wp.com\/chewett.co.uk\/blog\/wp-content\/uploads\/2017\/10\/upgrading_to_raspbian_stretch.jpg?fit=800%2C800&ssl=1&resize=525%2C300 1.5x, https:\/\/i0.wp.com\/chewett.co.uk\/blog\/wp-content\/uploads\/2017\/10\/upgrading_to_raspbian_stretch.jpg?fit=800%2C800&ssl=1&resize=700%2C400 2x"},"classes":[]},{"id":901,"url":"https:\/\/chewett.co.uk\/blog\/901\/raspberry-pi-cluster-node-03-basic-node-communication-two-nodes\/","url_meta":{"origin":569,"position":4},"title":"Raspberry Pi Cluster Node \u2013 03 Basic node communication between two nodes","author":"Chewett","date":"December 9, 2017","format":false,"excerpt":"This post builds on the second step to create a Raspberry Pi Cluster Node to create a hello world application across two different scripts. Here we will create a socket connection between the two and use it to send a message to the master. Designing the client-master Raspberry Pi Nodes\u2026","rel":"","context":"In &quot;Raspberry Pi Cluster&quot;","block_context":{"text":"Raspberry Pi Cluster","link":"https:\/\/chewett.co.uk\/blog\/category\/raspberry-pi-cluster\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/chewett.co.uk\/blog\/wp-content\/uploads\/2017\/12\/rpi_cluster_03_basic_communication.jpg?fit=800%2C800&ssl=1&resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/chewett.co.uk\/blog\/wp-content\/uploads\/2017\/12\/rpi_cluster_03_basic_communication.jpg?fit=800%2C800&ssl=1&resize=350%2C200 1x, https:\/\/i0.wp.com\/chewett.co.uk\/blog\/wp-content\/uploads\/2017\/12\/rpi_cluster_03_basic_communication.jpg?fit=800%2C800&ssl=1&resize=525%2C300 1.5x, https:\/\/i0.wp.com\/chewett.co.uk\/blog\/wp-content\/uploads\/2017\/12\/rpi_cluster_03_basic_communication.jpg?fit=800%2C800&ssl=1&resize=700%2C400 2x"},"classes":[]},{"id":1054,"url":"https:\/\/chewett.co.uk\/blog\/1054\/raspberry-pi-tutorial-website-online-now\/","url_meta":{"origin":569,"position":5},"title":"Raspberry Pi Tutorial Website online now!","author":"Chewett","date":"March 10, 2018","format":false,"excerpt":"This post talks about the new page on my website with linking to all my Raspberry Pi Cluster tutorials. Raspberry Pi Cluster Tutorial Webpage Now I have a couple Raspberry Pi Cluster tutorials I decided to link to all of them on my website. This will form the basis of\u2026","rel":"","context":"In &quot;Raspberry Pi Cluster&quot;","block_context":{"text":"Raspberry Pi Cluster","link":"https:\/\/chewett.co.uk\/blog\/category\/raspberry-pi-cluster\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/chewett.co.uk\/blog\/wp-content\/uploads\/2018\/03\/rpi_tutorial_website_online.jpg?fit=800%2C800&ssl=1&resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/chewett.co.uk\/blog\/wp-content\/uploads\/2018\/03\/rpi_tutorial_website_online.jpg?fit=800%2C800&ssl=1&resize=350%2C200 1x, https:\/\/i0.wp.com\/chewett.co.uk\/blog\/wp-content\/uploads\/2018\/03\/rpi_tutorial_website_online.jpg?fit=800%2C800&ssl=1&resize=525%2C300 1.5x, https:\/\/i0.wp.com\/chewett.co.uk\/blog\/wp-content\/uploads\/2018\/03\/rpi_tutorial_website_online.jpg?fit=800%2C800&ssl=1&resize=700%2C400 2x"},"classes":[]}],"_links":{"self":[{"href":"https:\/\/chewett.co.uk\/blog\/wp-json\/wp\/v2\/posts\/569","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/chewett.co.uk\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/chewett.co.uk\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/chewett.co.uk\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/chewett.co.uk\/blog\/wp-json\/wp\/v2\/comments?post=569"}],"version-history":[{"count":8,"href":"https:\/\/chewett.co.uk\/blog\/wp-json\/wp\/v2\/posts\/569\/revisions"}],"predecessor-version":[{"id":863,"href":"https:\/\/chewett.co.uk\/blog\/wp-json\/wp\/v2\/posts\/569\/revisions\/863"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/chewett.co.uk\/blog\/wp-json\/wp\/v2\/media\/612"}],"wp:attachment":[{"href":"https:\/\/chewett.co.uk\/blog\/wp-json\/wp\/v2\/media?parent=569"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/chewett.co.uk\/blog\/wp-json\/wp\/v2\/categories?post=569"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/chewett.co.uk\/blog\/wp-json\/wp\/v2\/tags?post=569"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}