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"
@pavankulka
Copy link

Hi Alex,
Thanks for this script.
Could you please help how to modify this script , if your backup directory is in remote server.

@alexcreek
Copy link
Author

alexcreek commented Jun 19, 2019

Since it's already using rsync to copy locally, you should be able to add the remote server's ip to backup_root and it should Just Work™

backup_root=1.2.3.4:/backups

@pavankulka
Copy link

Hi Alex,

Thanks for your reply. I will check and update you.

@pavankulka
Copy link

Hi ALex,
My requirement for backup script is as below,

  1. local server : 192.168.0.1/24 & Remote server : 192.168.0.5/24
  2. archieve the $targets and save it in Remote server path (192.168.0.5:/backup)
  3. rsync the $backup_dir with the local directories

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
rsync -Ra $i ${backup_dir}
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.

@alexcreek
Copy link
Author

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.

@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