2.3 Bash Problems Flashcards
Write a script to perform daily backups
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
~~~
After creating you backup script how do you automate it?
You create a cron Job:
crontab -e
0 0 * * * /path/to/daily_backup.sh
Write a bash script to automate the installation of packages
```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
~~~
Write a script that executes the tree command.
```sh
#! /bin/bash
read -p “provide the path: “ path
if [ ! -d $path ] ; then
echo “No such directory”
else
tree $path
fi
~~~
Write a script to verify if a file exist
```sh
#! /bin/bash
read -p “provide the path: “ path
if [ -f path ]; then
echo “File exists.”
else
echo “File does not exist.”
fi
~~~