I created a bash script that automated the backup of a folder. The script generates a compressed archive of the CompanyA folder with a timestamped filename.
In this task, I created a Bash shell script that automates the creation of a backup of the CompanyA folder as a compressed archive. The name of the archive was in the format date of the day-backup-companyA.tar.gz.
I had to use sudo to complete this task as I was not root.
To validate that I was in the home folder, I entered the following command:
Output:
To create a generic shell script called backup.sh, I entered the following command:
To change the file privileges to make backup.sh be executable, I entered the following command:
I used vi text editor to open the backup.sh file for editing:
To activate insert mode, I entered i
On line 1 of the script, I entered #!/bin/bash to add the shebang line, and pressed Enter to go to the next line.
To create a variable for the current date, I entered:
To create a variable for the backup file for the day, I entered:
On the next line, I entered:
Contents of backup.sh script written so far:
With my current text editor, I saved my script and exited from the editor by pressing the Esc key, entering :wq and pressing Enter.
To run backup.sh, I entered the following command:
Output:
To verify that the archive was created in the backups folder, I entered the following command:
Output:
I learned that I can schedule this type of script via cron to create a daily backup of the folder. I could also use other commands to copy this archive to other servers.