-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add backup script * Update README * Fix missing backup name * Revamp backup * Add a couple of example configs * Small tweaks and fixes * Add no-dry-run option * Clean up comments and update help * Use include & exclude files with archive backup * Update date/time format to be human readable * Check for arguments * Update sync functionality * Update help * In progress update of the README.md * Update README
- Loading branch information
Showing
9 changed files
with
264 additions
and
2 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,33 @@ | ||
# furzmunds_scripts | ||
Helpful scripts for Linux | ||
# Scripts | ||
1. ## Backup | ||
### Types of backups | ||
- Archive - Full backup to tar.gz file | ||
- Sync - Syncronised from source to destination based on changes | ||
|
||
### How To | ||
Create a backup configuration file using the fields in the table below | ||
|
||
*See also the examples in the repo* | ||
|
||
| Field | Description | Values or Examples | | ||
| ------------- | ----------------------------------- | --------------------------- | | ||
| BACKUP_NAME | Identifier and part of the filename | Valid filename | | ||
| BACKUP_TYPE | Type of backup desired | ARCHIVE or SYNC | | ||
| DESTINATION | Sync destination path | Valid directory path | | ||
| EXCLUDE_FILES | BASH array of files/dirs to exclude | ex. ("*.png" "Screenshots") | | ||
| SOURCE_FILES | BASH array of files/dirs to backup | ex. ("*.png" "Screenshots") | | ||
| WORKING_PATH | The path in which to run | Valid directory path | | ||
|
||
### Backup Configuration Example | ||
```bash | ||
BACKUP_NAME="photos" | ||
BACKUP_TYPE="ARCHIVE" | ||
DESTINATION="$HOME/backups" | ||
EXCLUDE_FILES=("*.png" "Screenshots") | ||
SOURCE_FILES=("Pictures") | ||
WORKING_PATH=$HOME | ||
``` | ||
|
||
Run the backup script using the configuration file | ||
|
||
```backup [CONFIG.bc] [OPTIONS,..]``` |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,195 @@ | ||
#!/bin/bash | ||
|
||
ParseArgs() { | ||
while test $# -gt 0; do | ||
_KEY="$1" | ||
case "$_KEY" in | ||
-n|--no-dry-run) | ||
DRY_RUN="" | ||
;; | ||
-h|--help) | ||
Help | ||
exit 0 | ||
;; | ||
-v|--version) | ||
Version | ||
exit 0 | ||
;; | ||
-*|--*) | ||
echo "Unknown argument: $_KEY" | ||
exit 1 | ||
;; | ||
*) # One positional argument | ||
BACKUP_CONFIG=$1 | ||
if [ ! -f $BACKUP_CONFIG ]; then | ||
echo "Backup configuration file not found: $BACKUP_CONFIG" | ||
exit 1 | ||
fi | ||
;; | ||
esac | ||
shift | ||
done | ||
} | ||
|
||
|
||
Help() { | ||
echo " | ||
Performs backups either by archiving files (tar.gz) or | ||
by synching files between two locations (rsync). | ||
Usage: | ||
backup [CONFIG.bc] [options] | ||
Options: | ||
-h, --help Display this help | ||
-n, --no-dry-run Skip dry-run (sync only) | ||
-v, --version Display the version | ||
" | ||
} | ||
|
||
|
||
Version() { | ||
echo "Furzmund Backup v0.0.1" | ||
} | ||
|
||
CheckInputs() { | ||
# Validate configuration file inputs | ||
# Change to working directory if given | ||
if [ -z $BACKUP_TYPE ]; then | ||
echo "Error - Missing required backup type" | ||
else | ||
if [ $BACKUP_TYPE != "ARCHIVE" ] && [ $BACKUP_TYPE != "SYNC" ]; then | ||
echo "Error - Invalid backup type: '$BACKUP_TYPE'" | ||
echo " Valid types: ARCHIVE or SYNC" | ||
exit 1 | ||
fi | ||
fi | ||
|
||
if [ -z "$SOURCE_FILES" ]; then | ||
echo " Error - Missing required source" | ||
exit 1 | ||
fi | ||
|
||
if [ -z $DESTINATION ]; then | ||
echo "Error - Missing required destination" | ||
exit 1 | ||
elif [ ! -d $DESTINATION ]; then | ||
echo " Backup destination does not exist. Attempting to create:" | ||
echo " $DESTINATION" | ||
mkdir -p $DESTINATION | ||
if [ $? -ne 0 ]; then | ||
echo "Error - Unable to create destination path: $DESTINATION" | ||
exit 1 | ||
else | ||
echo " Created directory at $DESTINATION" | ||
fi | ||
fi | ||
|
||
if [ -z $WORKING_PATH ]; then | ||
# If no working path, then the source must be an absolute path | ||
if [ ! -d $SOURCE ]; then | ||
echo "Error - Source does not exist or is a relative path:" | ||
echo " $SOURCE" | ||
echo " Either add a working directory or enter an existing source path" | ||
exit 1 | ||
fi | ||
else | ||
# tar --directory or -C does not work when source is a pattern | ||
cd $WORKING_PATH | ||
fi | ||
} | ||
|
||
|
||
ArrayToFile() { | ||
# Write the names given in an | ||
# array to the filename given | ||
# $1 = array of names | ||
# $2 = filename | ||
_names=$1[@] | ||
names=("${!_names}") | ||
printf "%s\n" "${names[@]}" >> $2 | ||
} | ||
|
||
|
||
ArchiveBackup() { | ||
echo "Starting archive backup.." | ||
tar \ | ||
--create \ | ||
--gzip \ | ||
--verbose \ | ||
--file=$BACKUP_PATH \ | ||
--exclude-from=$BACKUP_NAME.ex \ | ||
--files-from=$BACKUP_NAME.in | ||
if [ $? -eq 0 ]; then | ||
echo "Backup complete." | ||
echo " $BACKUP_PATH" | ||
else | ||
echo "Error - creating backup file" | ||
fi | ||
} | ||
|
||
|
||
SyncBackup() { | ||
echo "Starting sync.." | ||
rsync \ | ||
--archive \ | ||
--delete-after \ | ||
--exclude-from="$BACKUP_NAME.ex" \ | ||
--human-readable \ | ||
--verbose \ | ||
$DRY_RUN \ | ||
$SOURCE_FILES \ | ||
$DESTINATION \ | ||
| tee $LOG_FILE | ||
if [ $? -eq 0 ]; then | ||
echo "Sync complete." | ||
else | ||
echo "Error syncing" | ||
echo " $SOURCE_FILES" | ||
echo " $DESTINATION" | ||
fi | ||
} | ||
|
||
|
||
##################################################################### | ||
# Main # | ||
##################################################################### | ||
declare BACKUP_EXT=".tar.gz" | ||
declare DATE_TIME=$(date +"%Y-%m-%d_%H-%M-%S") | ||
declare DRY_RUN="--dry-run" | ||
|
||
# Read arguments | ||
if [ $# -lt 1 ]; then | ||
echo "No arguments given" | ||
Help | ||
exit 1 | ||
fi | ||
ParseArgs "$@" | ||
echo "Reading configuration file.." | ||
source $BACKUP_CONFIG | ||
CheckInputs | ||
BACKUP_PATH=${DESTINATION}/${BACKUP_NAME}_${DATE_TIME}${BACKUP_EXT} | ||
|
||
# Create exclude file | ||
ArrayToFile EXCLUDE_FILES "$BACKUP_NAME.ex" | ||
|
||
# Perform the backup | ||
if [ $BACKUP_TYPE = "ARCHIVE" ]; then | ||
ArrayToFile SOURCE_FILES "$BACKUP_NAME.in" | ||
ArchiveBackup | ||
elif [ $BACKUP_TYPE = "SYNC" ]; then | ||
SyncBackup | ||
else | ||
echo "Error - Bad backup type: '$BACKUP_TYPE'" | ||
exit 1 | ||
fi | ||
|
||
# Clean up | ||
if [ -f $BACKUP_NAME.in ]; then | ||
rm $BACKUP_NAME.in | ||
fi | ||
if [ -f $BACKUP_NAME.ex ]; then | ||
rm $BACKUP_NAME.ex | ||
fi | ||
|
||
exit 0 |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
# Will archive the Pictures directory in theq | ||
# HOME directory, but will skip any .jpg files | ||
BACKUP_NAME="example01" | ||
BACKUP_TYPE="ARCHIVE" | ||
SOURCE_FILES=("Pictures") | ||
DESTINATION="$PWD" | ||
WORKING_PATH="$HOME" | ||
EXCLUDE_FILES=("*.jpg") |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
# Sync Example | ||
# Will sync source to the dest directory | ||
|
||
BACKUP_NAME="example02" | ||
BACKUP_TYPE="SYNC" | ||
DESTINATION="examples/sync/dest" | ||
EXCLUDE_FILES=("secret.txt") | ||
LOG_FILE="$PWD/log.txt" | ||
SOURCE_FILES="examples/sync/source" | ||
WORKING_PATH="$PWD" |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
BACKUP_NAME="home" | ||
BACKUP_TYPE="ARCHIVE" | ||
DESTINATION="$PWD" | ||
EXCLUDE_FILES=("*.png" "Screenshots") | ||
SOURCE_FILES=(".bashrc" ".ssh" ".gitconfig" ".bash_aliases" ".gnupg" "Pictures") | ||
WORKING_PATH=$HOME |
Empty 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aenean nec dolor quis enim lacinia faucibus. Praesent tempor, lorem nec lacinia consequat, orci diam mollis risus, vel posuere ligula nulla at dui. Nullam suscipit orci et leo facilisis posuere in id ante. Mauris consectetur, sem nec pulvinar maximus, nibh tortor vulputate ante, et iaculis mi mi eu diam. Ut sed cursus orci, quis luctus urna. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia curae; Donec dictum scelerisque orci, eget feugiat lorem sagittis sed. Aenean efficitur sapien a enim cursus tristique. Donec mi quam, dictum sed odio nec, blandit condimentum massa. Fusce vestibulum nisl vel tortor venenatis, a mattis libero feugiat. | ||
|
||
Integer auctor metus vel augue rutrum, id aliquam enim rhoncus. Donec pulvinar laoreet risus, porttitor blandit ipsum maximus nec. Morbi at mattis eros, nec efficitur nulla. Fusce et dui faucibus, pharetra sapien eget, bibendum odio. Fusce id libero laoreet nisi lobortis vulputate. Curabitur cursus porttitor nunc. Ut vel odio mi. Vivamus malesuada ipsum ac urna tristique tristique. Mauris ut ullamcorper nisl. Nam nec urna lectus. Etiam facilisis aliquam nulla sit amet lobortis. Phasellus lacinia, nisl at ullamcorper semper, urna lectus pharetra nisl, et fringilla nibh nunc volutpat dolor. Nulla rutrum sit amet nisl in volutpat. Etiam quis dui tortor. | ||
|
||
Curabitur non odio in orci rhoncus aliquet. Morbi mattis vestibulum faucibus. Aliquam eget odio ut erat vehicula porta. Vivamus a ligula a tellus hendrerit eleifend ac ut nisi. Nunc nec consequat justo. Vestibulum orci sem, feugiat sit amet ligula eget, mollis placerat felis. In hac habitasse platea dictumst. Nullam ac pellentesque felis, a placerat augue. Donec sit amet ullamcorper nisi, ac tincidunt ex. Nulla arcu massa, malesuada et justo accumsan, rutrum maximus massa. Donec vestibulum eros non egestas volutpat. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Suspendisse quis est ultrices sem congue scelerisque. | ||
|
||
Etiam id sem sed nibh scelerisque congue id a arcu. Quisque venenatis velit nec ligula volutpat, vitae mollis ipsum cursus. Nulla a fermentum lacus, sit amet rhoncus odio. Phasellus pretium convallis nulla, sit amet ullamcorper nisi porttitor ut. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Sed fermentum imperdiet ligula in posuere. Donec id nisl ac enim pulvinar fermentum. Fusce lobortis quam quam, id porttitor dolor tristique vel. | ||
|
||
Donec vehicula ligula lorem, ut porta velit laoreet nec. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Integer ornare, ante quis scelerisque sodales, nunc arcu semper tellus, sit amet scelerisque mi tortor rhoncus odio. Phasellus pharetra lectus eget urna hendrerit, nec venenatis diam efficitur. Cras efficitur elit eget pellentesque semper. Fusce aliquet euismod porta. Curabitur eu est nec sem iaculis tincidunt. Donec vitae massa tortor. Mauris venenatis porttitor nulla. Nullam vestibulum, arcu vel vehicula blandit, est nulla suscipit justo, et dignissim purus dolor ac justo. Mauris et leo non tellus sodales pellentesque. Cras ac lacinia purus. Nunc quis suscipit quam. Vivamus gravida feugiat lacus nec venenatis. | ||
|
||
https://lipsum.com/ |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
secret |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.