tags:

views:

37

answers:

3

I am very new in bash and never coded in before but this task is stuck so need to get rid of it . I need to make bash script to make a single compressed file with several dirs.

Like -

/home/code/bots/
/var/config/
.
.
.
/var/system/

and all will be compressed to single file /var/file/bkup.[zip][tar.gz]

Thanks in advance

+1  A: 
# tar: (c)reate g(z)ip (v)erbose (f)ile [filename.tar.gz] [contents]...
tar -czvf /var/file/bkup.tar.gz /home/code/bots /var/config /var/system

# zip: (r)ecursive [filename.zip] [contents]...
zip -r /var/file/bkup.zip /home/code/bots /var/config /var/system
John Kugelman
+3  A: 

The problem as you've described it doesn't require a bash script, just tar.

tar cvzf /var/file/bkup.tar.gz /home/code/bots/ /var/config/ . . . /var/system/
ire_and_curses
A: 

You could create a bash file for it, if you intend to run it in a cronjob for example and add some other commands like a mysqldump beforehand

You need to create a file like backup.sh with the following contents (You may need to alter the path to bash, you can find bash with whereis bash)


#!/bin/bash
# 
# Backup script
# 

# Format: YEAR MONTH DAY - HOUR MINUTE SECOND
DATE=$(date +%Y%m%d-%H%M%S)

# MySQL backup file
MYSQLTARGET="/var/file/backup-mysql-$DATE.sql"

# Target file
TARTARGET="/var/file/backup-$DATE.tar.gz"

# MySQL dump
# you cannot have a space between the option and the password. If you omit the password value 
# following the --password or -p option on the command line, you are prompted for one.
mysqldump -u root -ppassword --all-databases > $MYSQLTARGET

tar -czvf $TARTARGET $MYSQLTARGET /home/code/bots /var/config /var/system

PS. This is untested code. It's just an example of how a bash script works in the current replied context.

Dennis