Ubuntu – Writing an alias that puts a folder and its subfolders/files into an encrypted archive titled with the date

aliasarchivecommand lineencryption

I am trying to write an alias that puts a folder and its subfolders/files into an encrypted archive titled with the date the command is run. Preferably in format "YYYYMMDD FolderName Backup".

E.g. replace YYYYMMDD with 20150707 or whatever day the command is called.

I know how to set an alias by editing/creating the .bash_aliases file in the Home folder and adding a line like this:

alias cryptdoc="cd ~/Desktop/ && 7z a -pSome_Pass -r ~/Desktop/YYYYMMDD_Documents_Backup.7z' '/home/location/Documents/'"

A few quick notes on the code above:

  1. alias cryptdoc= sets up the alias.
  2. The quotes are included in case I have some directory with a space in the name so that I can write a command like "cd '~/My Documents/Folderhere'" which I might need to work without messing up the command.
  3. cd ~/Desktop/ is because I want the file to pop out on the desktop
  4. && is included to make sure I can do the first command and then the second as long as the first one works.
  5. 7z because it is better, a to add a file to an archive and -p to include a password within the command of my choosing. Keep in mind if you add a password after -p that it should look like this: -psomepasswordthatdoesntlookrightbutis. -r is because I want it recursive through my folders and files within the main folder.

I also realize that this means there will be a password in plain text on my machine in the alias file. But if someone has access to the alias file they also have access to my folder with these files in it anyway so it becomes irrelevant.

This should let me just go to terminal and type in cryptdoc and hit enter in order to create a 7-Zip file encrypted with my chosen password all ready for upload to some less-secure cloud storage. But How can I add a variable that actually grabs the YYYYMMDD at the time of typing cryptdoc into terminal and inserts it into the title of the document?

Best Answer

This should work fine for you. The date command is all that is needed.

alias cryptodoc="cd ~/Desktop/ && 7z a -pSome_Pass -r ~/Desktop/$(date +%Y%m%d)_Documents_Backup.7z ~/Documents/* 2>/dev/null"

the command for date +%Y%m%d means, run date, then + means format, then %Y means YYYY, %m means MM and %d means DD. Having the $( ) around the date command means to run it at the time of the command.

Hope this helps!

Related Question