Skip to content

Instantly share code, notes, and snippets.

@arupgsh
Created February 21, 2023 19:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save arupgsh/db0b84b8ea78603c0e08a2b7206296e0 to your computer and use it in GitHub Desktop.
Save arupgsh/db0b84b8ea78603c0e08a2b7206296e0 to your computer and use it in GitHub Desktop.
A script to create and trasfer server (webapps) backups to remote storage server.
#!/bin/bash
# exit when any command fails
set -e
#get start time for calculating time taken to complete the process
start=$(date +%s)
#source location, backup location and archive names
SRCDIR="/var/www/html/"
DESTDIR="/home/username/location/"
BDIR=$DESTDIR"WEBSERV_"$(date +%s)"/"
FILENAME=FILES_$(date +%s).tgz
CONF=CONFIGS_$(date +%s).tgz
# list of directories where sever/webapp configuration files are present
CONFL=("/etc/nginx/sites-available/ /srv/shiny-server/ /etc/netplan/")
#create backup directory
mkdir -p $BDIR
# create archive for www dir
tar -av --create --gzip --file=$BDIR$FILENAME $SRCDIR 2> $BDIR"_COMPRESSION".log
wait
#create backup of databases
mysqldump -u bedsect --no-tablespaces --password='dbpassword' bv3 |gzip > $BDIR"bedsect_snap.sql.gz"
mysqldump -u labwebuser --no-tablespaces --password='dbpassword' labweb |gzip > $BDIR"wordpress.sql.gz"
wait
#generate server config file backup
tar -av --create --gzip --file=$BDIR$CONF $CONFL 1>> $BDIR$FILENAME.log
wait
#delete backup files older than 1 week
#find $BDIR -mtime +7 -exec rm {} \;
wait
#sync backup to the remote server
rsync -azP /home/username/location/ username@<remote_server_ip>:/home/username/location/
wait
echo "Backup successful!"
end=$(date +%s)
echo "Elapsed Time: $(($end-$start)) seconds!"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment