Skip to content

Commit

Permalink
feat: Add the sync files that should be hosted in the macOS system to…
Browse files Browse the repository at this point in the history
… sync the files from the GDrive

ADSF
  • Loading branch information
ioanlucut committed Apr 8, 2024
1 parent e0f8eed commit a513c5a
Show file tree
Hide file tree
Showing 7 changed files with 215 additions and 0 deletions.
62 changes: 62 additions & 0 deletions client-sync-macos/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
# Warning. Do not forget about the following:

In MacOS, you need to add the `cron` to have full access to the disk.

# Running a Script Every Minute with Monthly Log Rotation

This guide will show you how to run a script every minute and ensure that a new log file is created every month. This
approach simplifies managing log files while ensuring frequent execution of your script.

## Modified Wrapper Script

First, we'll create a wrapper script that will run your original script with a dynamically named log file, which
incorporates the current year and month. This setup ensures a new log file is started at the beginning of each month.

```bash
#!/bin/bash

# Directory where log files will be stored
LOG_DIR="/some-path/pp7/sync/log"

# Ensure the log directory exists
mkdir -p "$LOG_DIR"

# Generate a log file name based on the current year and month
LOG_FILE_NAME=$(date +"%Y-%m")-sync-log.txt
LOG_FILE_PATH="$LOG_DIR/$LOG_FILE_NAME"

# Execute your original script with the dynamically determined log file
./sync-via-gdrive.sh -s /path/to/source -o /path/to/out -l "$LOG_FILE_PATH"
```

Make sure to replace `/path/to/source` and `/path/to/out` with your actual source directory and output directory,
respectively. After creating this script, make it executable:

```bash
chmod +x run_sync_every_minute.sh
```

## Scheduling with Cron

Next, we'll use a cron job to run the script every minute. Open your crontab for editing:

```bash
crontab -e
```

Add the following line to execute the wrapper script every minute:

```bash
* * * * * /path/to/run_sync_every_minute.sh > /some-path/cron_run.log 2 > &1
```

Replace `/path/to/run_sync_every_minute.sh` with the actual path to your wrapper script.

## Summary

This setup allows you to run a specific script every minute with a new log file generated at the start of each month.
The log files are named according to the current year and month, simplifying log management and ensuring that your
script runs frequently.

Remember to adjust the paths in the wrapper script and cron job to match your system's configuration. Ensure your script
can complete within a minute to avoid overlapping executions.
1 change: 1 addition & 0 deletions client-sync-macos/bes/bes_crontab_content.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* * * * * ~/Documents/pp7/sync/run_sync_every_minute.sh > ~/Documents/pp7/sync/cron_run.log 2 > &1
16 changes: 16 additions & 0 deletions client-sync-macos/bes/bes_run_sync_every_minute.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#!/bin/bash

cd "$(dirname "$0")"

# Directory where log files will be stored
LOG_DIR="~/Documents/pp7/sync/log"

# Ensure the log directory exists
mkdir -p "$LOG_DIR"

# Generate a log file name based on the current year and month
LOG_FILE_NAME=$(date +"%Y-%m")-sync-log.txt
LOG_FILE_PATH="$LOG_DIR/$LOG_FILE_NAME"

# Execute your original script with the dynamically determined log file
./sync-via-gdrive.sh -s /Users/bessibiu/My\ Drive/PP7\ Generated\ songs -o /Users/bessibiu/Documents/ProPresenter/Libraries/Cantece\ auto\ importate\ \(nu\ modifica\ manual\)/ -l "$LOG_FILE_PATH"
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
14 changes: 14 additions & 0 deletions client-sync-macos/template/run_sync_every_minute.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#!/bin/bash

# Directory where log files will be stored
LOG_DIR="~/Documents/pp7/sync/log"

# Ensure the log directory exists
mkdir -p "$LOG_DIR"

# Generate a log file name based on the current year and month
LOG_FILE_NAME=$(date +"%Y-%m")-sync-log.txt
LOG_FILE_PATH="$LOG_DIR/$LOG_FILE_NAME"

# Execute your original script with the dynamically determined log file
./sync-via-gdrive.sh -s /path/to/source -o /path/to/out -l "$LOG_FILE_PATH"
78 changes: 78 additions & 0 deletions client-sync-macos/template/sync-via-gdrive.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
#!/bin/bash

