# Backup script with tar



## Phishfry (Jun 9, 2017)

I need some help. Will someone proof-read my backup script.
Summary- APU3 - FreeBSD Headless Wireless AP on 4GB SDCARD
Using NFSv3 I have /raid1 nfs share mount. I have a sub-directory /raid1/apu3-backup/ for the tar file.
So I want to back up entire system Live except for unneeded files.
I am using a crochet file as a template.
https://github.com/freebsd/crochet/blob/master/board/BeagleBone/overlay/root/copy-to-emmc.sh
I have also excluded my NFS share folder as to not backup the backup.

Does this look right? I added the date for the filename suffix.


```
#/bin/sh
tar -c -f /raid1/apu3-backup/backup-$(date +%F).tar -C / \
        --exclude usr/src \
        --exclude usr/ports \
        --exclude usr/obj \
        --exclude 'usr/swap*' \
        --exclude mnt \
        --exclude .sujournal \
        --exclude var/run \
        --exclude dev \
        --exclude raid1 \
        .\
```


----------



## Phishfry (Jun 9, 2017)

Phishfry said:


> $(date +%F)


Well the script is running. It didn't like the date format so i dropped it for a trial run.


----------



## aragats (Jun 9, 2017)

Phishfry said:


> It didn't like the date format


That's strange, works perfectly here.


----------



## Phishfry (Jun 9, 2017)

I ended up using a variable instead:
`#!/bin/sh
present=`date +'%d-%B-%Y'`
tar -c -f /raid1/apu3-backup/$present-backup.tar -C / \
--exclude usr/src \
--exclude usr/ports \
--exclude usr/obj \
--exclude 'usr/swap*' \
--exclude mnt \
--exclude .sujournal \
--exclude var/run \
--exclude dev \
--exclude raid1 \
.\`

My only issue is a lot of strange folder names in the tar file. Like dates.


----------



## aragats (Jun 9, 2017)

So, do you mean you don't have anything like that, but they have been created in the process of archiving?
By the way, the date format looks nice, but it's not sortable, that's why I prefer year-month-day: less natural, but easy to sort and find.


----------



## Phishfry (Jul 7, 2017)

My problem with weird output in the tar file was the archive program. I had switched to something more compact at the time to view my work.
archivers/xarchiver

When I switched to another program to view my tar file all was fine.
Engrampa drags in too much from Mate to make me happy so I have been trying other archivers for Xfce4.


----------



## ShelLuser (Jul 7, 2017)

Depending on the filesystem I think you should also seriously look into dump(8) and restore(8). There are some serious advantages over using those two instead of a tarball.

For example (the main reason I personal favor it): support for incremental backups as well as interactive restores. When I need a file from a dump dump (some pun intended ) I merely use restore with the -i parameter. I then use the normal commands I'm used to for navigating through a Unix filesystem (cd, ls, etc.) and when I found the file I need I tag it and start the restore.

That beats having to think about using tar correctly to dig up one specific file (or a directory, or..).

Maybe food for thought?

Obviously: this only works when you're using UFS. When using ZFS then you shouldn't be using tar in the first place in my opinion, because there are much better (and more efficient) ways than this.


----------



## SirDice (Jul 7, 2017)

ShelLuser said:


> When using ZFS then you shouldn't be using tar in the first place in my opinion, because there are much better (and more efficient) ways than this.


There are some, not so obvious, caveats when using ZFS's send/receive. The most important one is not being able to restore the files to non-ZFS fileystems (at least not without using a ZFS intermediate). The same could be said for dump/restore (it only works on UFS). In this respect a tar(1) archive is more general and can be unpacked on most, if not all, systems. As with pretty much all things UNIX, there's more than one way to skin a cat


----------



## Phishfry (Jul 7, 2017)

Yesterdays post with the same topic (backup script)spurned me to reply. Nothing was broke but my archive viewer.
Good to see another users tar commands as well. I did not add the -z compression flag
Does this flag add the tar.'bz' to the file name I see commonly with FreeBSD images?
Can you have a gunzip compressed file with .tar extension?
Is the bz extension added automatically or does it use my filename directive?
Maybe bz is another compression scheme and it should be gz extension?
Is the -z gunziped file actually a tar file or a compressed file with a tar file inside?

I appreciate the advice. Happily using UFS.


----------



## SirDice (Jul 7, 2017)

I'm used to supplying the extension but I do believe it automatically adds one if the filename doesn't have it. Bzip and Gzip are indeed two different compression algorithms but tar(1) is usually smart enough to figure out the difference, regardless of the actual filename or extension.

I quite often just absentmindedly type `tar -zxvf somefile.ext` without actually checking the compression or the extension. Most of the time it "just works(tm)".


----------



## balanga (Jul 29, 2018)

Phishfry said:


> I ended up using a variable instead:
> `#!/bin/sh
> present=`date +'%d-%B-%Y'`
> tar -c -f /raid1/apu3-backup/$present-backup.tar -C / \
> ...



I was trying to backup a partition today to a file on the network but couldn't manage to exclude /mnt/

Do I simply need to run:-

```
cd /
tar -cvf /mnt/backup/rootfs / --exclude /mnt
```

It didn't seem to work.


----------



## ralphbsz (Jul 29, 2018)

Try "tar -cvf /mnt/backup.rootfs --exclude /mnt /".  Options come before parameters.  Once parameters start, most commands will not go back to parsing options; your tar command probably did a full backup of "/", then tried to do a full backup of something called "--exclude" (which probably gave you an error message, there is no such file or directory), and then added everything under "/mnt" to the backup, which is not what you intended.

Another option is, if you only want to backup the root file system (and no other mounted file systems, including not /mnt), try "tar -cv --one-file-sytem ...", which will not leave the file system it started in.

Two style things.  I would rather write "tar -cv -f /mnt/backup.rootfs .tar ...".  I changed two things: I didn't group the -f option together with the other two options -cv.  Why?  Because I don't like mixing options that require values (the -f) with ones that don't.  Why don't I like it?  Because I think it gets confusing, in particular if multiple options take values.  Second, I put a file extension on the output tar file.  I like file extensions; they quickly tell me what kind of file it is.


----------



## Phishfry (Jul 29, 2018)

I added a tar extension to your location.
`tar -c -f -vv /mnt/backup/rootfs.tar -C / --exclude /mnt`
So if your wondering what `-C /` does here, it changes -C to root directory / for the tar build.



ralphbsz said:


> Second, I put a file extension on the output tar file. I like file extensions; they quickly tell me what kind of file it is.


Amen to that:


----------



## Chris_H (Aug 1, 2018)

Sorry to come to the party late. 
FWIW here are a couple of solutions I use

```
#/bin/sh -
cdate="$(date +%F)"
folders="
a
bunch
of
different
folders
"
cd /
for dirs in $folders
do
tar - $dirs | xz -9e>/some/place/else/$cdate-backups.txz
done
```
OTOH
you might also find rsync(1) net/rsync useful. 

HTH

--Chris


----------

