occasionally I run a backup of my phpbb forum files from the Shell command line:

zip -r forum_backup  ~/public_html/forum/*

I'd like to add date elements to the file name, so that the zip file created is automatically formed as

    forum_backup_05182013.zip

or something similar

Thanks

Dani AI

Generated

As noted by , injecting the date into the archive name is the right approach. To match the MMDDYYYY layout requested by while avoiding common pitfalls (shell glob expansion, storing full absolute paths inside the archive, cron PATH issues), build the filename in a variable, change into the web root and zip the directory name rather than an expanded file list. Example pattern:

dt=`date '+%m%d%Y'`
outfile="forum_backup_${dt}.zip"

cd ~/public_html || exit 1
zip -r -9 "../$outfile" forum -x "forum/cache/*" "forum/tmp/*"

Notes on the choices above: date '+%m%d%Y' produces the MMDDYYYY string; quoting the variable prevents word-splitting; cd into ~/public_html ensures files inside the zip are stored as forum/... instead of a long absolute path; -9 asks for maximum compression; -x patterns exclude caches or session files (quote them so the shell does not expand the patterns). Using the directory name (not *) avoids very long argument lists and the risk of the shell expanding nothing.

Rotation and safety: test this preview first, then remove older archives:

find /path/to/backups -name 'forum_backup_*.zip' -mtime +30 -print
# once verified:
find /path/to/backups -name 'forum_backup_*.zip' -mtime +30 -delete

Additional tips missing from earlier replies: include time (+%m%d%Y-%H%M) if multiple daily runs are needed; when running from cron, use absolute command paths or export PATH at the top of the script; and run the find -print step before -delete to verify which files will be removed.

Hi!

Try using the date command to generate the date for the filename. Something like this should work:

zip -r forum_backup_$(date +%Y%m%d) ~/public_html/forum/*

I took some liberty with the date format there. If you put the date first, then the month and day, it sorts nicely. You can re-arrange them if you want.

I hope this helps!
-G

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.