6

I have linux installed on SD card, I used this command to install the rootfs

tar xpjf rootfs.tar.bz -C /mnt/rootfs/

Now, I made some changes to the rootfs and I would like to create a backup that I can use with the same command above, I tried using:

tar cpjf rootfs.tar.bz2 /mnt/rootfs
and
tar cpjf rootfs.tar.bz2 -C / mnt/rootfs
I also tried
tar cpjf rootfs.tar.bz2 /mnt/rootfs/*

And tried:

cd /mnt/rootfs
tar -cvpjf rootfs.tar.bz2 --exclude=/rootfs.tar.bz2 .
tar: ./rootfs.tar.bz2: file changed as we read it

but I end up with an archive that has two levels before the file system i.e mnt/rootfs/files What am I doing wrong ?

casperOne
  • 70,959
  • 17
  • 175
  • 239
iabdalkader
  • 15,788
  • 2
  • 42
  • 68

1 Answers1

12

That's because it starts from current working directory, you can do:

cd /mnt/rootfs
tar cpjf /rootfs.tar.bz2 .

And that should create an archive at /rootfs.tar.bz2 with its root at the contents of /mnt/rootfs/

alex88
  • 4,398
  • 3
  • 36
  • 55
  • 2
    Don't use `*`. That might fail with files named like "--filename". Instead, use `./*` . – Ambroz Bizjak Jul 09 '12 at 12:41
  • Also, it might be worth noting that `./*` (and `*`) might not pick up files whose names begin with a dot. – Ambroz Bizjak Jul 09 '12 at 12:43
  • Thanks for the tip, i used * as the user posted the question with that, corrected the answer ;) – alex88 Jul 09 '12 at 12:43
  • @AmbrozBizjak so how do I include hidden files ? – iabdalkader Jul 09 '12 at 12:50
  • 1
    @mux `tar cjpf /rootfs.tar.bz2 .` – Ambroz Bizjak Jul 09 '12 at 12:53
  • @mux the alternative is stuff like `./* ./.*` which you may notice doesn't work quire right, because `./.*` will include `.` and `..`. Another problem with wildcards (`./*`) is that if there's nothing in the folder at all, it will not expand, and tar will try to pack a file named `./*`, which does not exist. – Ambroz Bizjak Jul 09 '12 at 12:55
  • Thank you all, I just tried installing from the backup and it boots fine. – iabdalkader Jul 09 '12 at 12:55
  • @alex88 you really should change that to a dot, its the only solution that just works right in all cases – Ambroz Bizjak Jul 09 '12 at 12:59
  • @AmbrozBizjak I will make another backup just in case, but, just to be clear, the right way to include everything (including hidden files) is tar cjpf /rootfs.tar.bz2 . right ? – iabdalkader Jul 09 '12 at 13:00
  • @mux yes, but this only applies to hidden files in the root directory, which probably doesn't matter in the specific case of backing up a rootfs. Hidden files in subdirectories will be included all right. – Ambroz Bizjak Jul 09 '12 at 13:01
  • @AmbrozBizjak edited, thanks for that again! – alex88 Jul 09 '12 at 13:02
  • 9
    `tar cpjf /rootfs.tar.bz2 -C /mnt/rootfs .` Where `-C` means "cd into then ...." Is simpler. – sabgenton Aug 14 '13 at 04:16