[小技巧]mac backup script cron + copy to telegram

date
Feb 25, 2023
slug
tip-mac-backup
status
Published
summary
小技巧
tags
tip
type
Post
URL

Problem & Summary

    Solution

    #!/bin/bash
    
    # Backup important files to telegram
    # Copy to PSN
    # 2023/02/24
    
    log() {
    	    echo "$(date "+%Y-%m-%d %H:%M:%S")" "$1"
        }
    
    # telegram
    api_key='bot1577xxxxxxx:AAHTPb1MJ7ZZtWBcxxxxxxx'
    chat_id='-100130xxxxx'
    
    # timestamp
    BACKUP_TIMESTAMP=$(date "+%Y-%m-%d")
    
    # Define an array of filenames to copy
    files_to_copy=(
      ~/".config/clash" \
      ~/".zshrc" \
    )
    
    # Get the directory where the script is located
    script_dir="$( cd "$( dirname "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )"
    target_dir=$script_dir/$BACKUP_TIMESTAMP
    echo "making backup for $target_dir"
    mkdir $target_dir
    
    # Loop over each file in the array and copy it to the script directory
    for file in "${files_to_copy[@]}"
    do
        if [ -d "$file" ]; then
            cp -r "$file" "$target_dir/"
        else
            cp "$file" "$target_dir/"
        fi
    
        # generate crontab
        crontab -l > $target_dir/crontab
        history > $target_dir/command_history
    done
    
    
    
    ###### use openssl
    #fName=/tmp/tgbaklp00tt_$(hostname)_$(date "+%F-%H%M%S").tar.gz
    #fNameEnc=$fName.enc
    #tar -czvPf  $fName $fList
    #openssl enc -aes256 -in $fName -out $fNameEnc -pass pass:"${BACKUPPASS}" -md sha1
    # decrypt use -
    # openssl enc -aes256 -in [encrypted backup] -out decrypted_backup.tar.gz -pass pass:[backup password] -d -md sha1
    
    BACKUPPASS='xxxxxxxxxxx'
    ###### use zip
    fName=$script_dir/tgbaklp00tt_$(hostname)_$(date "+%F-%H%M%S").zip
    log "zipping to $fName"
    fNameEnc=$fName
    
    # -v for verbose, add for debug
    #zip -v -9 --symlinks -P ${BACKUPPASS} -r $fNameEnc $target_dir
    # -q for quiet
    zip -q -9 --symlinks -P ${BACKUPPASS} -r $fNameEnc $target_dir
    
    #####
    log "--- copying to telegram"
    curl -F chat_id=$chat_id -F document=@"${fNameEnc}" https://api.telegram.org/$api_key/sendDocument  > /dev/null
    
    #rm -rf $fName $fNameEnc
    
    exit 0

    Cron

    0 2 * * * cd ~/_my/cloudsxxxxx/SynologyDrive/__QH_PSN/_PSN_Config/__bak; HTTP_PROXY=http://127.0.0.1:17890 HTTPS_PROXY=http://127.0.0.1:17890 bash ./psn_data_backup.sh
     

    © Ying Bun 2021 - 2024