Skip to content

Instantly share code, notes, and snippets.

@sergeyklay
Created August 16, 2024 15:16
Show Gist options
  • Save sergeyklay/30365802d46485482242cde27614e00b to your computer and use it in GitHub Desktop.
Save sergeyklay/30365802d46485482242cde27614e00b to your computer and use it in GitHub Desktop.
#!/bin/zsh
#
# Please note:
#
# 1. Currently, I'm using zsh here, not sh or bash.
# 2. I use this script in macOS
# 3. You have to change GDRIVE_EMAIL and GPG_RECIPIENT
#
LIBRARY_PATH="${HOME}/Documents/Library/"
DATE=$(date +"%Y%m%d-%H%M%S")
GDRIVE_EMAIL="[email protected]"
GPG_RECIPIENT="[email protected]"
BACKUP_PATH="${HOME}/Library/CloudStorage/GoogleDrive-${GDRIVE_EMAIL}/My Drive/Calibre/"
BACKUP_FILE="calibre_library_backup_${DATE}.tar.gz"
LOGS_DIR="${HOME}/logs/calibre"
LOG_FILE="${LOGS_DIR}/calibre-backup-${DATE}.log"
TAR_CMD="/usr/bin/tar"
GPG_CMD="/usr/local/bin/gpg"
RSYNC_CMD="/usr/bin/rsync"
SQLITE_CMD="/usr/bin/sqlite3"
mkdir -p "$LOGS_DIR" || exit 1
log() {
echo "[$(date)] $1" >> "$LOG_FILE"
}
trap 'rm -f "${BACKUP_FILE}" "${BACKUP_FILE}.gpg"; exit 1' INT TERM HUP
log "Backup started..."
log "Environment vars:"
log "HOME=${HOME}"
log "LIBRARY_PATH=${LIBRARY_PATH}"
log "DATE=${DATE}"
log "BACKUP_PATH=${BACKUP_PATH}"
log "BACKUP_FILE=${BACKUP_FILE}"
log "LOGS_DIR=${LOGS_DIR}"
log "LOG_FILE=${LOG_FILE}"
log "Programs:"
log "tar: $TAR_CMD"
log "gpg $GPG_CMD"
log "rsync: $RSYNC_CMD"
log "sqlite: $SQLITE_CMD"
for cmd in $TAR_CMD $GPG_CMD $RSYNC_CMD $SQLITE_CMD; do
if ! command -v "$cmd" &> /dev/null; then
log "Command $cmd not found."
exit 1
fi
done
mkdir -p "$BACKUP_PATH" || { log "Failed to create backup directory."; exit 1; }
cd "$(dirname "$LIBRARY_PATH")" || { log "Failed to change directory."; exit 1; }
log "Current working directory: $(pwd)"
queries=(
"SELECT COUNT(*) FROM books;"
"SELECT COUNT(*) FROM authors;"
"SELECT COUNT(*) FROM languages;"
"SELECT COUNT(*) FROM series;"
"SELECT COUNT(DISTINCT format) FROM data;"
"SELECT COUNT(*) FROM publishers;"
"SELECT COUNT(*) FROM tags;"
)
results=()
for query in "${queries[@]}"; do
log "Performing query: $query"
result=$($SQLITE_CMD -batch -noheader -separator "" "$LIBRARY_PATH/metadata.db" "$query" | tr -d '[:space:]')
if [ $? -ne 0 ]; then
log "SQLite query failed for: $query"
exit 1
fi
log "Query result: \"$result\""
results+=("${result:-?}")
done
request_date=$(date +"%b %d, %Y")
script_dir=$(cd -- "$(dirname -- "$0")" && pwd)
# Coulmns in this file are:
# Date,Books,Authors,Languages,Series,Formats,Publishers,Tags
echo "\"${request_date}\",${(j:,:)results}" >> "$script_dir"/calibre-library-stats.csv
$TAR_CMD -czf "${BACKUP_FILE}" "$(basename "$LIBRARY_PATH")" >> "$LOG_FILE" 2>&1 || {
log "tar command failed."
exit 1
}
log "tar command succeeded."
$GPG_CMD --encrypt --no-armor --recipient "$GPG_RECIPIENT" "$BACKUP_FILE" >> "$LOG_FILE" 2>&1 || {
log "gpg command failed."
exit 1
}
log "gpg command succeeded."
$RSYNC_CMD -av --delete "${BACKUP_FILE}.gpg" "$BACKUP_PATH" >> "$LOG_FILE" 2>&1 || {
log "rsync command failed."
exit 1
}
log "rsync command succeeded."
$RSYNC_CMD -av --delete "$script_dir"/calibre-library-stats.csv "$BACKUP_PATH" >> "$LOG_FILE" 2>&1
rm "${BACKUP_FILE}"
rm "${BACKUP_FILE}.gpg"
log "[$(date)] Backup completed."
# Local Variables:
# sh-basic-offset: 2
# End
#!/bin/zsh
#
# Please note:
#
# 1. Currently, I'm using zsh here, not sh or bash.
# 2. I use this script in macOS
# 3. You have to change GDRIVE_EMAIL
# 4. This file should be invoked after calibre-backup.sh
#
GDRIVE_EMAIL="[email protected]"
BACKUP_PATH="${HOME}/Library/CloudStorage/GoogleDrive-${GDRIVE_EMAIL}/My Drive/Calibre/"
MAX_BACKUPS=21
cd "$BACKUP_PATH" || exit 1
ls -t | grep -E 'calibre_library_backup_\d{8}-\d{6}\.tar\.gz\.gpg$' | tail -n +$(($MAX_BACKUPS + 1)) | xargs -I {} rm -f {}
# Local Variables:
# sh-basic-offset: 2
# End:
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment