Over the next few weeks (and possibly months), I’m going to create a series of quick tips, as I’m delving into linux (Raspbian), when I’m programming away on my new Raspberry Pi 2.

I’ve already learnt loads, so I need to get it written down before I forget it all!

This little tip is something I picked up while I was writing a script to backup a folder for a Project I’m working on in QT Creator.

I like to make regular backups of projects as I go along and as I hit milestones, so I prefer to date and time stamp my Zip Files. To begin with I created a Shell or “sh” script (In actual fact it’s a Bash Script, and the difference is explained on SO here);

[fusion_builder_container hundred_percent=”yes” overflow=”visible”][fusion_builder_row][fusion_builder_column type=”1_1″ background_position=”left top” background_color=”” border_size=”” border_color=”” border_style=”solid” spacing=”yes” background_image=”” background_repeat=”no-repeat” padding=”” margin_top=”0px” margin_bottom=”0px” class=”” id=”” animation_type=”” animation_speed=”0.3″ animation_direction=”left” hide_on_mobile=”no” center_content=”no” min_height=”none”][sourcecode language=”bash”]
#!/bin/bash
# Script to Backup my Project
cd /home/pi/projects
[/sourcecode]

Also, a side effect of my need to run QT Creator as root, means that some files end up being owned by root, and some files are owned by the normal pi user, so I also needed to change the ownership of the Project Directory and all it’s files.

Changing ownership is accomplished using the chown command, which I chose to run as sudo, and change the ownership the files to the normal pi user;

[/fusion_builder_column][fusion_builder_column type=”1_1″ background_position=”left top” background_color=”” border_size=”” border_color=”” border_style=”solid” spacing=”yes” background_image=”” background_repeat=”no-repeat” padding=”” margin_top=”0px” margin_bottom=”0px” class=”” id=”” animation_type=”” animation_speed=”0.3″ animation_direction=”left” hide_on_mobile=”no” center_content=”no” min_height=”none”][sourcecode language=”bash”]
sudo chown pi YourProjectDirectoryName -R
[/sourcecode]

The chown command accepts a User to Change the Ownership to, a File or Directory Name followed by a collection of switches to alter the process… I wanted to change the ownership of the Folder and all the files and folders inside, so I added the “-R” switch, which recursively changes the ownership of all the files and folders in that folder.

Next the crux of the script, Zipping up the Project Folder… I prefer to use Zip rather than BZ etc..;

[/fusion_builder_column][fusion_builder_column type=”1_1″ background_position=”left top” background_color=”” border_size=”” border_color=”” border_style=”solid” spacing=”yes” background_image=”” background_repeat=”no-repeat” padding=”” margin_top=”0px” margin_bottom=”0px” class=”” id=”” animation_type=”” animation_speed=”0.3″ animation_direction=”left” hide_on_mobile=”no” center_content=”no” min_height=”none”][sourcecode language=”bash”]
sudo zip -r "YourProjectName – `date +%d-%m-%y` – `date +%H-%M-%S`.zip" YourProjectDirectoryName
[/sourcecode]

Here we start by creating a Zip file using the zip command, which accepts various switches and arguments.

Firstly the “-r” switch, which recursively travels through all files and folders adding them to the Zip archive.

After this is the name of our archive. Here I’ve used a bit of Script goodness to get the current date and time using the date command. This command grabs the current date, and allows us to format it using a set of formatting switches. I use it to get the Date and Time like; “08-02-15 – 15-10-14“.

Finally, is the name of the Folder we want to Zip up…. Which leaves us with;

[/fusion_builder_column][fusion_builder_column type=”1_1″ background_position=”left top” background_color=”” border_size=”” border_color=”” border_style=”solid” spacing=”yes” background_image=”” background_repeat=”no-repeat” padding=”” margin_top=”0px” margin_bottom=”0px” class=”” id=”” animation_type=”” animation_speed=”0.3″ animation_direction=”left” hide_on_mobile=”no” center_content=”no” min_height=”none”][sourcecode language=”bash”]
#!/bin/bash
# Script to Backup my Project
cd /home/pi/projects
sudo chown pi YourProjectDirectoryName -R
sudo zip -r "YourProjectName – `date +%d-%m-%y` – `date +%H-%M-%S`.zip" YourProjectDirectoryName
[/sourcecode]

Save your script as whatever you like, with a .sh extension. You can then make it executable by using;

[/fusion_builder_column][fusion_builder_column type=”1_1″ background_position=”left top” background_color=”” border_size=”” border_color=”” border_style=”solid” spacing=”yes” background_image=”” background_repeat=”no-repeat” padding=”” margin_top=”0px” margin_bottom=”0px” class=”” id=”” animation_type=”” animation_speed=”0.3″ animation_direction=”left” hide_on_mobile=”no” center_content=”no” min_height=”none”][sourcecode language=”bash”]
chmod +x YourBackupScriptName.sh
[/sourcecode]
[/fusion_builder_column][/fusion_builder_row][/fusion_builder_container]

By |2017-07-24T08:33:16+01:00February 8th, 2015|Linux, Raspberry Pi, Scripts, Tips|0 Comments

About the Author:

Leave A Comment