-
-
Save alexcreek/907ce5553ee5b4078f74 to your computer and use it in GitHub Desktop.
#!/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" |
Hi Alex,
Thanks for your reply. I will check and update you.
Hi ALex,
My requirement for backup script is as below,
- local server : 192.168.0.1/24 & Remote server : 192.168.0.5/24
- archieve the $targets and save it in Remote server path (192.168.0.5:/backup)
- 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
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.
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?
Since it's already using
rsync
to copy locally, you should be able to add the remote server's ip tobackup_root
and it should Just Work™backup_root=1.2.3.4:/backups