From 2e020da33b7848c230edc511e8ba2fbefeee3b33 Mon Sep 17 00:00:00 2001 From: Lee Date: Tue, 28 Nov 2023 22:38:19 +0000 Subject: [PATCH] Add backups/backup.sh --- backups/backup.sh | 54 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 backups/backup.sh diff --git a/backups/backup.sh b/backups/backup.sh new file mode 100644 index 0000000..b4c4ba4 --- /dev/null +++ b/backups/backup.sh @@ -0,0 +1,54 @@ +#!/bin/bash + +# Configuration +desired_network="your_local_network" +# Replace with the actual process name +# You can find it with: ps -eo comm= +process_to_find="Minecraft_Bedro" +dir_to_backup="/home/deck/.local/share/mcpelauncher/games/com.mojang/minecraftWorlds/" +backup_path="/home/deck/Documents/mc_bedrock_backups/" +max_backups=48 # Set the maximum number of backups to keep + +# Interval Values +found_network=false +found_process=false + +# Check if the desired network is found +wireless_network_names=$(nmcli -t -f active,ssid dev wifi | grep yes | cut -d: -f2) +for network in $wireless_network_names; do + if [ "$network" == "$desired_network" ]; then + found_network=true + break + fi +done + +# If the desired network is not found, print a message and exit +if [ "$found_network" != true ]; then + echo "The desired network was not found. Not running..." + exit 1 +fi + +# Check if the desired process is running +if pgrep -x "$process_to_find" > /dev/null; then + found_process=true +else + echo "The desired process is not running. Not creating backups..." + exit 1 +fi + +cd "$dir_to_backup" + +# Backup the directory and save it with maximum compression +backup_filename="backup_$(date '+%Y%m%d_%H%M%S').zip" +zip -r -9 "$backup_path/$backup_filename" "./" + +# Check if the zip process was successful +if [ $? -eq 0 ]; then + echo "Backup successful. Saved to: $backup_path/$backup_filename" + + # Remove older backups to keep only the last N backups + ls -t "$backup_path" | tail -n +$((max_backups + 1)) | xargs -I {} rm -- "$backup_path/{}" + echo "Older backups removed to keep the last $max_backups backups." +else + echo "Backup failed." +fi