Hallo! Könntet mir bitte jemand meinen Code kontrollieren? Ich bin noch nicht so vertraut mit Bash-Programming und hätte daher gerne etwas Sicherheit bevor ich abgebe.
Aufgabe: Write a script my_back_up.sh to perform a back up of the current directory into a given folder. The script receives a single parameter which is the path of the folder where the back up is to be stored. If the back up folder already exists then a file needs to be copy if and only if it does not exist within the back up folder or if it is newer than the existing one in the back up folder.
Aufgabe: Write a script my_back_up.sh to perform a back up of the current directory into a given folder. The script receives a single parameter which is the path of the folder where the back up is to be stored. If the back up folder already exists then a file needs to be copy if and only if it does not exist within the back up folder or if it is newer than the existing one in the back up folder.
Code:
#!/bin/bash
#Sheet2
#Task2
#
backup_dir=$1'/backup'
if [ ! -d $backup_dir ]
#only if backup does not exist
then
#crates new backup directory
mkdir -p $backup_dir
#copies all files from current directory
#$PWD gives the current directory path of the script
cp -r $PWD'/' $backup_dir
exit 1
elif [ -d $backup_dir ]
#only if backup already exists
then
#iterates over all files in current directory
for file in $PWD'/'
do
if [ $file -nt $backup_dir'/$file' ]
#overrides a file in backup directory if its newer
then
cp -r $file $backup_dir
elif [ ! -a $backup_dir'/$file' ]
#copies a file if it is not existing in backup directory
then
cp -r $file §backup_dir
fi
done
fi