Add backup2me.sh
This commit is contained in:
parent
0bb0133513
commit
fe1b5584c4
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
@ -0,0 +1 @@
|
|||||||
|
*.lock
|
68
backup2me.sh
Executable file
68
backup2me.sh
Executable file
@ -0,0 +1,68 @@
|
|||||||
|
#!/bin/bash
|
||||||
|
|
||||||
|
function join {
|
||||||
|
local d=$1;
|
||||||
|
shift;
|
||||||
|
local p=$1;
|
||||||
|
shift;
|
||||||
|
echo -n "$p$1";
|
||||||
|
shift;
|
||||||
|
printf "%s" "${@/#/$d$p}";
|
||||||
|
}
|
||||||
|
|
||||||
|
lockfile-create -p -r 2 $0 || (echo "Another backup is running, aborting" & exit 1)
|
||||||
|
|
||||||
|
### Program arguments ###
|
||||||
|
|
||||||
|
# Backup target directory
|
||||||
|
BACKUP_TARGET=$1
|
||||||
|
|
||||||
|
# Backup local directory
|
||||||
|
BACKUP_DIRECTORY=$2
|
||||||
|
|
||||||
|
# Exclusions
|
||||||
|
BACKUP_EXCLUSIONS=$(join " " "--exclude=" $3)
|
||||||
|
echo "Exclusions: $BACKUP_EXCLUSIONS"
|
||||||
|
|
||||||
|
# Script executed before starting the backup
|
||||||
|
BEFORE_SCRIPT=$4
|
||||||
|
|
||||||
|
# Script executed after finishing the backup
|
||||||
|
AFTER_SCRIPT=$5
|
||||||
|
|
||||||
|
# Backup root path, change it to create a new backup
|
||||||
|
BACKUP_ROOT=$6
|
||||||
|
|
||||||
|
# Backup archive path, change it for each backup
|
||||||
|
BACKUP_ARCHIVE=$7
|
||||||
|
|
||||||
|
|
||||||
|
### Backup automatic properties ###
|
||||||
|
BACKUP_ROOT_PATH="$(cat /etc/hostname)"
|
||||||
|
BACKUP_BASE_PATH="$BACKUP_ROOT_PATH/$BACKUP_ROOT"
|
||||||
|
BACKUP_CONTENTS_PATH="$BACKUP_BASE_PATH/_backup/"
|
||||||
|
BACKUP_ARCHIVE_PATH="$BACKUP_BASE_PATH/$BACKUP_ARCHIVE"
|
||||||
|
|
||||||
|
|
||||||
|
echo "Starting backup"
|
||||||
|
|
||||||
|
### Before Script ###
|
||||||
|
eval "$BEFORE_SCRIPT"
|
||||||
|
|
||||||
|
### Create local model of the remote's backup directory tree and send it ###
|
||||||
|
mkdir -p "$BACKUP_CONTENTS_PATH"
|
||||||
|
rsync --timeout=30 -avzhP --relative "$BACKUP_CONTENTS_PATH" "$BACKUP_TARGET"
|
||||||
|
|
||||||
|
### Backup home directory except some unwanted files ###
|
||||||
|
rsync --timeout=30 -avzhP --delete --inplace $BACKUP_EXCLUSIONS --backup-dir="../$BACKUP_ARCHIVE_PATH" "$BACKUP_DIRECTORY" "$BACKUP_TARGET/$BACKUP_CONTENTS_PATH"
|
||||||
|
|
||||||
|
### Remove local model of the backup directory tree ###
|
||||||
|
rm -R "$BACKUP_ROOT_PATH"
|
||||||
|
|
||||||
|
### After Script ###
|
||||||
|
eval "$AFTER_SCRIPT"
|
||||||
|
|
||||||
|
|
||||||
|
# Remove lock
|
||||||
|
echo "Backup finished. Removing lock..."
|
||||||
|
lockfile-remove $0
|
Loading…
Reference in New Issue
Block a user