# Backup Media Script Documentation

This document provides an overview of the `backup-media.sh` script, which is used to back up media-related databases and configurations from various Docker containers.

## Script Overview

The script performs the following tasks:

1. Creates a log directory if it doesn't exist.
2. Generates a log file with the current date and time.
3. Defines a function to log file details.
4. Copies backup files from specified Docker containers to a backup directory.
5. Logs the details of each copied file.
6. Sends a notification upon completion.

## Detailed Steps

### 1. Create Log Directory

```bash
mkdir -p /mnt/share/media/backups/logs
```

This command ensures that the log directory exists.

### 2. Generate Log File

```bash
LOG_FILE="/mnt/share/media/backups/logs/backup_log_$(date +%Y%m%d_%H%M%S).md"
```

A log file is created with a timestamp in its name.

### 3. Log File Details Function

```bash
log_file_details() {
    local src=$1
    local dest=$2
    local size=$(du -sh "$dest" | cut -f1)
    echo "Source: $src" >> "$LOG_FILE"
    echo "Destination: $dest" >> "$LOG_FILE"
    echo "Size: $size" >> "$LOG_FILE"
}
```

This function logs the source, destination, and size of each backed-up file.

### 4. Copy Backup Files and Log Details

The script copies backup files from various Docker containers and logs the details using the `log_file_details` function. Here are the examples for all services:

```bash
docker cp sonarr:/config/Backups/scheduled /mnt/share/media/backups/sonarr/
log_file_details "sonarr:/config/Backups/scheduled" "/mnt/share/media/backups/sonarr/"

docker cp radarr:/config/Backups/scheduled /mnt/share/media/backups/radarr/
log_file_details "radarr:/config/Backups/scheduled" "/mnt/share/media/backups/radarr/"

docker cp prowlarr:/config/Backups/scheduled /mnt/share/media/backups/prowlarr/
log_file_details "prowlarr:/config/Backups/scheduled" "/mnt/share/media/backups/prowlarr/"

docker cp audiobookshelf:/metadata/backups /mnt/share/media/backups/audiobookshelf/
log_file_details "audiobookshelf:/metadata/backups" "/mnt/share/media/backups/audiobookshelf/"

docker cp tautulli:/config/backups /mnt/share/media/backups/tautulli/
log_file_details "tautulli:/config/backups" "/mnt/share/media/backups/tautulli/"

docker cp sabnzbd:/config/sabnzbd.ini /mnt/share/media/backups/sabnzbd/sabnzbd_$(date +%Y%m%d).ini
log_file_details "sabnzbd:/config/sabnzbd.ini" "/mnt/share/media/backups/sabnzbd/sabnzbd_$(date +%Y%m%d).ini"

mkdir -p /mnt/share/media/backups/jellyseerr/backup_$(date +%Y%m%d)
docker cp jellyseerr:/config/db/ /mnt/share/media/backups/jellyseerr/backup_$(date +%Y%m%d)/
log_file_details "jellyseerr:/config/db/" "/mnt/share/media/backups/jellyseerr/backup_$(date +%Y%m%d)/"

docker cp jellyseerr:/config/settings.json /mnt/share/media/backups/jellyseerr/backup_$(date +%Y%m%d)/
log_file_details "jellyseerr:/config/settings.json" "/mnt/share/media/backups/jellyseerr/backup_$(date +%Y%m%d)/"
```

### 5. Send Notification

```bash
curl \
    -H tags:popcorn,backup,sonarr,radarr,prowlarr,sabnzbd,audiobookshelf,tautulli,jellyseerr,${HOSTNAME} \
    -d "A backup of media-related databases has been saved to the /media/backups folder" \
    https://notify.peterwood.rocks/lab
```

A notification is sent upon completion of the backup process.

## Conclusion

The `backup-media.sh` script automates the process of backing up media-related databases and configurations from Docker containers, logging the details, and sending a notification upon completion.
