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

From Wikiversity - Reading time: 2 min


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[edit | edit source]

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"

:WHILE_0
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[edit | edit source]

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

CODE:

#!/bin/bash

# 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
fi

#Defining PATH1, PATH2 and the Time in minutes
PATH1="$1"
PATH2="$2"
TIME="$3"

# endless loop until user stops it
# 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
done

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)


Licensed under CC BY-SA 3.0 | Source: https://en.wikiversity.org/wiki/Backup_Script_for_Linux_and_Windows_-_Systems_(middle_school)
1 | Status: cached on March 21 2024 06:06:37
↧ Download this article as ZWI file
Encyclosphere.org EncycloReader is supported by the EncyclosphereKSF