Need help with bash command.

T

Tux1342

Guest
I play a game called starbound, but its in early beta so I want to back up my player data every hour, as it gets corrupted from time to time. so I am trying to make a simple script to back it up:


#!/bin/sh

while [ 1 ];do cp -r ~/.starbound/player ~/Documents/Starbound\ backups/$date;sleep 1h;done

The problem is, is it doesn't name the back up after the date, it names it player, so I want it so be $date, so I can keep track of when the back up was made, how can I get it to name it after the variable?
 


I wrote a similar script to auto-deploy WAR files on my work web server. It makes a backup copy of every WAR as LinkSyb_2014_03-11.war (if it was archived today).

Here is a snippet of my script (uses bash shell) hopefully it helps:

Code:
# Variables
apache_dir="/work/apache-tomcat-7.0.41/"
archive_dir="/work/MyLinkSyb_Archives/"
curr_date=`date +"%F"`
log="deployment_log.txt"

# Check if a file was given
if [ "$#" -ne "1" ]; then
        echo "ERROR: A .war file must be provided!"
        echo "Syntax: deploy myLinkSyb.war"
        exit 1
fi

# Below does the copying and shutdown/startup of server
cp $1 ${archive_dir}LinkSyb_${curr_date}.war
echo "$1 has been archived under $archive_dir."

Make sure you modify for date format as the default is long and includes spaces. You can also just add another line to rename the directory like:
Code:
dir="~/Documents/Starbound/backups/"
mydate=`date +"%F"`
mv  ${dir}player ${dir}${mydate}

Also, Starbound rocks :cool: I play occasionally I can't wait until more stuff comes out.
 
So now I do:
dir=dir=~/Documents/Starbound\ backups/
date=$date
Then :
cp -r ~/.starbound/player $dir$date
Then I just get:
cp: target `backups/date' is not a directory
 
Two things:
You may have more to this command.
the variable $date means nothing -->
~$ echo $date

~$
so try `date` instead of $date which will output the result of running date by itself
Since $date means nothing it makes sense it is naming it player (that is the original folder/file name)
Second:
date has a lot of info and spaces and such, making it not a great choice for a filename.
try:
while [ 1 ];do cp -r ~/.starbound/player ~/Documents/Starbound\ backups/`date +%m_%d_%H`_player; sleep 1h; done

This will create a date in M_Day_Hour_player format.
man date for more info :)
 
If you're making a new folder for each save you might as well just use two statements.

Also like Snipert said date is a command like any other in bash, check the man pages for different formatting options anything without spaces will work. I use +%F which translates to 2014-03-16 for today. The ` ` back quotes are used to execute a command and assign it to a variable so myVar=`date` would assign the unformatted date to a variable whereas myVar=date will just assign the word "date" to the variable.

Code:
#!/bin/bash

origin_dir=~/Documents/hello
curr_date=`date +%F`
backup_dir=~/Documents/blah

mkdir ${backup_dir}/player_${curr_date}
cp ${origin_dir}/* ${backup_dir}/player_${curr_date}
echo "Successfully moved!"

Once dirs are assigned we make a new directory in the backup folder named "player_2014-03-16" then copy all the files in the original dir (by adding the /*) to the newly created folder. Since you're doing this every hour you may want to use hours or minutes unless you're OK with it overwriting any earlier data from the same day.

Also, sometimes when a variable is surrounded with other text (or other variables) the interpreter can get confused at least in my experience. Usually if you're using a variable around other text enclose it in brackets to specify where the variable begins and ends:
"Hellomynameis${name}andtodaysdateis${date}"

Sometimes this isn't needed, but its good practice,
 

Members online


Top