2.3 Bash Problems Flashcards

1
Q

Write a script to perform daily backups

A

Directories

```sh
#!/bin/bash

Directories
DirectoryToBackUp=”/home/smxky/Desktop/myapp”
BackUpDirectory=”/home/smxky/Desktop/myapp-backups”

Backup file name
DATE=$(date +”%y-%m-%d”)
BackUpName=”myapp-backup-${DATE}.tar.gz”

Ensure the backup directory exists
if [ ! -d “$BackUpDirectory” ]; then
mkdir -p “$BackUpDirectory”
fi

Command to perform the backup
if tar -czf “${BackUpDirectory}/${BackUpName}” -C “${DirectoryToBackUp}” .; then
echo “Backup successful: ${BackUpDirectory}/${BackUpName}”
else
echo “Backup failed.”
exit 1
fi
~~~

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

After creating you backup script how do you automate it?

A

You create a cron Job:
crontab -e
0 0 * * * /path/to/daily_backup.sh

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Write a bash script to automate the installation of packages

A

```sh
#! /bin/bash

read -p “What package do you wish to download?: “ pakageName

sudo apt update

if [ sudo apt install $pakagename ] ; then
echo “pakage installed”
else
echo “erro occured”
fi

~~~

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Write a script that executes the tree command.

A

```sh
#! /bin/bash

read -p “provide the path: “ path

if [ ! -d $path ] ; then
echo “No such directory”
else
tree $path

fi

~~~

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Write a script to verify if a file exist

A

```sh
#! /bin/bash

read -p “provide the path: “ path

if [ -f path ]; then
echo “File exists.”
else
echo “File does not exist.”
fi
~~~

How well did you know this?
1
Not at all
2
3
4
5
Perfectly