Last active
May 18, 2023 13:59
-
-
Save alexcreek/907ce5553ee5b4078f74 to your computer and use it in GitHub Desktop.
simple backup script using rsync + tar
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
#!/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" |
Looks good. Feel free to fork this gist and modify it for your own use. That's gonna be the best way to make it work for your specific use case.
Hi alexcreek,
Many thanks for your code. Simple and efficient
Hey @alexcreek,
Thanks for this script, just what I needed. And it's simple enough.
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
Hi ALex,
My requirement for backup script is as below,
I have modified your script accordingly , kindly check the script
#!/bin/bash
today=$(date +%m%d%Y)
#local_server_targets
targets=( '/var/www' '/etc' )
#Remote server IP & path
backup_root='192.168.0.5:/backup
backup_dir=${backup_root}'/'${today}
#logs
log='/var/log/backup.log'
echo "$(date +%D" "%r): Beginning backup" >> $log
mkdir -p $backup_dir
for i in ${targets[@]}; do$i $ {backup_dir}
rsync -Ra
if [[ $? -eq 0 ]]; then
echo "Successfully backed up $i" >> $log
else
echo "Failed backing up $i" >> $log
fi
done
tar -czf $backup_dir'-backup.tgz' $backup_dir &> /dev/null
if [[ $? -eq 0 ]]; then
echo "Successfully compressed ${backup_dir}" >> $log
else
echo "Failed compressing ${backup_dir}.tgz" >> $log
fi
Remove uncompressed, duplicated files. Backups are saved in $backup_root
rm -rf $backup_dir
echo "Removing backups older than 30 days" >> $log
find $backup_root -mtime +30 >> $log
find $backup_root -mtime +30 -exec rm {} ;
echo "$(date +%D" "%r): Backup complete" >> $log
Please let me know , if you want me to make any changes to your script to meet the above mentioned requirements.