Backup Script for Linux and Windows - Systems (middle school)

From Wikiversity
Jump to navigation Jump to search

TITLE : Creating a Backup Script for Windows and Linux - Systems[edit | edit source]

AUTHOR : Peretto Lucas[edit | edit source]

REQUIREMENTS : Any Text Editor for Linux and Windows and any Command Interpreter (CMD, Shell)[edit | edit source]

DESCRIPTION : The Script backs up (saves a copy of all files and directories in a determinadet directory) a directory with all subfolders and files to keep it secure.[edit | edit source]

                                            ==HOW IT WORKS FOR WINDOWS==

CODE :[edit | edit source]

@echo off

IF [%1] EQU [] @echo "ERROR : Type in the Parameters" IF [%2] EQU [] @echo "ERROR : Type in the second Parameter" IF [%3] EQU [] @echo "ERROR : Type in the Time"


if 1 EQU 1 (

 @echo %~f1 %~f2    
 ROBOCOPY %~f1 %~f2
 timeout %3% /NOBREAK  

goto WHILE_0 )

To run the Script open a Command Interpreter (CMD)


And type the name of the script in the console (it has to end with .bat):


than put in the two directories (left : where to copy from ), (right : where to copy to ) and the Time delay you want (in seconds)

END-RESULT : All the Data from the directory gets copied to the wanted folder.[edit | edit source]

                                            ==HOW IT WORKS FOR LINUX ==

You can edit the script in any Text Editor you prefear.


  1. !/bin/bash
  1. Checking for parameters

if [ $# -eq 0 ]; then

 echo "Missing Parameters" >&2
 exit 1

fi if [ $# -eq 1 ]; then

 echo "Type also the Second Parameter !" >&2
 exit 1

fi if [ $# -eq 2 ]; then

 echo "Type also the Time Parameter !" >&2
 exit 1

fi if [ $# -eq 4 ]; then

 echo "Too much Parameters !" >&2
 exit 1


  1. Defining PATH1, PATH2 and the Time in minutes

PATH1="$1" PATH2="$2" TIME="$3"

  1. endless loop until user stops it
  2. loop repeats in a predefined time an copys a directory with all subordinate directorys and files

while [ $((1)) == 1 ]; do

     cp -r "$PATH1" "$PATH2"
     echo "backed up !"
 sleep "$TIME"m


To run the Script open a Command Interpreter (Shell) and write the name with the argumens : ./YourProgramName: (where to copy from) (where to copy to) (time in minutes)