Created
April 28, 2020 04:24
-
-
Save seeker1983/142a9a00d13d6b1b5a92354e875344c5 to your computer and use it in GitHub Desktop.
Restore files from backup
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/bash | |
DEFAULT_DAYS=30 | |
# Check for params and display usage | |
if [ "$#" -lt 2 ]; then | |
echo " $0 - restore missing files from backup folder" | |
echo ' Usage:' | |
echo " $0 <production-dir> <backup-dir> [DAYS]" | |
echo ' - production-dir Required. Files from backup, if not present here, will be restored.' | |
echo ' - backup-dir Required. Files here will be compared to <production-dir>.' | |
echo " - DAYS Optional. Missing files older than this value won't be restored. Defaults to $DEFAULT_DAYS" | |
exit | |
fi | |
# Check provided folders exist | |
if [ ! -d "$1" ]; then | |
echo Folder \"$1\" not found. | |
exit | |
fi | |
if [ ! -d "$2" ]; then | |
echo Folder \"$2\" not found. | |
exit | |
fi | |
# Check DAYS and set to default, if not specified or incorrect | |
if [[ "$3" -lt 1 ]]; then | |
DAYS=$DEFAULT_DAYS | |
echo Days set to default value of $DEFAULT_DAYS | |
else | |
DAYS=$3 | |
fi | |
# Set variables | |
PROD_DIR=$1 | |
BACKUP_DIR=$2 | |
total_restored=0 | |
total_skipped=0 | |
# List all files in backup | |
list=`cd "$BACKUP_DIR" && find . -type f` | |
while IFS= read -r i; do | |
file="${i/\.\//}" | |
# If appropriate file is missing in production | |
if [ ! -f "$PROD_DIR/$file" ]; then | |
# Check if file is not too old | |
if [[ $(cd "$BACKUP_DIR" && find "$file" -mtime -$DAYS -print) ]]; then | |
# File to restore | |
restored_file="$PROD_DIR/$file" | |
# Create folder just in case | |
mkdir -p "`dirname "$restored_file"`" | |
# Restore file | |
cp "$BACKUP_DIR/$file" "$restored_file" | |
echo $file - restored | |
total_restored=$((total_restored+1)) | |
else | |
echo $file - SKIPPED | |
total_skipped=$((total_skipped+1)) | |
fi | |
fi | |
done <<< "$list" | |
# Display summary | |
echo Totals: restored $total_restored, skipped $total_skipped | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment