Creating backups with tar.gz and rsync

5,498

Solution 1

Everywhere in your script you refer to $dest/$archive_file as the file you wish to create and copy, except in the rsync command that is supposed to copy it to the other host. Fix the call to rsync and try again.

Solution 2

You need to show us what actually happens to make you believe that "it doesn't copy it".

Having said that, the use of both tar and rsync together is unusual. rsync would normally be used on a tree of files itself, and it will only copy what it needs to copy, taking into account what is already there (only sends differences).

You're not wanting to do that: you are wanting to just send this tar.gz file every time. So I question the point of using rsync.

This line in your script is wrong:

file=$(file)

It is executing the command file and putting the output in a variable called file. By itself that will be error output on stderr and nothing on stdout, so $file remains empty. For the purposes of making progress I'll just assume this variable is set to the string "file".

Your script is doing this, once we resolve the contents of the variables:

tar czf /home/amp/Backups/file-Thursday-21-39 /home/amp/.ampdata/instances/Towny/
rsync file-Thursday-21-39 [email protected]:~/backup

So, as noted, you don't need to use both tar and rsync. You are using tar to store an archive locally and then sending it to a remote host. Why keep the local archive? How will you prevent them from filling up your local storage?

You can directly tar over an SSH connection:

# tar zcvf - /home/amp/.ampdata/instances/Towny/ | \
  ssh [email protected] 'cat > backup/file-Thursday-21-39.tar.gz'

Some other comments:

  • "Dayname-HH-MM" isn't a very good format in my opinion. Consider something more obvious and sortable like YYYY-MM-DD-HHMMSS.tar.gz.
  • By default SSH is not compressed but that's okay because your tar command used gzip compression. You might consider tar's other compression formats like XZ, bzip2, zstd, etc.
  • Complete backup programs have been made to do compressed, deduplicating, reliable backups. It's not always worth cooking up your own. You might consider looking into borgbackup or restic or similar.
Share:
5,498

Related videos on Youtube

Killbots2012
Author by

Killbots2012

Updated on September 18, 2022

Comments

  • Killbots2012
    Killbots2012 almost 2 years

    I'm trying to use this bash script to create and send the tar.gz file to my backup server, some reason it doesn't seem to copy it, is there a better solution to what I'm trying to do?

    I'd rather have it compressed before I send it over the network

    #!/bin/bash
    ####################################
    #
    # Backup to NFS mount script.
    #
    ####################################
    
    # What to backup. 
    backup_files="/home/amp/.ampdata/instances/Towny/"
    
    # Where to backup to.
    dest="/home/amp/Backups"
    
    # Create archive filename.
    day=$(date +%A)
    time=$(date +%H-%M)
    file=$(file)
    archive_file="$file-$day-$time.tgz"
    
    # Print start status message.
    echo "Backing up $backup_files to $dest/$archive_file"
    date
    echo
    
    # Backup the files using tar.
    tar czf $dest/$archive_file $backup_files
    rsync $archive_file [email protected]:~/backup
    # Print end status message.
    echo
    echo "Backup finished"
    date