Bash script to backup content on remote server



  • Here's a very simple script to grab a file with SCP on a remote server, and then upload it to Sia. I'm running this as a cron job to make regular backups of databases and other dynamic content.

    You can define a command in cmd that will run remotely before the file is copied over from the server.

    Example - Archive a directory:
    cmd="tar cfz ${src}${file} /home/user"

    Example - Backup all MySQL databases:
    cmd="mysqldump -u USER -pPASSWORD --all-databases | gzip > ${src}${file}"

    The Sia wallet needs to be unlocked, and the local machine should be able to login without a password prompt.

    
    # Source host and directory
    host="[email protected]"
    src="/tmp/"
    
    # Source file
    file="backup-$(date +%Y-%m-%d).tar.gz"
    
    # Remote command to run before transferring file (optional)
    cmd=""
    
    # Bandwidth limit (Kbits/s)
    bw="100"
    
    # END of configurable parameters
    
    
    # Run remote command
    [ ! -z "$cmd" ] && ssh ${host} ${cmd}
    
    # Copy file
    scp -l ${bw} $host:${src}${file} /tmp/scpsia
    
    # Upload to Sia
    curl -s -X POST --data "source=/tmp/scpsia&nickname=${file}" http://localhost:9980/renter/files/upload -A "Sia-Agent"
    

Log in to reply