user: I need to copy files from a computer folder to a network share folder. It's for a backup.
Can you write a bash script that could do the task every day in 11 PM? Also the script should figure out which files are already copied.

assistant: Sure! Here's a sample bash script that should accomplish the task:

```bash
#!/bin/bash

# Source and destination folders
source_folder=/path/to/computer/folder
destination_folder=/path/to/network/share/folder

# Check if destination folder exists, create it if it doesn't
if [ ! -d "$destination_folder" ]; then
  mkdir -p "$destination_folder"
fi

# Copy only new and modified files
rsync -av --update "$source_folder" "$destination_folder"

# Add a log entry
echo "$(date): Backup completed" >> /var/log/backup.log
```

To run this script every day at 11 PM, you can use a cron job. To edit your crontab file, run `crontab -e` in the terminal and add the following line:

javascript

0 23 * * * /path/to/backup_script.sh

This will run the script at 11 PM every day. The `0` at the beginning specifies that it should run at the exact minute (0th minute), and the `*` for the other fields means it should run every day of the month and every month.

The `rsync` command used in the script will only copy files that are new or have been modified since the last backup. This means you don't have to worry about copying files that are already backed up.

The script also adds a log entry to `/var/log/backup.log` with the current date and time when the backup is completed, so you can check the log to make sure the backup ran successfully.

user: How to automatically check the backup logs professionally? I would rather not do it manually for 15 server every morning.

assistant: You can create a script to automatically check the backup logs and send you a summary via email. Here's a sample script that you can modify as needed:
    
    
```bash
#!/bin/bash

# Email settings
email_subject="Backup Log Summary"
email_to="your_email@example.com"

# Servers list
servers=(
  "server1:/var/log/backup.log"
  "server2:/var/log/backup.log"
  # Add more servers as needed
)

# Function to get the last log entry for a server
get_last_log_entry() {
  server_log=$1
  ssh $server_log "tail -n 1 /var/log/backup.log"
}

# Main function
main() {
  email_body=""
  
  for server_log in "${servers[@]}"; do
    last_log_entry=$(get_last_log_entry $server_log)
    email_body+="Server: ${server_log}
Last Backup: ${last_log_entry}

"
  done

  echo -e "$email_body" | mail -s "$email_subject" "$email_to"
}

main
```

Make sure to replace `your_email@example.com` with your actual email address. You can add more servers to the `servers` array by following the same format: `servername:/var/log/backup.log`.

To run this script automatically every morning, you can add another cron job. For example, if you want the email summary sent at 9 AM, add the following line to your crontab (run `crontab -e` to edit it):
    
    
```javascript
0 9 * * * /path/to/check_backup_logs.sh
```

This script assumes you have SSH key-based authentication set up between the machine running the script and the servers, so you won't be prompted for a password. It also requires the `mail` command to be available, which can be installed on most systems using your package manager (e.g., `apt-get install mailutils` on Ubuntu).

By running the `check_backup_logs.sh` script, you'll receive an email summary of the last backup log entries for all servers, making it easy to check their status without having to manually access each server.