0

I made a script to, every 6 hours, copy a folder to a backup folder, then put them in /var/www/html/backup/.

My command to copy the files was cp -vR ~/world ~/backups/Backup\ on\ $date/world

My desired result was for each backup to become a folder, and for one folder to be inside it, called world, which is copied from my home folder.

Instead, my command kept the full path of the folder. This meant that the the folder structure of the backup ended up being:

Backup on 5-3-2019 ↳ home ↳ elijahmc ↳ backups ↳ Backup on 5-3-2019 ↳ world

To try to fix this, I changed the arguments from cp -vR to cp -vr, and this fixed the folder under /backups, but not when I move it to a Zip.

To recap, I changed cp -vR to cp -vr and now have this directory structure:

Backup on 5-3-2019 ↳ world

I now turn it into a zip like so:

rm -f ~/backup.zip (remove original zip)

zip -r ~/backup.zip ~/backups/Backup\ on\ $date (turn new backup into backup.zip in root directory

mv -f ~/backup.zip /var/www/html/backup/ (move backup.zip to web server)

Downloading backup.zip from the Apache server, the file structure has the same issue as the original command:

Backup on 5-3-2019 ↳ home ↳ elijahmc ↳ backups ↳ Backup on 5-3-2019 ↳ world

Does anybody know how to fix this?

2 Answers2

1
  1. To copy folder world to ~/backups/backup_2019-05-04:

    backupdir=~/backups/backup_$(date +%F)
    mkdir -p "$backupdir"
    cp -av ~/world "$backupdir"
    

    I used flag -a for archive mode which includes -R. You don't need to specify world in the target argument.

  2. To only include the world folder in zip file ~/backup_2019-05-04.zip:

    cd "$backupdir"
    zip -r ~/backup_$(date +%F).zip *
    

    You made the mistake to use the full path to the target directory when you "zipped" the directory.

Freddy
  • 2,039
  • 7
  • 13
  • Thanks for your answer, it did work for me, but not at first. I thought you were wrong until I realized that `$backupdir` in my `mv` command didn't have the `$` in the start, but after me correcting my error, the local backup worked great. ZIP files now go `backup-**date**/~/backups/backup-on-**date**/world/`. – Elijah Ciali May 04 '19 at 09:46
-1

Yeah, I'd say you should go take a course on Bacula as it'll be easier in the long run...but you better learn bash first.

  • ~ expands to the value of $HOME,
  • The cp command takes a source (from) and a destination (to) argument;
    • If you want to copy a directory make sure just after cp you use -R to copy recursively.
  • Put that '$date' in a subshell ie $(date <args>) so you can get the proper format like mm-dd-yyyy there will be more info on that in man date or date --help
leeand00
  • 4,869
  • 15
  • 69
  • 110
  • I'm pretty sure I know all of this. I'm not saying that it's copying from the wrong location, it **does** copy the right files, just under a copy of the path. I want it to copy `~/world`, not the entire path with it. I already have a $date variable, I just didn't include it. Do you want the full script? – Elijah Ciali May 04 '19 at 06:00
  • Normally `~` expands to the value of `$HOME`, not `/home/`. – user9517 May 04 '19 at 07:22
  • Oh - I see now you put it inside `<` and `>` which have special meaning, - @leeand00 is correct you need to learn the formatting stuff. – user9517 May 04 '19 at 07:24
  • @Freddy I was using a tablet. I'll fix it. You never can tell exactly what character you're typing on a tablet. – leeand00 May 04 '19 at 22:08