# Description of arguments
echo "This script syncs .pro files from SOURCE_DIRECTORY to OUT_DIRECTORY."
echo "Usage: sync-via-gdrive.sh [OPTIONS]"
echo ""
echo "Options:"
echo " -h, --help Show this help message and exit"
echo " -l, --log-file LOG_FILE Set the log file path"
echo " -s, --source-dir SOURCE_DIR Set the source directory path"
echo " -o, --out-dir OUT_DIR Set the output directory path"
echo ""

# Parse command line arguments
while [[ $# -gt 0 ]]; do
key="$1"
case $key in
-s|--source-dir)
SOURCE_DIRECTORY="$2"
shift
shift
;;
-o|--out-dir)
OUT_DIRECTORY="$2"
shift
shift
;;
-l|--log-file)
LOG_FILE="$2"
shift
shift
;;
*)
echo "Unknown option $1"
exit 1
;;
esac
done

# Ensure all required arguments are provided
if [ -z "$SOURCE_DIRECTORY" ] || [ -z "$OUT_DIRECTORY" ] || [ -z "$LOG_FILE" ]; then
echo "Error: SOURCE_DIRECTORY, OUT_DIRECTORY, and LOG_FILE must be provided."
exit 1
fi

# Ensure temp log file exists
TEMP_LOG_FILE=$(mktemp)
trap 'rm -f "$TEMP_LOG_FILE"' EXIT

# Log start of the script
TIMESTAMP=$(date +"%Y%m%d-%H%M%S")
echo "Script started at $TIMESTAMP" >> "$TEMP_LOG_FILE"

# Find directories in source directory
while IFS= read -r -d '' DIR; do
echo "Processing folder: $DIR [$TIMESTAMP]" >> "$TEMP_LOG_FILE"

# Find .pro files in each directory
while IFS= read -r -d '' FILE; do
if [[ $(basename "$FILE") != "manifest.json" ]]; then
mv "$FILE" "$OUT_DIRECTORY"
echo "Moved $(basename "$FILE") from $DIR to $OUT_DIRECTORY [$TIMESTAMP]" >> "$TEMP_LOG_FILE"
echo "Processed file: $FILE [$TIMESTAMP]" >> "$TEMP_LOG_FILE"
PRO_FILES_FOUND=true
fi
done < <(find "$DIR" -maxdepth 1 -type f -name "*.pro" -print0)
done < <(find "$SOURCE_DIRECTORY" -mindepth 1 -maxdepth 1 -type d -print0)

# Handle case when no .pro files found
if [ "$PRO_FILES_FOUND" = false ]; then
echo "No .pro files found in any folder. [$TIMESTAMP]" >> "$LOG_FILE"
fi

# Log script completion
echo "Script finished at $TIMESTAMP" >> "$TEMP_LOG_FILE"

# Move temp log file to final log file
mv "$TEMP_LOG_FILE" "$LOG_FILE"
44 changes: 44 additions & 0 deletions client-sync-macos/template/sync-via-gdrive.test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#!/bin/bash
set +x

# Create temporary directories
SOURCE_DIRECTORY=$(mktemp -d -t source_directory)
OUT_DIRECTORY=$(mktemp -d -t out_directory)
LOG_FILE=$(mktemp -t log_file)

# Print out the names of the temporary directories
echo "Source directory: $SOURCE_DIRECTORY"
echo "Output directory: $OUT_DIRECTORY"
echo "Log file: $LOG_FILE"

# Create two directories with three files each, with suffixes as timestamps
for i in {1..2}; do
timestamp=$(date +"%Y%m%d%H%M%S")
dir="$SOURCE_DIRECTORY/dir_$timestamp"
mkdir "$dir"
for j in {1..3}; do
touch "$dir/file$j-$timestamp.pro"
done
# Ensure that the timestamps for the two directories are different
sleep 1
done

# Run the script with temporary directories
./sync-via-gdrive.sh -s "$SOURCE_DIRECTORY" -o "$OUT_DIRECTORY" -l "$LOG_FILE"

# Check if files are moved to the output directory
if [ $(find "$OUT_DIRECTORY" -type f -name "*.pro" | wc -l) -eq 6 ]; then
echo "Files moved successfully."
else
echo "Error: Files not moved."
fi

# Check if log file is created
if [ -f "$LOG_FILE" ]; then
echo "Log file created successfully."
else
echo "Error: Log file not created."
fi

# Clean up temporary directories
rm -rf "$SOURCE_DIRECTORY" "$OUT_DIRECTORY" "$LOG_FILE"

0 comments on commit a513c5a

Please sign in to comment.