Generate a Unique Tarball

Posted by brian on 11-01-2016
posts

I was on recently on a client project and we needed to deploy our application code using tarballs and AWS S3 and EC2.

Problem: I need a history of tarballs that have been generated incase we need to rollback. While we create backups before deploying any new code and we use Git so we can generate a new tarball for any point in time I wanted something even easier.

Solution: A shell script that is aliased to run from the terminal.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#!/bin/sh

# Define vars
current_time=$(date "+%Y.%m.%d-%H.%M.%S")
file_name=[filename here].tar.gz

# Generate tarball
tar -czvf ~/Documents/$file_name .

# Define new file_name
new_fileName=$file_name.$current_time

# Move copy of file to backup folder
cp ~/Documents/$file_name ~/Documents/<tarball archive folder>/$new_fileName

Before running this script:

  • Create a folder on your file system with write rights
  • Create an alias to the above script

Using this script I’m able to quickly generate a new tarball along with a timestamped copy saved to the file system.