1. The Backup_scripts.zip contains scripts which will copy the latest backup report of the given database to the given backup location on the target machine. If the given database is not present in the system, then only the latest available backup report will be copied. If the given database is present then a schedule (or cron job) will be created that copies the latest available backup report of that database every 30 mins.

2. On extracting Backup_scripts.zip, two script files, a log file and a readme.txt file can be seen as shown below.

Backup_scripts/
├── backup.log
├── backup.sh
├── readme.txt
└── schedule_backup.sh


3. User should not execute the 'backup.sh' script. It will be executed by the schedule which copies the latest backup report of the given database.

4. User should execute the 'schedule_backup.sh' script as 'root' user, to Create or Delete a schedule for the required database. On executing 'schedule_backup.sh' script, below options will be provided.

*****************************************************
1. Create schedule to copy latest Backup Report
2. Delete schedule that copies latest Backup Report
3. Exit
*****************************************************

Option [1 | 2 | 3]:

5. On entering '1', details like database name, target machine IP address, OS user name of the target machine as whom the connection should be made to copy the backup report, password of the same OS user of target machine and backup location on the target machine, will be prompted. 

Below is the sample output of option '1' for different scenarios:

	Scenario 1 : If backup report(s) of the given database name is not present in the system. In this case, script just exits without doing any action.

		Option [1 | 2 | 3]:
		1

		Enter database name whose latest backup report must be copied: 
		testdb
		No backup reports found for the testdb database. Exiting...

	Scenario 2 : If backup report(s) of given database is present but the database is not present in the system. In this case, latest available will be just copied by the script and no schedule will be created.

		Option [1 | 2 | 3]:
		1

		Enter database name whose latest backup report must be copied: 
		mydb4

		Enter IP address of target machine where latest backup report must be copied: 
		10.128.5.133

		Enter OS user name of 10.128.5.133, as whom the connection should be made to copy the latest backup report: 
		root

		Enter password of 'root' user of 10.128.5.133 machine: 

		Enter backup location of 10.128.5.133 machine where the latest backup report must be copied: 
		/tmp

		mydb4 database is not present. Only latest backup report will be copied without creating the schedule
		Successfully created /tmp/Backup_scripts/.ssh directory
		Successfully generated SSH keys at /tmp/Backup_scripts/.ssh directory
		spawn ssh-copy-id -i .ssh/id_rsa root@10.128.5.133
		/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: ".ssh/id_rsa.pub"
		/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
		/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
		FIPS mode initialized
		root@10.128.5.133's password: 

		Number of key(s) added: 1

		Now try logging into the machine, with:   "ssh 'root@10.128.5.133'"
		and check to make sure that only the key(s) you wanted were added.

		Successfully copied the public key to 10.128.5.133
		Copying latest backup report to 10.128.5.133:/tmp by connecting as root user
		FIPS mode initialized
		FIPS mode initialized

	Scenario 3 : If the backup report(s) of the given database and also database itself is present in the system. In this case, a schedule to copy latest backup report for every 30 mins will be created by the script.

		Option [1 | 2 | 3]:
		1

		Enter database name whose latest backup report must be copied: 
		mydb5

		Enter IP address of target machine where latest backup report must be copied: 
		10.128.5.133

		Enter OS user name of 10.128.5.133, as whom the connection should be made to copy the latest backup report: 
		root

		Enter password of 'root' user of 10.128.5.133 machine: 

		Enter backup location of 10.128.5.133 machine where the latest backup report must be copied: 
		/tmp

		SSH keys already exists at /tmp/Backup_scripts/.ssh directory
		SSH connection already exists to 10.128.5.133
		 /etc/cron.allow exist. Adding root user..
		Successfully created cron job to copy latest backupreport of mydb5 database

	To verify that a schedule is created, the command 'crontab -l' can be executed as below.

	crontab -l | grep -i mydb5
	*/30 * * * * /bin/bash /tmp/Backup_scripts/backup.sh mydb5 /tmp/Backup_scripts 10.128.5.133 root /tmp >> /tmp/Backup_scripts/backup.log 2>&1

	Scenario 4 : If a schedule for a given database is already present in the system. In this case, the script quits asking to remove the existing script using optoion '2'.

		Option [1 | 2 | 3]:
		1

		Enter database name whose latest backup report must be copied: 
		mydb5
		Scheudule to copy latest backup report is already present for mydb5 database. Existing schedule must be deleted using option 2 to schedule new one. Exiting...


6. The logs generated by schedule are captured in backup.log file and that will be present in the directory where above scripts are present. The log enteries would be having timestamp and database name. Below is the log snippet.

Sun Mar 5 14:10:04 UTC 2023 mydb2 : Successfully listed all the backupreports of mydb2 database
Sun Mar 5 14:10:04 UTC 2023 mydb2 : Successfully sorted all backupreports of mydb2 database
Sun Mar 5 14:10:04 UTC 2023 mydb2 : Found latest backupreport for mydb2 database
Sun Mar 5 14:10:06 UTC 2023 mydb2 : Successfully created the backupreport JSON locally with ID

7. On entering '2', name of the database whose schedule needs to be deleted will be prompted. On specifying the name, its corresponding schedule corresponding will be deleted.

	Option [1 | 2 | 3]:
	2

	Enter database name whose schedule that copies the latest backup report must be deleted: 
	mydb5
	Successfully deleted schedule that copies latest backup report of mydb5 database

8. On entering '2' and providing the database name, if a schedule is not present for the given database name, then script exits without any action.

	Option [1 | 2 | 3]:
	2

	Enter database name whose schedule that copies the latest backup report must be deleted: 
	mydb4
	No schedule found for mydb4 database. Exiting...

9. On entering '3'. the script will exit. The script will also exist if any values other than '1' or '2' is provided.

10. SSH key based authentication will be used between the source and target machines to copy the latest backupreport. The SSH key will be generated in .ssh folder in the directory where above scripts are present.

11. Existing schedule can be listed using 'crontab -l' command. If the user wants to change the frequency of existing schedule, then 'crontab -e' must be executed and edit the cron expression of the required schedule. 
