Last active
July 4, 2024 17:44
-
-
Save levid0s/77621f0e776dd369c12b86a7e8a0dbab to your computer and use it in GitHub Desktop.
Backup Proc OpenWRT
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
BACKUP_FILE="/tmp/proc_backup_$(date +%Y%m%d_%H%M%S).txt.gz" | |
start=$(date +%s) | |
nl=" | |
" | |
echo "Backing up /proc to: $BACKUP_FILE" | |
dump_proc() { | |
prev_base="" | |
{ find /proc -type f -maxdepth 1 ! -name 'kmsg' ! -name 'sysrq-trigger' ! -name 'softirqs' ! -name 'locks' ! -name 'mounts' | sort; find /proc -type f -mindepth 2 | sort; } | while read item; do | |
rel_path="${item#/proc/}" | |
base_item="${rel_path%%/*}" | |
if [ "$base_item" != "$prev_base" ]; then | |
echo "Processing: /proc/$base_item" >&2 | |
prev_base="$base_item" | |
fi | |
[ ! -r "$item" ] && continue | |
content=$(cat "$item" 2>/dev/null) | |
if [ "${#content}" -lt 100 ] && lines="${content//[^$nl]}" && [ "${#lines}" -eq 0 ]; then | |
echo ">>>> $item: $content" | |
else | |
echo ">>>> $item" | |
echo "$content" | |
echo "" | |
fi | |
done | |
} | |
dump_proc | gzip > "$BACKUP_FILE" | |
end=$(date +%s) | |
duration=$((end - start)) | |
echo "Backup of /proc completed successfully. ($duration seconds)" | |
echo "Backup file: $BACKUP_FILE" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
BACKUP_DIR="/tmp/proc_backup_$(date +%Y%m%d_%H%M%S)" | |
SKIP_FILES="kmsg sysrq-trigger softirqs locks mounts" | |
SKIP_PATHS="/proc/sys/net/ipv6/conf" | |
mkdir -p "$BACKUP_DIR" | |
should_skip() { | |
for skip in $SKIP_FILES; do | |
if [ "$1" = "$skip" ]; then | |
return 0 | |
fi | |
done | |
return 1 | |
} | |
should_skip_path() { | |
for skip_path in $SKIP_PATHS; do | |
case "$1" in | |
$skip_path/*) return 0 ;; | |
esac | |
done | |
return 1 | |
} | |
backup_proc() { | |
for item in /proc/*; do | |
base_item=$(basename "$item") | |
if should_skip "$base_item" || should_skip_path "$item"; then | |
echo "skipping: $item" | |
continue | |
fi | |
echo "processing: $item" | |
if [ -d "$item" ]; then | |
TEMP_DIR="$BACKUP_DIR/$base_item" | |
mkdir -p "$TEMP_DIR" | |
find "$item" -type f -o -type d | while read sub_item; do | |
sub_relative_path=${sub_item#/proc/$base_item/} | |
if [ -d "$sub_item" ]; then | |
mkdir -p "$TEMP_DIR/$sub_relative_path" | |
elif [ -r "$sub_item" ] && [ -f "$sub_item" ]; then | |
cat "$sub_item" > "$TEMP_DIR/$sub_relative_path" 2>/dev/null | |
fi | |
done | |
tar -czf "$BACKUP_DIR/${base_item}.tar.gz" -C "$TEMP_DIR" . | |
rm -rf "$TEMP_DIR" | |
elif [ -r "$item" ] && [ -f "$item" ]; then | |
cat "$item" > "$BACKUP_DIR/$base_item" 2>/dev/null | |
fi | |
done | |
} | |
backup_proc | |
echo "Backup of /proc completed successfully." | |
echo "Backup directory: $BACKUP_DIR" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment