I'm trying to make a script to back up my we servers, but I have very limited knowledge with bash scripting. I want the script to name the backup file ht-"today's date". How would I go about doing that? Is it possible to use a command, like the date command, as a variable?
1 Answer
To backup directory dir
into a file named after today's date and time:
name=$(date '+ht-%F-%H-%M-%S.tgz')
tar -czf "$name" dir
You may eventually want to use a program such as rsnapshot, dirvish, or one of their competitors. These programs automate the process and also, via the use of hardlinks, save on disk space if you are keeping multiple backups.
-
1Thank you very much for this. I have a data logger that runs all the time creating the daily file called today.dat and have been having to be up at midnight every night to "mv today.dat Archive/2017-05-22.dat" before the master system scoops up all the archives after midnight.– SDsolarMay 27, 2017 at 4:27
-
@SDsolar So, I presume you use:
mv today.dat "$(date '+Archive/%F.dat')"
– John1024May 27, 2017 at 5:46 -
-
1@SDsolar Small changes can make a big difference. Instead of
date +F%
, trydate +%F
– John1024Oct 5, 2017 at 1:08 -
1Yikes. Gadzooks. Yes, of course that works. Thank you! It does help to actually understand why I am typing it so I won't make a copying error like that.– SDsolarOct 5, 2017 at 5:37