Skip to content

Instantly share code, notes, and snippets.

@arupgsh
Created February 21, 2023 19:02
Show Gist options
  • Save arupgsh/db0b84b8ea78603c0e08a2b7206296e0 to your computer and use it in GitHub Desktop.
Save arupgsh/db0b84b8ea78603c0e08a2b7206296e0 to your computer and use it in GitHub Desktop.

Revisions

  1. arupgsh created this gist Feb 21, 2023.
    50 changes: 50 additions & 0 deletions backup_remote_server.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,50 @@
    #!/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!"