Skip to content

Instantly share code, notes, and snippets.

@StoneLabs
Created April 13, 2023 06:42
Show Gist options
  • Save StoneLabs/b5fffe2d1a8b73c09c33d3d8c389b029 to your computer and use it in GitHub Desktop.
Save StoneLabs/b5fffe2d1a8b73c09c33d3d8c389b029 to your computer and use it in GitHub Desktop.
Backup script
#!/bin/zsh
#
# Copies all sources to target host
# =================================
#
# Copies all folders in ./sources to the target.
# These folder entries (seperated by newlines)
# should not end with a slash. If they do, only the
# files inside the folder are backed up, not the
# folder itself. Make sure that there are no
# trailing spaces after the entries.
#
# The ssh key used to ssh into the target should be
# located in ./ssh/id_rsa, or changed below.
#
# Additionally, you need to make sure that ./logs/
# exists. Rsync stdout will be logged to that folder.
#
#
# Version
# =======
#
# Version 1 - Initial
# On Target machine
target_ssh=root@10.10...
target_path="/Backup/backup-script/$(hostname)/backup-$(date +%A)/"
target=$target_ssh:$target_path
# On this machine
sources="./sources"
log="./logs/backup-script-$(date +\%Y\%m\%d\%H\%M\%S).log"
key="./ssh/id_rsa"
# Create folder
echo Creating folder...
ssh -i $key -o LogLevel=QUIET -t $target_ssh "mkdir -p $target_path"
# Copy files
echo Backing up sources...
xargs -0 -I '{}' -n 1 \
rsync -vza --delete "{}" -e "ssh -i $key" $target \
< <(tr \\n \\0 <$sources) 2>&1 >$log
echo Done.
/srv
/home
/etc
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment