Skip to content

Instantly share code, notes, and snippets.

@alexcreek
Last active May 18, 2023 13:59
Show Gist options
  • Star 12 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save alexcreek/907ce5553ee5b4078f74 to your computer and use it in GitHub Desktop.
Save alexcreek/907ce5553ee5b4078f74 to your computer and use it in GitHub Desktop.
simple backup script using rsync + tar
#!/bin/bash
TODAY=$(date +%Y%m%d)
TARGETS=( '/etc' '/home' '/root' '/var/www' )
BACKUP_ROOT='/backups'
BACKUP_DIR="${BACKUP_ROOT}/${TODAY}"
echo "$(date +%D" "%r): Beginning backup"
mkdir -p $BACKUP_DIR
for i in ${TARGETS[@]}; do
if rsync -Ra $i ${BACKUP_DIR}; then
echo "Successfully backed up $i"
else
echo "Failed backing up $i"
fi
done
if tar -czf "${BACKUP_DIR}.tgz" $BACKUP_DIR &> /dev/null; then
rm -rf $BACKUP_DIR
echo "Successfully compressed ${BACKUP_DIR}"
else
rm -rf $BACKUP_DIR
echo "Failed compressing ${BACKUP_DIR}.tgz"
echo "$(date +%D" "%r): Backup failed"
exit 1
fi
echo "Removing backups older than 30 days"
find $BACKUP_ROOT -name '*.tgz' -mtime +30 -delete -print
echo "$(date +%D" "%r): Backup complete"
@Elkan76
Copy link

Elkan76 commented Aug 14, 2020

Hi alexcreek,
Many thanks for your code. Simple and efficient

@GWando
Copy link

GWando commented Jan 2, 2021

Hey @alexcreek,
Thanks for this script, just what I needed. And it's simple enough.

@karim298
Copy link

thanks for the scrypt, can you make one to restore?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment