You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
96 lines
2.0 KiB
96 lines
2.0 KiB
5 years ago
|
#!/bin/sh
|
||
|
|
||
|
BACKUP_COUNT_MAX=5
|
||
|
|
||
|
# Backup individual files
|
||
|
FILES="
|
||
|
/etc/netctl/eth0-dhcp
|
||
|
"
|
||
|
|
||
|
# Backup all files from folder
|
||
|
FOLDER_FILES="
|
||
|
/etc/netctl
|
||
|
"
|
||
|
|
||
|
# Backup complete folder (recursively)
|
||
|
FOLDERS="
|
||
|
/etc/netctl
|
||
|
"
|
||
|
|
||
|
# Database credentials
|
||
|
USER="username"
|
||
|
PASSWORD="password"
|
||
|
HOST="127.0.0.1"
|
||
|
|
||
|
# Database names
|
||
|
DATABASES="
|
||
|
example
|
||
|
example2
|
||
|
"
|
||
|
|
||
|
# ------------------------------------ #
|
||
|
|
||
|
### Remove / create backup directories ###
|
||
|
|
||
|
if [ "$(dirname "$0")" != "." ]; then
|
||
|
echo "Please run this script from the directory it resides."
|
||
|
exit 1
|
||
|
fi
|
||
|
|
||
|
# All directories
|
||
4 years ago
|
BACKUP_FILES="$(find . -mindepth 1 -maxdepth 1 -type d -printf '%f\n' | sort -r)"
|
||
5 years ago
|
|
||
|
# Current amount of backups
|
||
4 years ago
|
BACKUP_COUNT=$(echo "$BACKUP_FILES" | wc -l)
|
||
5 years ago
|
|
||
|
# Remove the oldest backup folders
|
||
4 years ago
|
if [ "$BACKUP_COUNT" -ge $BACKUP_COUNT_MAX ]; then
|
||
|
BACKUPS_OLDEST="$(echo "$BACKUP_FILES" | sed -nE "
|
||
|
1,${BACKUP_COUNT_MAX}d;
|
||
|
s/^([0-9]{2}|-|_|:){12}$/\0/p
|
||
|
")"
|
||
|
|
||
|
printf "%s\n%s\n" "Removing directories:" "$BACKUPS_OLDEST"
|
||
|
rm -rf $BACKUPS_OLDEST
|
||
5 years ago
|
fi
|
||
|
|
||
|
# Execution location
|
||
|
DIR="$(pwd)"
|
||
|
|
||
|
# Create new backup folder
|
||
|
BACKUP_NAME="$(date +%Y-%m-%d_%H:%M:%S)"
|
||
|
mkdir "$DIR/$BACKUP_NAME"
|
||
|
|
||
|
### Start backup procedure ###
|
||
|
|
||
|
# Individual files
|
||
|
for FILE in $FILES; do
|
||
|
mkdir -p "$DIR/$BACKUP_NAME/$(dirname "$FILE")"
|
||
5 years ago
|
cp -a "$FILE" "$DIR/$BACKUP_NAME/$FILE"
|
||
5 years ago
|
done
|
||
|
|
||
|
# All files inside folder
|
||
|
for FOLDER_FILE in $FOLDER_FILES; do
|
||
|
mkdir -p "$DIR/$BACKUP_NAME/$FOLDER_FILE"
|
||
|
|
||
|
# Usind find -> read ensures filename space support
|
||
|
find "$FOLDER_FILE" -maxdepth 1 -type f | while read -r FILE; do
|
||
|
cp "$FILE" "$DIR/$BACKUP_NAME/$FILE"
|
||
|
done
|
||
|
done
|
||
|
|
||
|
# Complete folder
|
||
|
for FOLDER in $FOLDERS; do
|
||
|
mkdir -p "$DIR/$BACKUP_NAME/$FOLDER"
|
||
|
cp -r "$FOLDER" "$DIR/$BACKUP_NAME/$(dirname "$FOLDER")"
|
||
|
done
|
||
|
|
||
|
# Set default file permissions
|
||
|
umask 177
|
||
|
# MYSQL Dump
|
||
|
for DATABASE in $DATABASES; do
|
||
5 years ago
|
# mysqldump --user=$USER --protocol=socket -S /var/run/mysqld/mysqld.sock "$DATABASE" \
|
||
5 years ago
|
mysqldump --user=$USER --password=$PASSWORD --host=$HOST "$DATABASE" \
|
||
5 years ago
|
> "$DIR/$BACKUP_NAME/$DATABASE-$BACKUP_NAME.sql"
|
||
|
done
|